繁体   English   中英

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

[英]Get pixels matrix from YUV byte array

我需要从YUV byte []数组中获取像素矩阵,该数组是从onPreviewFrame函数中的摄像头预览获得的。 我需要矩阵形式的像素才能实现进一步的图像处理。 我通过以下方式进行操作:

  1. byte []数组到YUV图像
  2. YUV图片转JPEG。
  3. JPEG到位图。
  4. 位图到像素数组。
  5. 像素阵列到像素矩阵。

有没有获得相同结果的最佳方法? 目前,我的实现如下。

@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());
}
}

只需指定相机预览必须提供RGB图像即可

Camera.Parameters.setPreviewFormat(ImageFormat.RGB_565);

暂无
暂无

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

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