如何在opengl中将纹理保存为位图

时间:2012-09-20 10:25:06

标签: android opengl-es opengl-es-2.0

我使用渲染到纹理来渲染图像,我修改纹理,我想将纹理保存为位图。目前我使用方法GLES20.glReadPixels将数据存储在ByteBuffer中并从该数据创建位图。然而,当我渲染到纹理时,我已经将“renderText [0]”纹理附加到FBO,所以我想它是一种更简单的方法将纹理变成位图......是吗?

这是我目前的代码:

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



}

0 个答案:

没有答案