java - 如何在使用表面 View 录制视频时拍照(快照)

标签 java android camera surfaceview image

我已完成使用表面 View 录制视频。现在我想知道“如何在使用表面 View 录制视频时拍照(快照)”??谁能帮帮我。我需要在我的相机按钮中添加什么才能拍照??

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>

最佳答案

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

};  

更改 Surface 管理器的宽度和高度

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

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

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

关于java - 如何在使用表面 View 录制视频时拍照(快照),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14229464/

相关文章:

java - EditText 输入类型文本密码未更改

java - 回收器 View : Changing background color of a Textview on each List Item

java - 打开相机后未调用 onActivityResult

C# 相机对象跟踪

适用于所有 Android 设备的相机应用程序

java - 从 Tomcat Servlet 中读取韩文字符

java - Bootstrap 3 DataTable导出到Excel不导出表数据

android - LinearLayout 中按钮之间的间距相等

java - Android 菜单在错误的地方膨胀

java - 如何在 LinearLayout 中将我的 ImageView 与我的 TextView 对齐?