I’m currently trying to save an image (e.g. from a Texture) to the sd card for example, here is the code I use, I think it will explain it best. I used some of the methods from AndroidScreenshots.java and JmeAndroidSystem.java and just copied them for easier testing:
[java]
Image image = an image like new Image(Image.Format.RGB565, (int) width, (int) height, pixels);
…
FileOutputStream outStream = new FileOutputStream(someTargetFileDestination);
ByteBuffer imagePixels = image.getData(0);
writeImageFile(outStream, imagePixels, image.getWidth(),
image.getHeight());
private void writeImageFile(FileOutputStream outStream,
ByteBuffer imageData, int width, int height) {
Bitmap bitmapImage = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
convertScreenShot(imageData, bitmapImage);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 95, outStream);
}
public static void convertScreenShot(ByteBuffer buf, Bitmap bitmapImage) {
int width = bitmapImage.getWidth();
int height = bitmapImage.getHeight();
int size = width * height;
// Grab data from ByteBuffer as Int Array to manipulate data and send to
// image
IntBuffer intBuffer = buf.asIntBuffer();
Log.w(LOGTAG, “width=” + width);
Log.w(LOGTAG, “height=” + height);
Log.w(LOGTAG, “imagesize=” + size);
Log.w(LOGTAG, “intBuffer.capacity()=” + intBuffer.capacity());
int[] data = new int[size];
intBuffer.get(data, 0, data.length);
// intBuffer.get(data);
// convert from GLES20.GL_RGBA to Bitmap.Config.ARGB_8888
// ** need to swap RED and BLUE **
if (bitmapImage.getConfig() == Bitmap.Config.ARGB_8888) {
for (int idx = 0; idx < data.length; idx++) {
int initial = data[idx];
int pb = (initial >> 16) & 0xff;
int pr = (initial << 16) & 0x00ff0000;
int pix1 = (initial & 0xff00ff00) | pr | pb;
data[idx] = pix1;
}
}
// OpenGL and Bitmap have opposite starting points for Y axis (top vs
// bottom)
// Need to write the data in the image from the bottom to the top
// Use size-width to indicate start with last row and increment by
// -width for each row
bitmapImage.setPixels(data, size – width, -width, 0, 0, width, height);
}
[/java]
When i try to use this the image is not stored because the intBuffer.capacity is half the size of the imagesize (I think because the Image has the format Image.Format.RGB565 and the bitmap has the format Bitmap.Config.ARGB_8888. Here are the log outputs i added in the code:
width=640
height=480
imagesize=307200
intBuffer.capacity()=153600
so my question is how to I convert the Image format or how do i fill the data array correctly when I get an Image with format Image.Format.RGB565 ?