I need to get pixels matrix from YUV byte[] array which I obtain from camera preview in onPreviewFrame function. I need pixels in form of matrix in order to implement further image processing. I'm doing this in the following way:
- byte[] array to YUV image
- YUV image to JPEG.
- JPEG to Bitmap.
- Bitmap to pixels array.
- 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());
}
}