问题描述
我想实现两个方法。
一个带一个ImageView的输入和输出uncom pressed字节数组。
One that takes an ImageView as input and outputs uncompressed byte array.
第二个接受字节数组输入转换为位图。
The second takes byte array input and converts to a bitmap.
这是两种方法,我使用,但是bytesToImage()未能出示imageToBytes输出的一个有效的位图重新presentation:
These are the two methods I use however bytesToImage() fails to produce a valid bitmap representation of the output of imageToBytes:
private static byte[] imageToBytes(ImageView iv) {
byte[] imageInByte = null;
Bitmap originalImage;
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
originalImage = drawable.getBitmap();
int numOfbytes = originalImage.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(numOfbytes);
originalImage.copyPixelsToBuffer(buffer);
imageInByte = buffer.array();
return imageInByte;
}
private static Bitmap bytesToImage(byte data[]) {
ByteBuffer byte_buffer = ByteBuffer.wrap(data);
byte_buffer.rewind();
Bitmap bmp = Bitmap.createBitmap(60, 60, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(byte_buffer);
return bmp;
}
然后这些方法称为通过以下方式:
These methods are then called in the following way:
ImageView iv = (ImageView) findViewById(R.id.imageViewInput);
iv.setImageResource(R.drawable.panda);
imageInBytes = imageToBytes(iv);
Bitmap bmp = bytesToImage(imageInBytes);
ImageView image = (ImageView) findViewById(R.id.imageViewOutput);
image.setImageBitmap(bmp);
然而,imageViewOutput只显示一个空的白牌。
However the imageViewOutput just displays an empty whitebox.
图片大熊猫为.bmp图片
The image panda is a .bmp image
推荐答案
所以,问题是在bytesToImage位图被正确配置。
So the problem is with the bitmap in bytesToImage being incorrectly configured.
这需要原始位在直接通过。
This requires the original bitmap to be passed in directly.
下面是更新bytesToImage方法,这给正确的答案。
Here is the updated bytesToImage method which gives correct answer.
private static Bitmap bytesToImage(byte data[], Bitmap originalImage) {
Bitmap newBmp;
newBmp = Bitmap.createBitmap(originalImage.getWidth(), originalImage.getHeight(), originalImage.getConfig());
ByteBuffer buffer1 = ByteBuffer.wrap(data);
buffer1.rewind();
newBmp.copyPixelsFromBuffer(buffer1);
byte[] imageInByte = null;
ByteBuffer byte_buffer = ByteBuffer.wrap(data);
byte_buffer.rewind();
newBmp.copyPixelsFromBuffer(byte_buffer);
return newBmp;
}
这篇关于转换和从uncom pressed位图的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!