So i'm trying to port some C++ code from a colleague that grabs image data over a Bluetoth serial port (I'm using an Android phone). From the data I will need to generate a bitmap.
Before testing the ported code, I wrote this quick function to suposedly generate a pure red rectangle. However, BitmapFactory.decodeByteArray() always fails and returns with a null bitmap. I've checked for both of the possible exeptions it can throw and neither one is thrown.
byte[] pixelData = new byte[225*160*4];
for(int i = 0; i < 225*160; i++) {
pixelData[i * 4 + 0] = (byte)255;
pixelData[i * 4 + 1] = (byte)255;
pixelData[i * 4 + 2] = (byte)0;
pixelData[i * 4 + 3] = (byte)0;
}
Bitmap image = null;
logBox.append("Creating bitmap from pixel data...\n");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.outWidth = 225;
options.outHeight = 160;
try {
image = BitmapFactory.decodeByteArray(pixelData, 0, pixelData.length, options);
} catch (IllegalArgumentException e) {
logBox.append(e.toString() + '\n');
}
//pixelData = null;
logBox.append("Bitmap generation complete\n");
decodeByteArray() code:
public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts) {
if ((offset | length) < 0 || data.length < offset + length) {
throw new ArrayIndexOutOfBoundsException();
}
Bitmap bm;
Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
try {
bm = nativeDecodeByteArray(data, offset, length, opts);
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
}
setDensityFromOptions(bm, opts);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
}
return bm;
}
I would presume that it's nativeDecodeByteArray() that is failing.
I also notice the log message:
D/skia: --- SkImageDecoder::Factory returned null
Anyone got any ideas?