简体   繁体   English

从YUV字节数组获取像素矩阵

[英]Get pixels matrix from YUV byte array

I need to get pixels matrix from YUV byte[] array which I obtain from camera preview in onPreviewFrame function. 我需要从YUV byte []数组中获取像素矩阵,该数组是从onPreviewFrame函数中的摄像头预览获得的。 I need pixels in form of matrix in order to implement further image processing. 我需要矩阵形式的像素才能实现进一步的图像处理。 I'm doing this in the following way: 我通过以下方式进行操作:

  1. byte[] array to YUV image byte []数组到YUV图像
  2. YUV image to JPEG. YUV图片转JPEG。
  3. JPEG to Bitmap. JPEG到位图。
  4. Bitmap to pixels array. 位图到像素数组。
  5. Pixels array to pixels matrix. 像素阵列到像素矩阵。

Is there any optimal way for achieving same results? 有没有获得相同结果的最佳方法? Currently, my implementation is as follow. 目前,我的实现如下。

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

try {
    final YuvImage image = new YuvImage(data, ImageFormat.NV21,
            size.width, size.height, null);
    final File file = new File(Environment.getExternalStorageDirectory()
            .getPath() + "/" + String.valueOf(br) + "out.jpg");
    final FileOutputStream file_o_s = new FileOutputStream(file);
    Thread comp_thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                image.compressToJpeg(
                        new Rect(0, 0, size.width, size.height), 40,
                        file_o_s);
                file_o_s.flush();
                file_o_s.close();

                Bitmap bMap = BitmapFactory.decodeFile(file.getPath());
                int[] pixels_array = new int[bMap.getHeight()*bMap.getWidth()];
                bMap.getPixels(pixels_array, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
                int[][] pixels_matrix = new int[bMap.getHeight()][bMap.getWidth()];

                int start = 0;
                for (int r = 0; r < bMap.getHeight(); r++) {
                    int L = Math.min(bMap.getWidth(), pixels_array.length - start);
                    pixels_matrix[r] = java.util.Arrays.copyOfRange(pixels_array, start, start + L);
                    start += L;
                }

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

        }
    });
    comp_thread.start();

} catch (Exception e) {
    Log.e(tag, e.getMessage());
}
}

Just specify that camera preview must provide RGB images 只需指定相机预览必须提供RGB图像即可

ie Camera.Parameters.setPreviewFormat(ImageFormat.RGB_565); Camera.Parameters.setPreviewFormat(ImageFormat.RGB_565);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM