How to implement video recording using CameraX?

811 views
Skip to first unread message

Brandon Palacios Zamudio

unread,
Jul 1, 2020, 5:45:32 AM7/1/20
to Android CameraX Discussion Group
Hello colleagues, I already have CameraX configured to capture photos using imageCapture and it works well for me, now I want to add videoCapture to record videos but I don't know how to configure that, can someone give me some guidance please.

Edson Rocha

unread,
Jul 1, 2020, 7:03:02 PM7/1/20
to Android CameraX Discussion Group, sdc.brando...@gmail.com
Hi, this is my code that records a video for 10 seconds. Remember that if you are taking a photo and also recording video, you have to initialise the camera when changing from video to photo and vice versa.

 Preview preview = new Preview.Builder()
.setTargetResolution(new Size(1280, 720))
.build();

cameraProvider.unbindAll();
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
.setTargetResolution(new Size(1280, 720))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();
imageAnalysis.setAnalyzer(cameraExecutor, image -> {
rotationDegrees = image.getImageInfo().getRotationDegrees();
});
videoCapture = new VideoCaptureConfig.Builder()
.setTargetResolution(new Size(480, 272))
.build();
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, preview, videoCapture);
}

private void takePhoto() {

imageFileName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = new File(file_path + "VID_" + imageFileName + ".mp4");

// CountDownTimer starts with 10 seconds and every onTick is 1 second
final int tenSec = 11 * 1000; // 1 minute in milli seconds
new CountDownTimer(tenSec, 1000) {
public void onTick(long millisUntilFinished) {
}

public void onFinish() {
videoCapture.stopRecording();
}
}.start();

videoCapture.startRecording(file, cameraExecutor, new VideoCapture.OnVideoSavedCallback() {
@Override
public void onVideoSaved(@NonNull File file) {
System.out.println("****** Saving Video *****");
});
}

@Override
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
}
});

}
}

Reply all
Reply to author
Forward
0 new messages