Mill3r
Mill3r

Reputation: 544

Display the YUV value from live camera preview Android

This question has been asked many times already but i cannot seem to find what i want exactly. I am trying to create a camera app where I want to display the YUV or RGB value into the log when I point my camera to some color. The values must me 0...255 range for RGB or correspondent YUV color format. I can manage the conversion between them as there are many such examples on stack overflow. However, i cannot store the values into 3 separate variables and display them in the log.

so far i have managed to get

package com.example.virus.bpreader;

public class CaptureVideo extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {

private SurfaceHolder mHolder;
private Camera mCamera;
private int[] pixels;

public CaptureVideo(Context context, Camera cameraManager) {
    super(context);

    mCamera = cameraManager;
    mCamera.setDisplayOrientation(90);

    //get holder and set the class as callback
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    //when the surface is created, let the camera start the preview
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
        mCamera.cancelAutoFocus();

        Camera.Parameters params = mCamera.getParameters();
        //get fps
        params.getSupportedPreviewFpsRange();
        //get resolution
        params.getSupportedPreviewSizes();
        //stop auto exposure
        params.setAutoExposureLock(false);

        // Check what resolutions are supported by your camera
        List<Camera.Size> sizes = params.getSupportedPictureSizes();
        // Iterate through all available resolutions and choose one
        for (Camera.Size size : sizes) {
            Log.i("Resolution", "Available resolution: " + size.width + " " + size.height);
        }
        //set resolution at 320*240
        params.setPreviewSize(320,240);

        //set frame rate at 10 fps
        List<int[]> frameRates = params.getSupportedPreviewFpsRange();
        int last = frameRates.size() - 1;
        params.setPreviewFpsRange(10000, 10000);
        //set Image Format
        //params.setPreviewFormat(ImageFormat.NV21);
        mCamera.setParameters(params);


    } catch (IOException e) {
        e.printStackTrace();
    }


}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    //need to stop the preview and restart on orientation change
    if (mHolder.getSurface() == null) {
        return;
    }
    //stop preview
    mCamera.stopPreview();

    //start again
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    //stop and release
    mCamera.startPreview();
    mCamera.release();
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {

    int frameHeight = camera.getParameters().getPreviewSize().height;
    int frameWidth = camera.getParameters().getPreviewSize().width;
    // number of pixels//transforms NV21 pixel data into RGB pixels
    int rgb[] = new int[frameWidth * frameHeight];
    // convertion
    int[] myPixels = decodeYUV420SP(rgb, data, frameWidth, frameHeight);

    Log.d("myPixel", String.valueOf(myPixels.length));

}

//yuv decode
int[] decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {

    final int frameSize = width * height;
    int r, g, b, y1192, y, i, uvp, u, v;
    for (int j = 0, yp = 0; j < height; j++) {
        uvp = frameSize + (j >> 1) * width;
        u = 0;
        v = 0;
        for (i = 0; i < width; i++, yp++) {
            y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0)
                y = 0;
            if ((i & 1) == 0) {
                // above answer is wrong at the following lines. just swap ***u*** and ***v***
                u = (0xff & yuv420sp[uvp++]) - 128;
                v = (0xff & yuv420sp[uvp++]) - 128;
            }

            y1192 = 1192 * y;
            r = (y1192 + 1634 * v);
            g = (y1192 - 833 * v - 400 * u);
            b = (y1192 + 2066 * u);

            r = Math.max(0, Math.min(r, 262143));
            g = Math.max(0, Math.min(g, 262143));
            b = Math.max(0, Math.min(b, 262143));

            // combine RGB
            rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) | 0xff);
        }
    }
return rgb;
}
}

Here the decode method should give a hex format of RGB color space (quite sure its all right as most of the answers are the same). The problem i am facing is when i am not quite sure how to call it inside the OnPreviewFrame method so that it displays the RGB values separately into the log.

N.B like i said i have seen lot of similar questions but could not find a solution to it. I do not want to store the file (image/video) as i only need the RGB/YUV values from the live camera preview when i point the camera to some color.

I need the rgb or yuv values because i want to plot a graph out of it against time.

Any help will be much appreciated.

Upvotes: 0

Views: 1015

Answers (1)

MadScientist
MadScientist

Reputation: 2174

Well if the problem is to get separate values of R, G And B from the RGB array, check this SO post here.

Hope it helps!

Upvotes: 0

Related Questions