【问题标题】:How do you convert opengl texture back to bitmap in android?你如何将opengl纹理转换回android中的位图?
【发布时间】:2014-06-26 01:56:47
【问题描述】:

我想对我的图像应用图像过滤器并使用android HelloEffects sample

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

【问题讨论】:

  • 你的结果如何?我也想使用媒体效果,但我还需要能够将结果保存到位图。我开始问和你一样的问题,但后来发现了这个问题。将位图从 GL 纹理保存回来是正确的方法吗?

标签: android opengl-es bitmap textures


【解决方案1】:

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

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

位图到内部存储代码,

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

【讨论】:

  • 这很好用,但是比 10fps 需要很多时间,我们能不能快点 20fps 左右?
  • 对不起,Rohit,我不再在 android 上工作,所以我无能为力。也许其他人可以提供帮助。
【解决方案2】:

【讨论】:

  • 这个!这比我在 SO 上找到的任何其他方法都快得多。
猜你喜欢
  • 2017-06-18
  • 2012-06-02
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
相关资源
最近更新 更多