2013-01-09 96 views
4

我已完成使用表面視圖錄制視頻的工作。現在我想知道「如何在使用曲面視圖錄制視頻時拍攝照片(快照)」?任何人都可以幫助我。我需要添加什麼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

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聽者

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

}; 

更改您的表面經理widht和HEIGH

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

,並在您添加android:background="#FF0000"線性佈局

它會幫助你弄清楚你的快照代碼的工作與否

+0

你能否請電話確切的變化,我需要修改我的上述編輯的代碼。謝謝 – Aruva

+0

可以發表您的XML代碼以及 – Zohaib

+0

我發佈了我的XML文件。請檢查 – Aruva

相關問題