从视频中获取位图帧

时间:2012-09-11 10:09:01

标签: android video bitmap video-capture sd-card

我已经编写了捕获视频的代码,现在我想要从那个方法来获取位图帧。这里我正在获取实时视频,我想要捕获位图帧。如何获取帧?

public class FdActivity extends Activity {
                private Camera myCamera;
                private MyCameraSurfaceView myCameraSurfaceView;
                private MediaRecorder mediaRecorder;
                private VideoCapture        mCamera;
                Button myButton;
                SurfaceHolder surfaceHolder;
                boolean recording;
                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    recording = false;
                    setContentView(R.layout.main);
                    //Get Camera for preview
                    myCamera = getCameraInstance();
                    if(myCamera == null){
                        Toast.makeText(FdActivity.this, 
                                "Fail to get Camera", 
                                Toast.LENGTH_LONG).show();
                    }
                    myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
                    FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
                    myCameraPreview.addView(myCameraSurfaceView);
                    myButton = (Button)findViewById(R.id.mybutton);
                    myButton.setOnClickListener(myButtonOnClickListener);
                }
                Button.OnClickListener myButtonOnClickListener
                = new Button.OnClickListener(){
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if(recording){
                              mediaRecorder.stop();  // stop the recording
                            releaseMediaRecorder(); // release the MediaRecorder object
                              finish();
                        }else{
                                releaseCamera();
                            mediaRecorder.start();
                            recording = true;
                            myButton.setText("STOP");
                        }
                    }};

                private Camera getCameraInstance(){
                    // TODO Auto-generated method stub
                    Camera c = null;
                    try {
                        c = Camera.open(); // attempt to get a Camera instance
                    }
                    catch (Exception e){
                    }
                    return c; // returns null if camera is unavailable
                }
                private boolean prepareMediaRecorder(){
                    myCamera = getCameraInstance();
                    mediaRecorder = new MediaRecorder();
                    myCamera.unlock();
                    mediaRecorder.setCamera(myCamera);
                    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
                    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
                    mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
                    mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
                    mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
                    mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
                    mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
                    try {
                        mediaRecorder.prepare();
                    } catch (IllegalStateException e) {
                        releaseMediaRecorder();
                        return false;
                    } catch (IOException e) {
                        releaseMediaRecorder();
                        return false;
                    }
                    return true;
                }
                @Override
                protected void onPause() {
                    super.onPause();
                    releaseMediaRecorder();       // if you are using MediaRecorder, release it first
                    releaseCamera();              // release the camera immediately on pause event
                }
                private void releaseMediaRecorder(){
                    if (mediaRecorder != null) {
                        mediaRecorder.reset();   // clear recorder configuration
                        mediaRecorder.release(); // release the recorder object
                        mediaRecorder = null;
                        myCamera.lock();           // lock camera for later use
                    }
                }
                private void releaseCamera(){
                    if (myCamera != null){
                        myCamera.release();        // release the camera for other applications
                        myCamera = null;
                    }
                }
                public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
                    private SurfaceHolder mHolder;
                    private Camera mCamera;
                    public MyCameraSurfaceView(Context context, Camera camera) 
                    {
                        super(context);
                        mCamera = camera;
                        mHolder = getHolder();
                        mHolder.addCallback(this);
                        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                    }
                    public void surfaceChanged(SurfaceHolder holder, int format, int weight,
                            int height) 
                    {
                        if (mHolder.getSurface() == null){
                          return;
                        }
                        try {
                            mCamera.stopPreview();
                        } catch (Exception e){
                        }
                        try {
                            mCamera.setPreviewDisplay(mHolder);
                            mCamera.startPreview();
                        } catch (Exception e){
                        }
                    }
                    public void surfaceCreated(SurfaceHolder holder) {
                        try {
                            int offset = 0;
                            int stride = 0;
                            int width = 0;
                            int y = 0;
                            int x = 0;
                            int[][] pixels = null;
                            Bitmap src = null;
                            int height = 0;
                            mCamera.setPreviewDisplay(holder);
                            mCamera.startPreview();
                        } catch (IOException e) {
                        }
                    }
                    public void surfaceDestroyed(SurfaceHolder holder) {
                    }
                }
            }

0 个答案:

没有答案