0

Java から直接バイトバッファを割り当て、ビットマップから入力し、そのバッファからビットマップを作成しようとしています。しかし、結果として私はnullを受け取ります。

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    mCurrentBitmap = BitmapFactory.decodeFile(hardCodedPath, options);

    // 4 - bytes count per pixel
    bytesCount = mCurrentBitmap.getWidth() * mCurrentBitmap.getHeight() * 4;

    pixels = ByteBuffer.allocateDirect((int) bytesCount);
    mCurrentBitmap.copyPixelsToBuffer(this.pixels);

    byte[] bitmapdata = new byte[pixels.remaining()];
    pixels.get(bitmapdata);

    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Bitmap newBitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, (int) bytesCount, opt);

newBitmap が null である理由を誰かが理解するのを手伝ってくれますか?

4

1 に答える 1

2

decodeByteArray()単純な RGB(A) バイト配列ではなく、エンコードされたビットマップ データ (PNG、JPEG など) を想定しています。必要なことを行うには、単に使用できますBitmap.setPixels()。まず、適切なサイズ/構成 (Bitmap.create(width, height, Bitmap.Config.ARGB_8888)たとえば) のビットマップを作成し、それを呼び出しますsetPixels(bitmapdata, 0, width, 0, 0, width, height)

があるので、ByteBufferを呼び出すことでさらに簡単に実行できますBitmap.copyPixelsFromBuffer()。と同様にsetPixels()、最初に適切なサイズのビットマップを作成します。

于 2013-04-18T19:19:55.760 に答える