jpegandroid-camera2yuvimage-reader

In Android Camera2 API. Can I use a image reader (YUV_420_888) for processing and another(JPEG) for capture still image?


I try but preview freeze when I press the capture button and no image created. Any suggestion ? How many imagereader can I use in one Camera2 project?


Solution

  • For every CaptureSession you are creating,you can only have one ImageReader. If you want to use two different ImageReader for each type of Image then you should use two CaptureSession. But you have to be very careful about the handling of camera resources between the two Sessions(ex. you should close ImageReader for JPEG before starting other CaptureSession for capturing YUV_420_888 image). Using multiple CaptureSession is also heavy on the device and generally not recommended. Instead you can use same bytes for both type of image.

    public void onImageAvailable(ImageReader imageReader) {
                byte[] bytes = null;
                Image image = imageReader.acquireLatestImage();
                try {
    
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    bytes = new byte[buffer.capacity()];
                    buffer.get(bytes);
                    //use the bytes to manipulate
                } catch (Exception e) {
                    e.printStackTrace();
                }
                image.close();
                imageReader.close();
    
            }