1

Im using rendering-to-texture to render an image, Im modifying the texture and I want to save the texture as a bitmap. Currently Im using the method GLES20.glReadPixels storing the data in a ByteBuffer and creating the bitmap from that data. However as Im rendering to a texture I already have the "renderText[0]" texture attached to the FBO so I suppose its a simpler way to get that texture into a bitmap ... is it?

This is my current code :

public void saveChanges() {

    int width = currentBitmapWidth;
    int height = currentBitmapHeight;


    int size = width * height;
    ByteBuffer buf = ByteBuffer.allocateDirect(size * 4);
    buf.order(ByteOrder.nativeOrder());
    GLES20.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, buf);

    int data[] = new int[size];
    buf.asIntBuffer().get(data);
    buf = null;
    Bitmap createdBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    createdBitmap.setPixels(data, size-width, -width, 0, 0, width, height);
    data = null;

    short sdata[] = new short[size];
    ShortBuffer sbuf = ShortBuffer.wrap(sdata);
    createdBitmap.copyPixelsToBuffer(sbuf);
    for (int i = 0; i < size; ++i) {
        //BGR-565 to RGB-565
        short v = sdata[i];
        sdata[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
    }

    sbuf.rewind();
    createdBitmap.copyPixelsFromBuffer(sbuf);

    try {

        if(true)
        {
            Matrix flip = new Matrix();
            flip.postScale(1f, -1f);


            temp = Bitmap.createBitmap(createdBitmap, 0, 0, createdBitmap.getWidth(), createdBitmap.getHeight(), null, true);
            System.out.println("In save changes the temp width = "+temp.getWidth() + " height = "+temp.getHeight());
            oldBitmap = createdBitmap;

            oldBitmap = Bitmap.createBitmap(oldBitmap, 0, 0, oldBitmap.getWidth(), oldBitmap.getHeight(), flip, true);

            //currentImage = bmp;
            mOldTextureId = TextureHelper.loadTexture(context, oldBitmap);

            currentTextureModified = true;
            //drawOld = true;

            createdBitmap.recycle();
            createdBitmap = null;

        }



    } catch (Exception e) {
        // handle
        System.out.println("SAVE IMAGE ERRORRRRRR !!!!");
        System.out.println("Exception description !!! "+e.getMessage());

    }finally
    {
        saving = false;
    }



}
4

0 回答 0