2

This is how I instantiate the ImageReader.

Size[] sizes = configs.getOutputSizes(ImageFormat.YUV_420_888);
        mImageReader = ImageReader.newInstance(width, height, ImageFormat.YUV_420_888, 2);

        mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null);

        Surface rgbCaptureSurface = mImageReader.getSurface();
        List<Surface> surfaces = new ArrayList<Surface>();
        surfaces.add(rgbCaptureSurface);

        //surfaces.add(surface);

        mPreviewRequestBuilder
                = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        //mPreviewRequestBuilder.addTarget(surface);

        mPreviewRequestBuilder.addTarget(rgbCaptureSurface);

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


            @Override
            public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                // The camera is already closed
                if (null == mCameraDevice) {
                    return;
                }

                // When the session is ready, we start displaying the preview.
                mCaptureSession = cameraCaptureSession;
                try {
                    // Auto focus should be continuous for camera preview.
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                            CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO);

                    // Flash is automatically enabled when necessary.
                    //setAutoFlash(mPreviewRequestBuilder);

                    // Finally, we start displaying the camera preview.
                    mPreviewRequest = mPreviewRequestBuilder.build();
                    mCaptureSession.setRepeatingRequest(mPreviewRequest,
                            mCaptureCallback, null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

Reading is done like this:

  public void onImageAvailable(ImageReader reader) {
        Image image;
        while (true) {
            image = reader.acquireLatestImage();

            if (image == null) return;


            Image.Plane Y = image.getPlanes()[0];
            Image.Plane U = image.getPlanes()[1];
            Image.Plane V = image.getPlanes()[2];

            int Yb = Y.getBuffer().remaining();
            int Ub = U.getBuffer().remaining();
            int Vb = V.getBuffer().remaining();

            byte[] data = new byte[Yb + Ub + Vb];


            Y.getBuffer().get(data, 0, Yb);
            U.getBuffer().get(data, Yb, Ub);
            V.getBuffer().get(data, Yb + Ub, Vb);

I tried several different ImageFormats. I'm testing on LG G3, API 21 and the problem occurs.On Nexus 4 I do not have the problem, API 22.

Lyubomir Dinchev
  • 320
  • 3
  • 12

2 Answers2

1

I upgraded to API 23 and the same code worked fine. Also tested on API 22 and it also worked.

Same as : Using Camera2 API with ImageReader

Community
  • 1
  • 1
Lyubomir Dinchev
  • 320
  • 3
  • 12
0

Your observation is correct. API 21 does not properly support Camera2. This has been found by several people independently here on SO, see e.g. Camera2 API21 not working

So it is reasonable to start using Camera2 not before API22. It is not understandable why documentation hasn't been amended in the meantime.

Personally I am continuing to perform Camera2 studies, but I am still reluctant to use Camera2 in my app now. I first want to test it on many many devices first and for the near future I don't expect "Camera1" not being supported anymore by new devices.

Community
  • 1
  • 1
Settembrini
  • 1,338
  • 1
  • 19
  • 32
  • Do you have any observation on how to fix the orientation of the image. I have this problem also : http://stackoverflow.com/questions/28845245/image-data-from-android-camera2-api-flipped-squished-on-galaxy-s5 – Lyubomir Dinchev Jul 13 '16 at 08:50
  • I have solved the the conversion YUV420_888 to RGB, see here http://stackoverflow.com/q/36212904/5148048. I am confident this works for many or even all devices. You need to set-up RenderScript, which is not so difficult. – Settembrini Jul 13 '16 at 11:55
  • Does the skew problem persist with render script? – Lyubomir Dinchev Jul 14 '16 at 09:44
  • No, the pictures are perfect. If you haven't set-up RenderScript here is a short manual: http://stackoverflow.com/a/31843987/5148048. In point 12 you take the rs-Code from the link above, put it into an empty file within the rs-Folder and name that file yuv420888.rs. – Settembrini Jul 14 '16 at 14:33