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

时间:2014-06-26 01:56:47

标签: android opengl-es bitmap textures

我想将图片过滤器应用于我的图片并使用android HelloEffects sample

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

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

我用一种方式做到了,

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

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

  3. 纹理到位图代码

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

    内部存储代码的位图,

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