转换位图的字节数组的android位图、数组、字节、android

2023-09-12 21:40:50 作者:Address(地址)

我有我想要发送到服务器通过其编码为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.

任何指针?

推荐答案

您可以使用copyPixelsToBuffer()到像素的数据移动到缓存,也可以使用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.