Android Camera2 API Raw Setup
This android tutorial series is about explaining how to do the android camera2 api raw setup. Where the android application will be capturing raw images.
Get Code
This code has been based on the android camera2 api app tutorial series that can be found here
The code to start this tutorial is on github here
https://github.com/mobapptuts/recyclerview_image_gallery.git Tag camera2-write-swap-fix
or you can run this command
git clone –branch camera2-write-swap-fix https://github.com/mobapptuts/recyclerview_image_gallery.git
The code changes will be provided in part 2 of this android tutorial series.
Code Samples
Add helper methods for comparing image size & array mode check
Helper method jpeg & raw image size comparisons
private static class CompareSizeByArea implements Comparator<Size> { @Override public int compare(Size lhs, Size rhs) { return Long.signum((long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight()); } }
Helper method used to check if a camera mode is supported or not
private static Boolean contains(int[] modes, int mode) { if(modes == null) { return false; } for(int i : modes) { if(i == mode) { return true; } } return false; }
Creating folders & files for Raw
Create an activity member for the raw folder
private File mRawGalleryFolder;
Create a file for the captured raw image
private static File mRawImageFile;
Create separate folders for both the jpeg and raw images
private void createImageGallery() { File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); mGalleryFolder = new File(storageDirectory, "JPEG Images"); mRawGalleryFolder = new File(storageDirectory, "Raw Images"); if(!mGalleryFolder.exists()) { mGalleryFolder.mkdirs(); } if(!mRawGalleryFolder.exists()) { mRawGalleryFolder.mkdirs(); } }
Add a method for creating the file for the raw image
File createRawImageFile() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "RAW_" + timeStamp + "_"; File image = File.createTempFile(imageFileName, ".dng", mRawGalleryFolder); mImageFileLocation = image.getAbsolutePath(); return image; }
CameraCharacteristics setup
Create an activity member for camera characteristics
private CameraCharacteristics mCameraCharacteristics;
Initialise the camera characteristics member inside the setupCamera method
mCameraCharacteristics = cameraCharacteristics;
Create Image Reader & image available listener for capturing raw
Initial configuration inside the setupCamera method
Check the camera device supports raw
if(!contains(cameraCharacteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES), CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_RAW)) { continue; }
Find the largest raw image size
Size largestRawImageSize = Collections.max( Arrays.asList(map.getOutputSizes(ImageFormat.RAW_SENSOR)), new CompareSizeByArea());
Create a new instance of the image reader & image available listener for the raw images.
mRawImageReader = ImageReader.newInstance(largestRawImageSize.getWidth(), largestRawImageSize.getHeight(), ImageFormat.RAW_SENSOR, 1); mRawImageReader.setOnImageAvailableListener(mOnRawImageAvailableListener, mBackgroundHandler);
Add the raw image reader surface to the create capture session inside the cameraCreate PreviewSession method
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface(), mRawImageReader.getSurface()),
That’s the end of the setup portion of the android camera2 api raw application select here for the capture portion of the android tutorial.