你如何在android中将opengl纹理转换回位图?

eug*_*ene 3 android textures opengl-es bitmap

我想将图像过滤器应用于我的图像并使用android HelloEffects 示例

它将图像数据从位图转换为纹理。
应用图像滤镜效果后,我想将图像恢复为 jpeg 格式,但不知道该怎么做。

Riz*_*med 5

我以一种方式做到了这一点,

  1. 我使用 glreadpixles() 方法将纹理图像转换为位图

  2. 然后我将位图保存到 SD 卡

纹理到位图代码

public static Bitmap SavePixels(int x, int y, int w, int h){
    int b[]=new int[w*(y+h)];
    int bt[]=new int[w*h];
    IntBuffer ib = IntBuffer.wrap(b);
    ib.position(0);
    GLES20.glReadPixels(0, 0, w, h, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);

    for(int i=0, k=0; i<h; i++, k++)
    {//remember, that OpenGL bitmap is incompatible with Android bitmap
        //and so, some correction need.
        for(int j=0; j<w; j++)
        {
            int pix=b[i*w+j];
            int pb=(pix>>16)&0xff;
            int pr=(pix<<16)&0x00ff0000;
            int pix1=(pix&0xff00ff00) | pr | pb;
            bt[(h-k-1)*w+j]=pix1;
        }
    }

    Bitmap sb=Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
    return sb;
}
Run Code Online (Sandbox Code Playgroud)

位图到内部存储代码,

 public  static void saveImage(Bitmap finalBitmap) {

    File myDir=new File("/sdcard/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)