在Android中录制视频并捕获图像

时间:2012-09-14 10:02:22

标签: android image video record

我正在开发一个应用程序,我需要在我的应用程序中运行常量Camera 所以我有两件事要做, 1.捕获图像 2.录制视频

我在录制视频和停止并重置相机时遇到困难

以下是我的代码片段: -

import java.io.File;
import java.io.IOException;
import java.util.Date;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Menu;
import android.view.SurfaceView;
import android.widget.Toast;

public class Myvideo1 extends Activity  implements SurfaceHolder.Callback {

    @Override
    protected void onDestroy() {
     //   stopRecording();
        super.onDestroy();
    }

    private SurfaceHolder surfaceHolder;
    private SurfaceView surfaceView;
    public MediaRecorder mrec = new MediaRecorder();    
    private Camera mCamera;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        surfaceView = (SurfaceView) findViewById(R.id.videoview);
   //     if(mCamera==null)
    //        mCamera = Camera.open();

        // Please maintain sequence of following code. 

        // If you change sequence it will not work.

        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        try {
            startRecording();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {

        menu.add(0, 0, 0, "Start");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
   public boolean onOptionsItemSelected(MenuItem item)

    {
        if(item.getTitle().equals("Start"))
        {
            try {

                startRecording();
            //    item.setTitle("Stop");

            } catch (Exception e) {

                String message = e.getMessage();
                Log.i(null, "Problem " + message);
                mrec.release();
            }
        }

        else
        //  if(item.getTitle().equals("Stop"))
        {
          //  mrec.stop();
         //   mrec.release();
         //   mrec = null;
          //  item.setTitle("Start");
        }

        return super.onOptionsItemSelected(item);
    }

    protected void startRecording() throws IOException
    {
        if(mCamera==null)
            mCamera = Camera.open();

         String filename;
         String path;

         path= Environment.getExternalStorageDirectory().getAbsolutePath().toString();

         Date date=new Date();
         filename="/rec"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";

         //create empty file it must use
         File file=new File(path,filename);

        mrec = new MediaRecorder(); 

        mCamera.lock();
        mCamera.unlock();

        // Please maintain sequence of following code. 

        // If you change sequence it will not work.
        mrec.setCamera(mCamera);    
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setAudioSource(MediaRecorder.AudioSource.MIC);     
        mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mrec.setPreviewDisplay(surfaceHolder.getSurface());
        mrec.setOutputFile(path+filename);
        mrec.setMaxDuration(10000);     //10 sec



  //  }


    mrec.setOnInfoListener(new OnInfoListener() {   
        @Override  
        public void onInfo(MediaRecorder mr, int what, int extra) {  
            if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {   
                onStop();    

                Toast.makeText(getApplicationContext(), "video of 10 sec is completed", Toast.LENGTH_SHORT).show();

            }  
        }
    }
    ); 

    }


    public void preparerec()
    {
         try {
            mrec.prepare();
            mCamera.setPreviewDisplay(this.surfaceHolder);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void startrec()
    {
         mrec.start();
    }





    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {      

        Camera.Parameters parameters = mCamera.getParameters();      
        parameters.setPreviewSize(width, height);     
        mCamera.setParameters(parameters);       
        mCamera.startPreview(); 


    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {       

       /* if (mCamera != null) {
            Parameters params = mCamera.getParameters();
            mCamera.setParameters(params);

            Log.i("Surface", "Created");
        }
        else {
            Toast.makeText(getApplicationContext(), "Camera not available!",
                    Toast.LENGTH_LONG).show();

            finish();
        }*/


         try                 {      
             //Open the Camera in preview mode            
            mCamera = Camera.open(); 
            preparerec();
            //        
             }              
         catch(Exception ioe)          
             {          
             ioe.printStackTrace(System.out);          
             } 

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera.release();       

    }


    public void onStop(){
        mrec.stop();
        mrec.release();
        mrec = null;
     //   item.setTitle("Start");
    }


}

当我开始录制屏幕在我的三星Galaxy s2上闪烁和绿色时,对此有什么解决方案吗?

2 个答案:

答案 0 :(得分:0)

你在onCreate中创建一个相机对象(在startRecording中 - 它总是在surfaceCreated之前运行),然后在surfaceCreated中创建。只打开相机一次;第二次会抛出异常。

我建议当用户点击按钮时,将录制代码移动到所有运行状态,然后开始;你已经将设置工作分成了2-3个方法,这些方法都是在不同的时间触发的,你可以假设它们被调用的顺序很多。

如果你需要在应用程序启动时立即开始录制,我建议每次回调检查所有其他人是否完成,然后启动录制。

答案 1 :(得分:0)

This Solution对我来说非常适合。