0

I am trying to use android camera2 for face/eye detection. My problem is that there isn't much documentation on it and some functions do not work as I expect them to. I need to process every frame sent from the camera.

I am trying to use the captureRequestBuilder.addTarget() to send a frame to the imageReader.OnAvailableImage() but when I do that, I no longer get frames sent to the preview session.

This piece of code breaks my phone preview of the camera.

        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(imageSize.getWidth(),imageSize.getHeight());
        Surface surface = new Surface(texture);
        ArrayList<Surface> surfaces = new ArrayList<>();
        surfaces.add(surface);
        surfaces.add(imageReader.getSurface());
        captureRequestBuilder = cameraDevice.createCaptureRequest(cameraDevice.TEMPLATE_PREVIEW);
        captureRequestBuilder.addTarget(surface);
        captureRequestBuilder.addTarget(imageReader.getSurface());
        cameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {

But if I do it like this the preview works, but I can't find any way to process the frames

        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(imageSize.getWidth(),imageSize.getHeight());
        Surface surface = new Surface(texture);
        captureRequestBuilder = cameraDevice.createCaptureRequest(cameraDevice.TEMPLATE_PREVIEW);
        captureRequestBuilder.addTarget(surface);
        cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {

this is the ImageReader

ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1);
        List<Surface> outputSurface = new ArrayList<>(2);
        outputSurface.add(reader.getSurface());
        outputSurface.add(new Surface(textureView.getSurfaceTexture()));

        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                Image image = null;
                try
                {
                    image = reader.acquireLatestImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.capacity()];
                    buffer.get(bytes);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                finally {
                    {
                        if (image != null)
                            image.close();
                    }
                }
            }
        };
        reader.setOnImageAvailableListener(readerListener,mBackgroundHandler);

Can anybody help me with real time processing of Camera2 frames?

  • The code snippet looks fine, but it's only a part of the program. What size of ImageReader are you trying to use, and what format? How do you set the capture request to be captured by the camera? And can you provide system logs from a run of the program, to see what logging may be coming from the camera implementation? – Eddy Talvala Jan 24 '19 at 18:04
  • I added the imageReader – JUSTINIAN-IONUT PETCU Jan 24 '19 at 18:12
  • Nothing obviously wrong there either, though not sure what the outputSurface is for. You could take a look at Camera2Video sample and try to modify it for your uses? – Eddy Talvala Jan 24 '19 at 23:37

0 Answers0