有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在使用surface view录制视频时拍照(快照)

我已经用surface view录制完视频了。现在我想知道“如何在使用surface view录制视频时拍照(快照)??谁能帮帮我吗。!我需要在mycam按钮中添加什么来拍照

public class ActiveCam extends Activity implements SurfaceHolder.Callback 
{
  boolean recording = false;
  MediaRecorder mediaRecorder;
  SurfaceHolder surfaceHolder;
  Button myStart;
  Button myStop;
  Button mycam;
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    recording = false;
    mediaRecorder = new MediaRecorder();
    initMediaRecorder();
    setContentView(R.layout.camact); //This is my XML file

    Button b3=(Button)findViewById(R.id.button3);
    SurfaceView myVideoView = (SurfaceView)findViewById(R.id.preview);
    surfaceHolder = myVideoView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    myStart = (Button)findViewById(R.id.button1);
    myStart.setOnClickListener(myButtonOnClickListener);
    myStop = (Button)findViewById(R.id.button2);
    myStop.setOnClickListener(myButtonOnClickListener1);
    mycam = (Button)findViewById(R.id.button3);
    mycam.setOnClickListener(myButtonOnClickListener3);

  }
  private Button.OnClickListener myButtonOnClickListener1 = new  Button.OnClickListener()
  {

    @Override
    public void onClick(View arg0) 
    {
        // TODO Auto-generated method stub

        mediaRecorder.stop();
        mediaRecorder.release();
        mediaRecorder = null;
        //finish();
    }

  };
Camera.PictureCallback mCall = new Camera.PictureCallback()  
{  
    @Override  
    public void onPictureTaken(byte[] data, Camera camera)  
    {  
        //decode the data obtained by the camera into a Bitmap  
        bmp = BitmapFactory.decodeByteArray(data, 0, data.length);  
        //set the iv_image  
        Toast.makeText(ActiveCam.this,"cap", Toast.LENGTH_LONG).show();
        //iv_image.setImageBitmap(bmp);  
    }  
};  

private Button.OnClickListener myButtonOnClickListener3 = new Button.OnClickListener()
{

    @Override
    public void onClick(View arg0) 
    {

        //I need to take picture after pressing this button...What I needs to add    here??? 

    }

};      
private Button.OnClickListener myButtonOnClickListener = new Button.OnClickListener()
{

    @Override
    public void onClick(View arg0) 
    {
       mediaRecorder.start();
   recording = true;
   myStop.setText("STOP");
    }
 };
 @Override
 public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 
 {
    // TODO Auto-generated method stub

}
@Override
public void surfaceCreated(SurfaceHolder arg0) 
{
    // TODO Auto-generated method stub
    prepareMediaRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) 
{
    // TODO Auto-generated method stub

}
private void initMediaRecorder()
{
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

    mediaRecorder.setProfile(camcorderProfile_HQ);
    mediaRecorder.setOutputFile("/sdcard/your.mp4");
     //mediaRecorder.setMaxFileSize(..set size..); 
}
private void prepareMediaRecorder()
{
  mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
  try 
  {
     mediaRecorder.prepare();
  }
  catch (IllegalStateException e) 
  {
     // TODO Auto-generated catch block
     e.printStackTrace();
  } 
  catch (IOException e) 
  {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

}

这是我的XML文件

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" >
<Button
      安卓:id="@+id/button1"
      安卓:layout_width="wrap_content"
      安卓:layout_height="wrap_content"
      安卓:layout_alignParentLeft="true"
      安卓:layout_alignParentTop="true"
      安卓:text="Start" />

  <Button
      安卓:id="@+id/button2"
      安卓:layout_width="wrap_content"
      安卓:layout_height="wrap_content"
      安卓:layout_alignParentRight="true"
      安卓:layout_alignParentTop="true"
      安卓:layout_marginRight="16dp"
      安卓:text="Pause" />

<SurfaceView
    安卓:id="@+id/preview"
    安卓:layout_width="500px"
    安卓:layout_height="500px"
    安卓:layout_alignLeft="@+id/textView1"
    安卓:layout_alignParentBottom="true"
    安卓:layout_alignParentRight="true"
    安卓:layout_marginBottom="34dp" />
   <Button
    安卓:id="@+id/button3"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentLeft="true"
    安卓:layout_below="@+id/spinner1"
    安卓:layout_marginLeft="20dp"
    安卓:layout_marginTop="36dp"
    安卓:text="Button" />
  </RelativeLayout>

共 (1) 个答案

  1. # 1 楼答案

    RecordingView link&link。你可以通过这个code拍摄快照 如果xml中有videoView,则必须将linearLayout作为其父级(videoView作为linearLayout的子级),以及何时保存linearLayout图像,这意味着您将保存videoView图像

    试试这个,读一下我发送的用来拍摄快照的代码

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"
              android:text="Start" />
    
          <Button
              android:id="@+id/button2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:layout_alignParentTop="true"
              android:layout_marginRight="16dp"
              android:text="Pause" />
    
      <ImageView
        android:layout_width="120dip"
        android:layout_height="120dip"
        android:text="Take Screenshot"
        android:id="@+id/ImageView01"
        android:layout_centerHorizontal="true"
        />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="500px"
            android:layout_height="500px"
            android:id="@+id/LinearLayout01"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="#FF0000"
            android:layout_marginBottom="34dp">
        <SurfaceView
            android:id="@+id/preview"
            android:layout_width="400px"
            android:layout_height="400px"
            android:layout_gravity="center"
             />
        </LinearLayout>
           <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/spinner1"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="36dp"
            android:text="Button" />
          </RelativeLayout>
    

    希望能有所帮助

    将此添加到Button3 Listner

    private Button.OnClickListener myButtonOnClickListener3 = new Button.OnClickListener()
    {
    
        @Override
        public void onClick(View arg0) 
        {
    
            //I need to take picture after pressing this button...What I needs to add    here??? 
            //View v1 = L1.getFocusedChild();
            L1.setDrawingCacheEnabled(true);
            Bitmap bm = L1.getDrawingCache();
            BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
            ImageView image = (ImageView) findViewById(R.id.ImageView01);
            image.setBackgroundDrawable(bitmapDrawable);
        }
    
    };  
    

    改变你的地面经理

    <SurfaceView
                android:id="@+id/preview"
                android:layout_width="400px"
                android:layout_height="400px"
                android:layout_gravity="center"
                 />
    

    并在线性布局中添加android:background="#FF0000"

    它将帮助您确定快照代码是否有效