转换位图的字节数组的android [英] Converting bitmap to byteArray android

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

问题描述

我有我想要发送到服务器通过其编码为base64位图,但我不想COM preSS在任何PNG或JPEG图像。

I have a bitmap that I want to send to the server by encoding it to base64 but I do not want to compress the image in either png or jpeg.

现在我在previously做了。

Now what I was previously doing was.

ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
//then simple encoding to base64 and off to server
encodedImage = Base64.encodeToString(b, Base64.NO_WRAP);

现在我只是不希望使用任何COM pression也没有任何格式的朴素简单的byte []从位图,我可以连接code和发送到服务器。

Now I just dont want to use any compression nor any format plain simple byte[] from bitmap that I can encode and send to the server.

任何指针?

推荐答案

您可以使用<一个href="http://developer.android.com/reference/android/graphics/Bitmap.html#copyPixelsToBuffer%28java.nio.Buffer%29"><$c$c>copyPixelsToBuffer()到像素的数据移动到缓存,也可以使用<一个href="http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels%28int%5B%5D,%20int,%20int,%20int,%20int,%20int,%20int%29"><$c$c>getPixels()然后转换成整数与位移字节。

You can use copyPixelsToBuffer() to move the pixel data to a Buffer, or you can use getPixels() and then convert the integers to bytes with bit-shifting.

copyPixelsToBuffer()可能是你想要的东西使用,所以这里是如何使用它的一个例子:

copyPixelsToBuffer() is probably what you'll want to use, so here is an example on how you can use it:

//b is the Bitmap

//calculate how many bytes our image consists of.
int bytes = b.getByteCount();
//or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
//int bytes = b.getWidth()*b.getHeight()*4; 

ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

byte[] array = buffer.array(); //Get the underlying array containing the data.

这篇关于转换位图的字节数组的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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