Mick
Mick

Reputation: 7927

YUV_NV21_TO_RGB not working?

I am staring to develop an app which monitors the camera preview, and does some image processing on it and displays int on a canvas. Just as a diagnostic I have the following code:

camera = Camera.open();

ImageFormat imf = new ImageFormat();
Camera.Parameters param = camera.getParameters();
param.setPreviewSize(128, 128);
preview_format = param.getPreviewFormat();
Camera.Size sz = param.getPreviewSize();

myimage = new int[sz.width*sz.height];

At run time it reports that preview_format is 17 which I understand is "NV21".

Later I have:

    camera.setPreviewCallback(new PreviewCallback()
    {    
          public void onPreviewFrame(byte[] _data, Camera _camera)
          {
              YUV_NV21_TO_RGB(myimage , _data, 128, 128) ;
          }
    });

The function YUV_NV21_TO_RGB was taken from here.

Meanwhile in another thread I have:

    canvas.drawBitmap(
            myimage,        // the int array
            0,              // where to start in the array
            128,            // the stride ???
            200,            // x coord of where to display
            200,            // y coord of where to display
            128,            // wid
            128,            // ht
            false,          // alpha used?
            null);          // the paint used

The resulting image can be seen amongst other diagnostics in the square below. The stripes change as I move the phone around and appear to in some way correspond to what the camera is pointing at, but clearly it has been mangled. I tried using an alternative function found here, and another from wikipedia, but with seemingly identical results. Any ideas?

EDIT: One thought I had was that perhaps NV21 may not completely specify the format - maybe its a class of formats, where you need to go on and specify the bits per pixel or similar.

EDIT: An extra clue - if I cover the camera completely, the square goes entirely pure green.

Output

Upvotes: 1

Views: 307

Answers (1)

Codo
Codo

Reputation: 78815

Your preview size is not 128 by 128 because you fail to set it. You set it on the Camera.Parameters instance but you don't apply it to the camera.

You need to add the following line:

camera.setParameters(param);

And it's probably safe to get the parameters directly from the Camera instance:

preview_format = camera.getPreviewFormat();
Camera.Size sz = camera.getPreviewSize();

Upvotes: 1

Related Questions