ImageReader Android can't read any image

1.5k Views Asked by At

I'm trying to implement an ImageReader on my application, but i don't know why, he doesn't read anything.

List<Surface> surfaces = new ArrayList<Surface>();

        Surface previewSurface = new Surface(texture);
        previewRequestBuilder.addTarget(previewSurface);
        recordRequestBuilder.addTarget(previewSurface);
        surfaces.add(previewSurface);

        Surface recorderSurface = mediaRecorder.getSurface();
        surfaces.add(recorderSurface);

        ImageReader mImageReader = ImageReader.newInstance(previewSize.getWidth(),previewSize.getHeight(), ImageFormat.JPEG,5);
        Surface processSurface = mImageReader.getSurface();
        surfaces.add(processSurface);
        mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                Log.v("ImageReader ","An Image");
            }
        },null);

        cameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {

            @Override
            public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                captureSession = cameraCaptureSession;
                updateRequest(PREVIEW_REQUEST);
            }

            @Override
            public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                Activity activity = getActivity();
                if (null != activity) {
                    Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
                }
            }
        }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

So i got 3 Surface : previewSurface for the display, recordSurface for recording the video and processSurface to get the images (with ImageReader) and process it. But I don't even see my Log.v once !

Thanks by advance for your answers.

1

There are 1 best solutions below

0
On

There are at least 2 reasons why your code might not work:

  1. In your implementation of the OnImageAvailableListener, in the method onImageAvailable(ImageReader reader) you do not read and close the image. In my experience if you don't read/close the image from the reader the camera freezes. If this is the case then you should see the log message at least once (or even more times). I would suggest that you add reading and closing the image to the method:


    @Override
     public void onImageAvailable(ImageReader reader) {
         Log.v("ImageReader ","An Image");
         Image img = reader.acquireNextImage();
         img.close();
     }

  1. Taking 3 streams (for 3 surfaces) of a certain size and type might not be supported on your device. You should verify what support you have on your device (LEGACY/LIMITED/FULL). For instance, your device may not support 3 simultaneous streams of maximum size. Check the documentation. There are nice tables showing what is possible and carefully check if your sizes/types are ok.