Android camera preview callback 的使用

来源:互联网 发布:ransac算法原理 编辑:程序博客网 时间:2024/04/28 08:17

android 的camera有一个preview callback,该接口中有一个方法onPreviewFrame:

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

第一个参数data的编码格式默认应该是YCbCr_420_SP (NV21)

该格式的data却无法用BitmapFactory.decodeByteArray方法来解码。在SDK8之后出现了YuvImage的API可以用来处理,但是似乎该API只能用来写入outputStream

以下提供人工解码方式:

public Bitmap rawByteArray2RGBABitmap2(byte[] data, int width, int height) {int frameSize = width * height;        int[] rgba = new int[frameSize];            for (int i = 0; i < height; i++)                for (int j = 0; j < width; j++) {                    int y = (0xff & ((int) data[i * width + j]));                    int u = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 0]));                    int v = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 1]));                    y = y < 16 ? 16 : y;                    int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128));                    int g = Math.round(1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));                    int b = Math.round(1.164f * (y - 16) + 2.018f * (u - 128));                    r = r < 0 ? 0 : (r > 255 ? 255 : r);                    g = g < 0 ? 0 : (g > 255 ? 255 : g);                    b = b < 0 ? 0 : (b > 255 ? 255 : b);                    rgba[i * width + j] = 0xff000000 + (b << 16) + (g << 8) + r;                }        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);        bmp.setPixels(rgba, 0 , width, 0, 0, width, height);        return bmp;}

如果使用opencv,则可以直接如下处理:

public Bitmap rawByteArray2RGBABitmap(byte[] data, int width, int height) {Mat originMat = new Mat(height + height / 2, width, CvType.CV_8UC1);originMat.put(0, 0, data);Mat rgbaMat = new Mat();Imgproc.cvtColor(originMat, rgbaMat, Imgproc.COLOR_YUV420sp2RGB, 4);Log.e(TAG, "rgba type is " + rgbaMat.channels() + rgbaMat.type());Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);if (Utils.matToBitmap(rgbaMat, bmp)) {originMat.release();rgbaMat.release();            return bmp;}bmp.recycle();        return null;}

以上来自android opencv 教程源码。

原创粉丝点击