如何在使用表面视图录制视频时拍摄照片(快照)

时间:2013-01-09 06:28:02

标签: java android camera surfaceview image

我完成了使用表面视图录制视频。现在我想知道“使用表面视图录制视频时如何拍照(快照)”?请有人帮帮我。我需要在 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: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" />

<SurfaceView
    android:id="@+id/preview"
    android:layout_width="500px"
    android:layout_height="500px"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="34dp" />
   <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>

1 个答案:

答案 0 :(得分:0)

RecordingView link&amp; link。您可以通过此code拍摄快照 如果你的xml中有一个videoView,那么你需要将linearLayout作为其父级(videoView作为linearLayout的子级),当你保存linearlayout图像时,这意味着你将保存你的视频图像。

试试这个并阅读我发送的代码以拍摄快照。

  <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);
    }

};  

更改Surface管理器并高度

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

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

它可以帮助您弄清楚您的快照代码是否有效

相关问题