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

前端 未结 1 580
孤独总比滥情好
孤独总比滥情好 2021-01-26 01:29

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 b

相关标签:
1条回答
  • 2021-01-26 01:59

    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.

    0 讨论(0)
提交回复
热议问题