How to programmatically take Photos while recording Video using Camera2 API in Android

前端 未结 6 1789
北恋
北恋 2020-12-24 09:41

I want to capture image while recording video using camera2 API.

Two separate demos are available. 1. To capture image and 2. To record video

I tried to com

6条回答
  •  孤独总比滥情好
    2020-12-24 10:11

      public void onClick(View v) {
        // TODO Auto-generated method stub
    
        // if(v == myButton)
        // {
        if (recording) {
            // stop recording and release camera
            // type.setVisibility(View.GONE);1
            type.setText("");
            myChronometer.stop();
            myChronometer.setBase(SystemClock.elapsedRealtime());
            // mediaRecorder.stop(); // stop the recording
            releaseMediaRecorder(); // release the MediaRecorder object
            //
            myCamera.lock();
            recording = false;
    
            // "/sdcard/fivo_flim/" + "fivo_flim_"+video_no
            // + ".mp4";
    
    
            System.out.println("11111111111   "+SessionManager.getVideoCount(prefs1));
    
            new UploadPicture(VideoScreen.this, mApi, "/", new File(
                    "/sdcard/record/" + "record"
                            + SessionManager.getVideoCount(prefs1) + ".mp4"))
                    .execute();
            SessionManager.saveVideoCount(prefs1,
                    SessionManager.getVideoCount(prefs1) + 1);
    
            mWakeLock.release();
            System.out.println("uuuuuuuuuuuuuuuuuu");
        } else {
            // Release Camera before MediaRecorder start
            releaseCamera();
            mWakeLock.acquire();
    
            if (!prepareMediaRecorder()) {
                Toast.makeText(
                        VideoScreen.this,
                        "Fail in PrepareCamera()!\n         \n   Please Insert SD Card or\n   Restart your phone ",
                        Toast.LENGTH_LONG).show();
                finish();
            }
            System.out.println("prepare media recorder");
            try {
                mediaRecorder.prepare();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println("Starting Media Recorder");
            try {
                mediaRecorder.start();
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("exception is " + e);
            }
            type.setText("Recording...");
    
            myChronometer.setBase(SystemClock.elapsedRealtime());
            myChronometer.start();
    
            recording = true;
            myButton.setClickable(false);
            try {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        myButton.setClickable(true);
                    }
                }, 3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
    @SuppressLint({ "NewApi", "SdCardPath" })
    private boolean prepareMediaRecorder() {
        myCamera = getCameraInstance();
        mediaRecorder = new MediaRecorder();
        myCamera.setDisplayOrientation(90);
        myCamera.unlock();
        mediaRecorder.setCamera(myCamera);
        mediaRecorder.setOrientationHint(90);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    
        mediaRecorder.setProfile(CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH));
    
        File mediaStorageDir = new File("/sdcard/record/");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Logger.Debug("failed to create directory");
            }
        }
    
    
        video_no=SessionManager.getVideoCount(prefs1);
    
    
        mFiles.add("/sdcard/record/" + "record" + video_no
                + ".mp4");
        mediaRecorder.setOutputFile("/sdcard/record/" + "record"
                + video_no + ".mp4");
        video_no++;
    
        mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder()
                .getSurface());
    
        try {
            mediaRecorder.prepare();
    
        } catch (IllegalStateException e) {
            releaseMediaRecorder();
            return false;
    
        } catch (IOException e) {
            releaseMediaRecorder();
            return false;
        }
    
        return true;
    
    }
    
    @SuppressLint("SdCardPath")
    private void releaseMediaRecorder() {
        if (mediaRecorder != null) {
            mediaRecorder.reset();
            mediaRecorder.release();
            mediaRecorder = null;
            myCamera.lock();
    
        }
    }
    
    private void releaseCamera() {
        if (myCamera != null) {
            myCamera.release();
            myCamera = null;
        }
    
    }
    
    }
    

提交回复
热议问题