转换位图的ByteArray,反之亦然

编程入门 行业动态 更新时间:2024-10-28 05:24:13
本文介绍了转换位图的ByteArray,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的Andr​​oid应用程序,我想转换形象,从相机拍摄的,以字节数组,转换回位图来查看一个图像视图。我可以通过Bitmap preSS轻松地做到这一点。但我想这样做没有Bitmap 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 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 Bitmappress. But i want to do it without Bitmappress. 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(); bmppress(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,反之亦然

本文发布于:2023-07-26 19:10:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1217937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   ByteArray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!