I have an int[] intArray, i can make a bitmap like :
Bitmap bitmap = Bitmap.createBitmap(intArray, width, height, Bitmap.Config.ARGB_8888);
Now, i want to use byte[] to make this bitmap, so i try convert int[] to byte[], but it does't work, the bitmap it make seems lack some color
public static byte[] intArrayToByteArray(int[] intArray) { byte[] ba = new byte[intArray.length * 4];
for(int i = 0, k = 0; i < intArray.length; i++) {
int temp= intArray[i];
for(int j = 0; j < 4; j++, k++) {
ba[k] = (byte)((temp>> (8 * j)) & 0xFF);
}
}
return ba;
}
public static Bitmap byteArrayToBitmap(byte[] byteArray, int width, int height) {
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
return bitmap;
}
the bitmap it make seems lack some color by this way.
But if I make bitmap by int[] firstly, and then converted bitmap to byte[], and the byte[] seems to be correct!! This way is not good.
So does somebody has a way convert int[] to byte[], so that we can use byte[] to make a bitmap indtead of using int[].
You need set alpha = false -> bitmap.setHasAlpha(false)
public static Bitmap byteArrayToBitmap(byte[] byteArray, int width, int height) {
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(false);
bitmap.copyPixelsFromBuffer(buffer);
return bitmap;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.