Has anyone managed to obtain a YUV_420_888 frame using RenderScript and the new Camera API?

13,409

Solution 1

The Android sample app HdrViewfinderDemo uses RenderScript to process YUV data from camera2.

https://github.com/googlesamples/android-HdrViewfinder

Specifically, the ViewfinderProcessor sets up the Allocations, and hdr_merge.rs reads from them.

Solution 2

Yes I did it, since I couldn't find anything useful. But I didn't go the proposed way of defining an allocation to the surface. Instead I just converted the output of the three image planes to RGB. The reason for this approach is that I use the YUV420_888 data twofold. First on a high frequency basis just the intensity values (Y). Second, I need to make some color Bitmaps too. Thus, the following solution. The script takes about 80ms for a 1280x720 YUV_420_888 image, maybe not ultra fast, but ok for my purpose.

UPDATE: I deleted the code here, since I wrote a more general solution here YUV_420_888 -> Bitmap conversion that takes into account pixelStride and rowStride too.

Solution 3

I think that you can use an ImageReader to get the frames of you camera into YUV_420_888

reader = ImageReader.newInstance(previewSize.getWidth(), previewSize.getHeight(), ImageFormat.YUV_420_888, 2);

Then you set an OnImageAvailableListener to the reader :

 reader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
             int jump = 4; //Le nombre d'image à sauter avant d'en traiter une, pour liberer de la mémoire
             Image readImage = reader.acquireNextImage();

             readImage.getPlane[0] // The Y plane
             readImage.getPlane[1] //The U plane
             readImage.getPlane[2] //The V plane

             readImage.close();
            }
        }, null);

Hope that will help you

Share:
13,409
Bogdan Chende
Author by

Bogdan Chende

Beginner Java developer

Updated on July 20, 2022

Comments

  • Bogdan Chende
    Bogdan Chende almost 2 years

    I'm using RenderScript and Allocation to obtain YUV_420_888 frames from the Android Camera2 API, but once I copy the byte[] from the Allocation I receive only the Y plane from the 3 planes which compose the frame, while the U and V planes values are set to 0 in the byte[]. I'm trying to mimic the onPreviewframe from the previos camera API in order to perform in app processing of the camera frames. My Allocation is created like:

    Type.Builder yuvTypeBuilderIn = new Type.Builder(rs, Element.YUV(rs));
        yuvTypeBuilderIn.setX(dimensions.getWidth());
        yuvTypeBuilderIn.setY(dimensions.getHeight());
        yuvTypeBuilderIn.setYuvFormat(ImageFormat.YUV_420_888);
        allocation = Allocation.createTyped(rs, yuvTypeBuilderIn.create(),
                Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);
    

    while my script looks like:

    #pragma version(1)
    #pragma rs java_package_name(my_package)
    #pragma rs_fp_relaxed
    
    rs_allocation my_frame;
    
  • Bogdan Chende
    Bogdan Chende almost 9 years
    Hi. I already tried your sollution. It was one of the first things I tried but after receiving about 50 frames it stops and throws some error.