Reputation: 59
I am trying to obtain rgb value of a pixel from camera.
I keep getting null values. Is there another way to capture the image from camera into bitmap? I've looked into several options but most of them generated NullPointerException.
It also outputs SkImageDecoder::Factory returned null.
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
// Image image = reader.acquireNextImage();
// mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile));
try {
Image image = reader.acquireNextImage();
final Image.Plane[] planes = image.getPlanes();
final Buffer buffer = planes[0].getBuffer();
Log.d("BUFFER", String.valueOf(buffer));
int offset = 0;
//
byte[] bytes = new byte[buffer.remaining()];
Log.d("BUYTES", String.valueOf(bytes));
//
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); // NULL err
Log.d("R1", "bitmap created");
//
int r1, g1, b1;
int p = 50;
r1 = (p >> 16) & 0xff;
g1 = (p >> 8) & 0xff;
b1 = p & 0xff;
Log.d("R1", String.valueOf(r1));
Log.d("G1", String.valueOf(g1));
Log.d("B1", String.valueOf(b1));
} catch (Exception e) {
e.printStackTrace();
}
}
};
Upvotes: 0
Views: 1617
Reputation: 18107
What format is your ImageReader using? If it's JPEG, then your approach should generally work, but you're not actually copying buffer into bytes anywhere.
You're creating the bytes array, and then passing that empty array into decodeByteArray. You need something like ByteBuffer.get to actually copy data into bytes.
If the ImageReader is YUV or RAW, then this won't work; those Images are raw arrays of image data, and have no headers/etc for BitmapFactory to know what to do with them. You'd have to just inspect the pixel values directly, since the contents aren't compressed in any way already.
Upvotes: 1