In this tutorial, we will implement CameraX using Java. The purpose of this post is to provide an updated implementation, as my previous tutorial used the Alpha version of CameraX APIs, which are now outdated.
Watch the Video Tutorial
If you prefer a step-by-step walkthrough, watch the video tutorial below:
1. Setting Up the Project
First, create a new project in Android Studio. In the activity_main.xml
layout, replace the default TextView
with a PreviewView
for camera preview.
Adding Dependencies
Include the following dependencies in your build.gradle
file:
implementation "androidx.camera:camera-core:1.3.0"
implementation "androidx.camera:camera-camera2:1.3.0"
implementation "androidx.camera:camera-lifecycle:1.3.0"
implementation "androidx.camera:camera-view:1.3.0"
After adding these dependencies, sync your project.
2. Updating the Layout
Modify activity_main.xml
to include a PreviewView
for displaying the camera preview and buttons for capturing images and videos.
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/capturePhoto"
android:text="Capture Photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/captureVideo"
android:text="Capture Video"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
3. Implementing CameraX
Before writing CameraX logic, add the following permissions to AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Getting Camera Provider
First, obtain the Camera Provider in MainActivity.java
:
ProcessCameraProvider cameraProvider =
ProcessCameraProvider.getInstance(this).get();
Starting CameraX
Initialize CameraX and bind it to the lifecycle:
private void startCameraX(ProcessCameraProvider cameraProvider) {
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
Preview preview = new Preview.Builder().build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
cameraProvider.bindToLifecycle(this, cameraSelector, preview);
}
4. Capturing Images
Implement the image capture functionality:
ImageCapture imageCapture = new ImageCapture.Builder().build();
capturePhoto.setOnClickListener(view -> {
File file = new File(getExternalFilesDir(null), "photo.jpg");
ImageCapture.OutputFileOptions options =
new ImageCapture.OutputFileOptions.Builder(file).build();
imageCapture.takePicture(options, executor, new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults output) {
Toast.makeText(getApplicationContext(), "Photo Captured", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
exception.printStackTrace();
}
});
});
5. Capturing Videos
To record videos, use the new CameraX Recorder
API:
VideoCapture videoCapture = new VideoCapture.Builder().build();
captureVideo.setOnClickListener(view -> {
if (recording == null) {
File file = new File(getExternalFilesDir(null), "video.mp4");
VideoCapture.OutputFileOptions options =
new VideoCapture.OutputFileOptions.Builder(file).build();
recording = videoCapture.getOutput()
.prepareRecording(getApplicationContext(), options)
.start(ContextCompat.getMainExecutor(this), event -> {
if (event instanceof VideoRecordEvent.Start) {
captureVideo.setText("Stop Recording");
} else if (event instanceof VideoRecordEvent.Finalize) {
captureVideo.setText("Capture Video");
recording = null;
Toast.makeText(this, "Video Saved", Toast.LENGTH_SHORT).show();
}
});
} else {
recording.stop();
recording = null;
}
});
6. Running the Application
After implementing the above code, run the application. You should be able to preview the camera, capture images, and record videos. Ensure you have granted camera and storage permissions in the app settings.
Conclusion
In this tutorial, we implemented CameraX in Java, covering preview, image capture, and video recording using the latest APIs. If you found this guide helpful, feel free to check out more tutorials on our channel!
No comments:
Post a Comment
Share your thoughts ...