我需要从 YUV byte [] 数组中获取像素矩阵,这是我从 onPreviewFrame 函数中的相机预览中获得的。我需要矩阵形式的像素以实现进一步的图像处理。 我是按照以下方式做到的:
有没有达到相同结果的最佳方法? 目前,我的实施如下。
@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());
}
}