如何打开相机然后切换到图像模式(反之亦然)

时间:2012-12-25 08:40:47

标签: android video camera photo switching

在我的情况下,我想拍照或拍摄视频,实际上如果我创建单独的意图,我可以这样做。我的意思是我可以打开相机作为图像模式或视频模式,但不能在它们之间切换。这与我使用的意图过滤器有关吗?我该怎么办?我如何在它们之间切换?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。在我想用相同的表面视图在同一个Activity中录制视频的按钮后,我做了一个表面视图和一个照相机的活动,但我不知道是否可能。好吧,我在Activity中编写了这个方法来准备de MediaRecorder并采用表面视图。

    public Boolean prepararCamaraVideo(){

    mMediaRecorder = new MediaRecorder();

    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    state = MediaRecorderState.INITIALIZED;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
        mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    else { 
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    }

    state = MediaRecorderState.DATA_SOURCE_CONFIGURED;

    mOutputFile = Files.getExternalMediaFile(Files.MEDIA_TYPE_VIDEO).toString();
    mMediaRecorder.setOutputFile(mOutputFile);

    mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d("Video", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;

    } catch (IOException e) {
        Log.d("Video", "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;

}

此命令mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder()。getSurface());获得摄像机的表面。

最后记录视频的方法。

    public void grabaVideo(View v) {
    if (state!=MediaRecorderState.RECORDING){
        if (prepararCamaraVideo()) {
            mMediaRecorder.start();
            state = MediaRecorderState.RECORDING;
            Toast.makeText(getApplicationContext(), getString(R.string.capturing_video), Toast.LENGTH_SHORT).show();                
        } else {
            // prepare didn't work, release the camera
            releaseMediaRecorder();
            // inform user
        }
    }
    else{
        mMediaRecorder.stop();  // stop the recording
        releaseMediaRecorder(); // release the MediaRecorder object
        mCamera.lock();         // take camera access back from MediaRecorder

        state = MediaRecorderState.INITIAL;
        Toast.makeText(getApplicationContext(), getString(R.string.video_stored_in) + " " + mOutputFile, Toast.LENGTH_SHORT).show();

    }

}

我希望能帮助你。