How to open camera then switch to Image mode (vice versa)

那年仲夏 提交于 2019-12-20 07:35:45

问题


In my case I want to take photo or capture video, actually I can do these if I create separate intents. I mean I can open camera as image mode or video mode but can not switch between them. Is this related to intent filters which I use? What should I do? How do I switch between them?


回答1:


I had the same problem. I did a surface view and an Activity for a photo camera after I thought put a button to record a video in the same Activity with the same surface view but I don´t know if it was possible. Well, I wrote this method in the Activity to prepare de MediaRecorder and take the surface view.

    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;

}

This command mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface()); get the surface for the Video Camera.

Finally the method to Record the video.

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

    }

}

I hope to help you.



来源:https://stackoverflow.com/questions/14029057/how-to-open-camera-then-switch-to-image-mode-vice-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!