1

I have an application that currently takes black and white 176 x 144 images via camera2 in jpeg format and saves it to storage. In addition to this, I also need an int/float array where each point corresponds to the intensity of one pixel from the jpeg image. As the image is black and white this array of numbers should be sufficient to reconstruct my image by simply plotting it as a heatmap along the appropriate dimensions, as only one value per pixel is needed in black and white space.

The way I have found to do this is to convert the jpeg into a byte array into a bitmap into an int array of sRGB values into an int array of R values. This does work (code below), but seems like a really longwinded and inefficient way of doing this. Is anyone able to suggest a more direct way? Such as getting pixel values directly from the original jpeg Image?

    // Convert photo (176 x 144) to byte array (1x25344)
    Image mImage = someImage  // jpeg capture from camera
    ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);

    //Save photo as jpeg
    savePhoto(bytes);

    //Save pixel values by converting to Bitmap first
    Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    int x = image.getWidth();
    int y = image.getHeight();
    int[] intArray = new int[x * y];
    image.getPixels(intArray, 0, x, 0, 0, x, y);
    for(int i = 0; i < intArray.length; i++) {
        intArray[i] = Color.red(intArray[i]); //Any colour will do
    }

    //Save pixel values
    saveIntArray(intArray);
James
  • 57
  • 8
  • Convert it to grayscale, then save the bitmap [convert example](https://stackoverflow.com/a/3391061/940834) Not sure if its more efficient. – IAmGroot Jun 01 '17 at 14:30
  • The bitmap is already grayscale, the code i am using is to extract from the bitmap encoding a single value (i.e. one of the RGB channels). – James Jun 02 '17 at 08:59
  • Ye I get that. I was just suggesting an alternative to do the same thing. As you were looking for more efficient ways to do it? Maybe I misunderstood. Your code generally looks fine anyway. – IAmGroot Jun 02 '17 at 12:41
  • 1
    Yes sorry, thank you for the feedback. I didn't mean to appear ungrateful. The bitmap convert example is less efficient than what I have come up with myself. Maybe that's as good as it's going to get after all. It seems image processing on Android is not as streamlined as the rest of their libraries – James Jun 03 '17 at 21:38

0 Answers0