In Java, I have the user select an image file. I then decode it, covert to PNG, and pass through to JNI. I suspect that I do not need to copy and allocate so much memory constantly. How can I pare this down? Is there a way I can detect if the image type is JPEG or PNG and not decode & re-encode the bitmap and just pass the original JPEG/PNG data? Or do the conversion with fewer allocations? Right now it is eating up a LOT of memory.
Java:
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, outStream);
byte[] byteData = outStream.toByteArray();
JNILib.PickedImage(byteData);
In C, I have the following code for PickedImage:
jbyte *b = (jbyte*)env->GetByteArrayElements(data, NULL);
jsize dataSize = env->GetArrayLength(data);
byte* imageData = (byte*)malloc(dataSize);
memcpy(imageData, b, dataSize);
env->ReleaseByteArrayElements(data, b, 0);
imageSelectCallback(imageData, dataSize);