android camera2 api image reader
This tutorial describes setting up the android camera2 api image reader which provides the surface for capturing a still image.
Get Code
You can get it from here https://github.com/mobapptuts/recyclerview_image_gallery.git Tag camera2-imagereader
or else run this command
git clone —branch camera2-imagereader https://github.com/mobapptuts/recyclerview_image_gallery.git
Code Sampes
Create File, ImageReader, ImageReader.OnAvailableListener & ImageSaver members
private static File mImageFile; private ImageReader mImageReader; private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage())); } }; private static class ImageSaver implements Runnable { private final Image mImage; private ImageSaver(Image image) { mImage = image; } @Override public void run() { ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer(); byte[] bytes = new byte[byteBuffer.remaining()]; byteBuffer.get(bytes); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(mImageFile); fileOutputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { mImage.close(); if(fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
In the takePhoto method assign a new File to the Activity file.
public void takePhoto(View view) { try { mImageFile = createImageFile(); } catch (IOException e) { e.printStackTrace(); } lockFocus(); }
In the setupCamera method calculate the size of the largest image the camera can capture. Then create a new instance for the ImageReader using the calculated size, then enable the ImageReader’s setOnImageAvailableListener callback.
private void setupCamera(int width, int height) { CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { for(String cameraId : cameraManager.getCameraIdList()) { CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId); if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT){ continue; } StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); Size largestImageSize = Collections.max( Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new Comparator<Size>() { @Override public int compare(Size lhs, Size rhs) { return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getWidth() * rhs.getHeight()); } } ); mImageReader = ImageReader.newInstance(largestImageSize.getWidth(), largestImageSize.getHeight(), ImageFormat.JPEG, 1); mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler); mPreviewSize = getPreferredPreviewSize(map.getOutputSizes(SurfaceTexture.class), width, height); mCameraId = cameraId; return; } } catch (CameraAccessException e) { e.printStackTrace(); } }