[英]Convert Bitmap to ByteArray and vice versa
在我的Android應用程序中,我想將從相機拍攝的圖像轉換為字節數組,然后轉換回位圖以在圖像視圖中查看。 我可以通過Bitmap.compress輕松實現。 但我想沒有Bitmap.compress。 問題是我得到的是白線(每次(線)的圖像都很差)
Bitmap hello; //image coming from camera
ByteBuffer buffer = ByteBuffer.allocate(hello.getByteCount());
hello.copyPixelsToBuffer(buffer);
byte[] bytes1 = buffer.array();
byte [] Bits = new byte[bytes1.length*4];
int i;
for(i=0;i<bytes1.length;i++)
{
Bits[i*4] =
Bits[i*4+1] =
Bits[i*4+2] = (byte) ~bytes1[i]; //Invert the source bits
Bits[i*4+3] = -1;//0xff, that's the alpha.
}
Bitmap bmimage = Bitmap.createBitmap( 360,248, Bitmap.Config.ARGB_8888);
bmimage.copyPixelsFromBuffer(ByteBuffer.wrap(Bits));
imageView11.setImageBitmap(bmimage);
位圖到字節數組:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageBytes = stream.toByteArray();
要么
int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bitmap.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
字節數組到位圖:
InputStream inputStream = new ByteArrayInputStream(bytes);
BitmapFactory.Options o = new BitmapFactory.Options();
BitmapFactory.decodeStream(inputStream, null, o);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
///useing following code
String encoded = Base64.encodeToString(b, Base64.DEFAULT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.