51

base64にエンコードしてサーバーに送信したいビットマップがありますが、画像をpngまたはjpegで圧縮したくありません。

今、私が以前やっていたことはでした。

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

今では、エンコードしてサーバーに送信できる、ビットマップからの圧縮やフォーマットの単純なbyte[]を使用したくありません。

ポインタはありますか?

4

3 に答える 3

135

を使用copyPixelsToBuffer()してピクセル データを に移動するBufferか、 を使用getPixels()して整数をビット シフトでバイトに変換できます。

copyPixelsToBuffer()おそらくあなたが使いたいと思うものなので、それをどのように使用できるかの例を次に示します:

//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.
于 2012-04-17T13:21:00.243 に答える
9

@jave answer の次の行の代わりに:

int bytes = b.getByteCount();

次の行と関数を使用します。

int bytes = byteSizeOf(b);

protected int byteSizeOf(Bitmap data) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
    return data.getRowBytes() * data.getHeight();
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    return data.getByteCount();
} else {
      return data.getAllocationByteCount();
}
于 2015-04-23T06:27:37.157 に答える