显示实时相机预览Android

时间:2017-09-28 09:57:34

标签: android image-processing colors camera

这个问题已被多次询问,但我似乎无法找到我想要的东西。 我正在尝试创建一个相机应用程序,当我将相机指向某种颜色时,我想在日志中显示YUV或RGB值。对于RGB或对应的YUV颜色格式,值必须为0 ... 255范围。我可以管理它们之间的转换,因为堆栈溢出有很多这样的例子。但是,我无法将值存储到3个单独的变量中,并将它们显示在日志中。

到目前为止,我已设法获得

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;
}
}

这里的解码方法应该给出RGB颜色空间的十六进制格式(非常确定它可以,因为大多数答案是相同的)。我面临的问题是当我不太确定如何在OnPreviewFrame方法中调用它时,它将RGB值分别显示在日志中。

N.B就像我说我见过很多类似的问题,却无法找到解决方法。我不想存储文件(图像/视频),因为当我将相机指向某种颜色时,我只需要实时相机预览中的RGB / YUV值。

我需要rgb或yuv值,因为我想根据时间绘制图形。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果问题是从RGB数组中获取R,G和B的单独值,请检查此SO帖子here

希望它有所帮助!