分配本机缓冲区并在JAVA中使用?

时间:2013-04-18 07:12:23

标签: java android c++ android-ndk java-native-interface

我有位图,我应该在c ++和Java方面操作。 因此,根据this帖子,我在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);

感谢任何意见和建议。

1 个答案:

答案 0 :(得分: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);

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