312
  Bitmap bmp   = intent.getExtras().get("data");
  int size     = bmp.getRowBytes() * bmp.getHeight();
  ByteBuffer b = ByteBuffer.allocate(size);

  bmp.copyPixelsToBuffer(b);

  byte[] bytes = new byte[size];

  try {
     b.get(bytes, 0, bytes.length);
  } catch (BufferUnderflowException e) {
     // always happens
  }
  // do something with byte[]

バイトの呼び出しcopyPixelsToBufferがすべて0になった後、バッファを見ると...カメラから返されるビットマップは不変です...しかし、コピーを実行しているので、それは問題ではありません。

このコードの何が問題になっている可能性がありますか?

4

10 に答える 10

694

次のようなものを試してください。

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bmp.recycle();
于 2011-02-14T06:49:43.350 に答える
81

CompressFormatが遅すぎます...

ByteBufferを試してください。

※※※バイトへのビットマップ※※※</p>

width = bitmap.getWidth();
height = bitmap.getHeight();

int size = bitmap.getRowBytes() * bitmap.getHeight();
ByteBuffer byteBuffer = ByteBuffer.allocate(size);
bitmap.copyPixelsToBuffer(byteBuffer);
byteArray = byteBuffer.array();

※※※バイトからビットバイトまで※※※</p>

Bitmap.Config configBmp = Bitmap.Config.valueOf(bitmap.getConfig().name());
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, configBmp);
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
bitmap_tmp.copyPixelsFromBuffer(buffer);
于 2015-12-08T20:34:57.417 に答える
24

.convertToByteArrayこれがKotlinで書かれた ビットマップ拡張です。

/**
 * Convert bitmap to byte array using ByteBuffer.
 */
fun Bitmap.convertToByteArray(): ByteArray {
    //minimum number of bytes that can be used to store this bitmap's pixels
    val size = this.byteCount

    //allocate new instances which will hold bitmap
    val buffer = ByteBuffer.allocate(size)
    val bytes = ByteArray(size)

    //copy the bitmap's pixels into the specified buffer
    this.copyPixelsToBuffer(buffer)

    //rewinds buffer (buffer position is set to zero and the mark is discarded)
    buffer.rewind()

    //transfer bytes from buffer into the given destination array
    buffer.get(bytes)

    //return bitmap's pixels
    return bytes
}
于 2017-12-11T14:50:54.483 に答える
19

おそらく、バッファを巻き戻す必要がありますか?

また、これは、ビットマップのストライド(バイト単位)がピクセル単位の行の長さ*バイト/ピクセルより大きい場合に発生する可能性があります。サイズの代わりにバイトの長さをb.remaining()にします。

于 2011-02-14T05:56:35.680 に答える
10

以下の関数を使用して、ビットマップをbyte []に​​エンコードし、その逆も行います。

public static String encodeTobase64(Bitmap image) {
    Bitmap immagex = image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immagex.compress(Bitmap.CompressFormat.PNG, 90, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

public static Bitmap decodeBase64(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
于 2016-04-22T10:28:25.037 に答える
6

バイト配列が小さすぎます。各ピクセルは1バイトだけでなく4バイトを占めるため、サイズ* 4を掛けて、配列が十分に大きくなるようにします。

于 2012-03-23T01:05:01.227 に答える
4

Ted Hoppは正しいです、APIドキュメントから:

public void copyPixelsToBuffer (Buffer dst)

"...このメソッドが戻った後、バッファの現在の位置が更新されます。位置は、バッファに書き込まれた要素の数だけ増分されます。"

public ByteBuffer get (byte[] dst, int dstOffset, int byteCount)

「指定されたオフセットから開始して、現在の位置から指定されたバイト配列にバイトを読み取り、読み取られたバイト数だけ位置を増やします。」

于 2016-02-19T14:11:37.410 に答える
3

より大きなファイルのエラーを回避するためにOutOfMemory、ビットマップをいくつかの部分に分割し、それらの部分のバイトをマージすることによってタスクを解決することをお勧めします。

private byte[] getBitmapBytes(Bitmap bitmap)
{
    int chunkNumbers = 10;
    int bitmapSize = bitmap.getRowBytes() * bitmap.getHeight();
    byte[] imageBytes = new byte[bitmapSize];
    int rows, cols;
    int chunkHeight, chunkWidth;
    rows = cols = (int) Math.sqrt(chunkNumbers);
    chunkHeight = bitmap.getHeight() / rows;
    chunkWidth = bitmap.getWidth() / cols;

    int yCoord = 0;
    int bitmapsSizes = 0;

    for (int x = 0; x < rows; x++)
    {
        int xCoord = 0;
        for (int y = 0; y < cols; y++)
        {
            Bitmap bitmapChunk = Bitmap.createBitmap(bitmap, xCoord, yCoord, chunkWidth, chunkHeight);
            byte[] bitmapArray = getBytesFromBitmapChunk(bitmapChunk);
            System.arraycopy(bitmapArray, 0, imageBytes, bitmapsSizes, bitmapArray.length);
            bitmapsSizes = bitmapsSizes + bitmapArray.length;
            xCoord += chunkWidth;

            bitmapChunk.recycle();
            bitmapChunk = null;
        }
        yCoord += chunkHeight;
    }
    
    return imageBytes;
}


private byte[] getBytesFromBitmapChunk(Bitmap bitmap)
{
    int bitmapSize = bitmap.getRowBytes() * bitmap.getHeight();
    ByteBuffer byteBuffer = ByteBuffer.allocate(bitmapSize);
    bitmap.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();
    return byteBuffer.array();
}
于 2016-11-07T17:23:08.967 に答える
1

私はこれでうまくいくと思います-

public static byte[] convertBitmapToByteArray(Bitmap bitmap){
        ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(byteBuffer);
        byteBuffer.rewind();
        return byteBuffer.array();
    }
于 2021-07-07T07:26:35.753 に答える
0

String-BitmapまたはBitmap-Stringを変換するにはこれを試してください

/**
 * @param bitmap
 * @return converting bitmap and return a string
 */
public static String BitMapToString(Bitmap bitmap){
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
    byte [] b=baos.toByteArray();
    String temp=Base64.encodeToString(b, Base64.DEFAULT);
    return temp;
}

/**
 * @param encodedString
 * @return bitmap (from given string)
 */
public static Bitmap StringToBitMap(String encodedString){
    try{
        byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
        Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
        return bitmap;
    }catch(Exception e){
        e.getMessage();
        return null;
    }
}
于 2017-05-03T05:29:34.350 に答える