Marko February 2016

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. 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
  2. YUV image to JPEG.
  3. JPEG to Bitmap.
  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) {
                     

Answers


Francisco Durdin Garcia February 2016

Just specify that camera preview must provide RGB images

i.e. Camera.Parameters.setPreviewFormat(ImageFormat.RGB_565);

Post Status

Asked in February 2016
Viewed 2,875 times
Voted 5
Answered 1 times

Search




Leave an answer