转换位图的ByteArray,反之亦然 [英] Convert Bitmap to ByteArray and vice versa

查看:266
本文介绍了转换位图的ByteArray,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我想转换形象,从相机拍摄的,以字节数组,转换回位图来查看一个图像视图。我可以通过Bitmap.com preSS轻松地做到这一点。但我想这样做没有Bitmap.com preSS。问题是,我收到白线(较差的图像每次(线))

 位图您好; //图像从摄像机里
                        ByteBuffer的缓冲= ByteBuffer.allocate(hello.getByteCount());
                        hello.copyPixelsToBuffer(缓冲液);
                        字节[] = bytes1 buffer.array();
                        字节[]位=新的字节[bytes1.length * 4];
                        INT I;
                   对于(i = 0; I< bytes1.length;我++)
                {
                    位[我* 4] =
                        位[我* 4 + 1] =
                        位[我* 4 + 2] =(字节)〜bytes1 [I]; //反转源位
                    位[我* 4 + 3] = -1; // 0xFF的,这是阿尔法。
                }                位图bmimage = Bitmap.createBitmap(360248,Bitmap.Config.ARGB_8888);
                bmimage.copyPixelsFromBuffer(ByteBuffer.wrap(位));
                imageView11.setImageBitmap(bmimage);


解决方案

位图的字节数组:

  ByteArrayOutputStream流=新ByteArrayOutputStream();
bmp.com preSS(Bitmap.Com pressFormat.PNG,100,流);
字节[] = imageBytes stream.toByteArray();

 字节INT = bitmap.getByteCount();
ByteBuffer的缓冲= ByteBuffer.allocate(字节);
bitmap.copyPixelsToBuffer(缓冲液);
byte []数组= buffer.array();

字节数组位图:

 的InputStream的InputStream =新ByteArrayInputStream进行(字节);
  BitmapFactory.Options O =新BitmapFactory.Options();
  BitmapFactory.de codeStream(InputStream中,空,O);

In my android application i want to convert image ,taken from camera, to byte array and convert back to bitmap to view in a image view. I can do it easily through Bitmap.compress. But i want to do it without Bitmap.compress. The problem is that i am getting white lines (poor images every time(lines) )

                        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);

解决方案

Bitmap to byte array:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageBytes = stream.toByteArray();

or

int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); 
bitmap.copyPixelsToBuffer(buffer); 
byte[] array = buffer.array();

byte array to Bitmap:

 InputStream inputStream = new ByteArrayInputStream(bytes);
  BitmapFactory.Options o = new BitmapFactory.Options();
  BitmapFactory.decodeStream(inputStream, null, o);

这篇关于转换位图的ByteArray,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆