0

我有位图,我应该在 c++ 和 Java 方面进行操作。因此,根据这篇文章,我在 C++ 中分配了缓冲区并将引用传递给 Java。在 Java 中,我使用copyPixelsToBuffer方法从位图填充缓冲区。当我尝试从该缓冲区创建位图(没有任何操作)时,decodeByteArray返回 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) allocNativeBuffer(bytesCount);
    pixels.order(ByteOrder.nativeOrder());

    mCurrentBitmap.copyPixelsToBuffer(pixels);
    pixels.flip();
    pixels.order(ByteOrder.BIG_ENDIAN);
    byte[] bitmapdata = new byte[pixels.remaining()];

    pixels.get(bitmapdata);

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

    Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length, opt);

任何意见和建议表示赞赏。

4

1 回答 1

0

这是我的做法。

ByteBuffer mPixels = ByteBuffer.allocateDirect( saveWidth*saveHeight*4).order(ByteOrder.nativeOrder()); GLES20.glReadPixels(0, 0, mWidth, mHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, mPixels);

resultBitmap = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
 resultBitmap.copyPixelsFromBuffer(mPixels);

于 2013-06-11T21:12:11.153 回答