0

I have bitmap which I should manipulate both in c++ and Java sides. Therefore, according to this post I allocated the buffer in C++ and passed the reference to Java. In Java I filled the buffer from bitmap using copyPixelsToBuffer method. When I tried to create bitmap from that buffer(without any manipulations) the decodeByteArray returned null. And I don't understand what was my mistak. Below the code which I used.

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

Any comments and suggestions are appreciated.

Community
  • 1
  • 1
someUser
  • 965
  • 12
  • 24

1 Answers1

0

Here is how i do it.

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

user65721
  • 2,833
  • 3
  • 19
  • 28