如何将GLSurfaceView保存为纹理/缓冲区/位图

3

我正在开发一个使用GLSurfaceView的Android应用程序。有一刻,我需要将我的GLSurfaceView替换为其图像。问题是,如何获取正确的图像?我使用了以下代码:

    v.setDrawingCacheEnabled(true);
    v.measure(View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.AT_MOST),
            View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.AT_MOST));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false); // clear drawing cache
    return b;

但它总是返回黑色的位图。

也许我们可以使用一些其他的东西,而不是位图(也可以放置在GLSurfaceView中)?

2个回答

2
我认为使用GLSurfaceView无法实现此功能。帧缓冲可能在GPU内部存在,CPU无法直接访问。
您可以使用帧缓冲对象将图像渲染到纹理中,然后使用glReadPixels将数据下载到缓冲区,并将缓冲区转换为Bitmap

嗨,Thomas,谢谢你的回答。你能否提供任何示例代码或链接吗? - harikrishnan
@Thomas 我正在尝试保存位图,但我只想获取位图部分,而不是GLSurfaceView的其他部分。如何做到这一点? - Shreyash Mahajan
如果问题与此处提出的问题不同,请开一个新的问题。 - Thomas

0

将GLSurfaceView保存为位图。它正常工作。

MyRenderer Class :

@Override
public void onDrawFrame(GL10 gl) {


try {
    int w = width_surface ;
    int h = height_surface  ;

    Log.i("hari", "w:"+w+"-----h:"+h);

    int b[]=new int[(int) (w*h)];
    int bt[]=new int[(int) (w*h)];
    IntBuffer buffer=IntBuffer.wrap(b);
    buffer.position(0);
    GLES20.glReadPixels(0, 0, w, h,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE, buffer);
    for(int i=0; i<h; i++)
    {
         //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-i-1)*w+j]=pix1;
         }
    }           
    Bitmap inBitmap = null ;
    if (inBitmap == null || !inBitmap.isMutable()
         || inBitmap.getWidth() != w || inBitmap.getHeight() != h) {
        inBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    }
    //Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    inBitmap.copyPixelsFromBuffer(buffer);
    //return inBitmap ;
    // return Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
    inBitmap = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    inBitmap.compress(CompressFormat.JPEG, 90, bos); 
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream fis = new ByteArrayInputStream(bitmapdata);

    final Calendar c=Calendar.getInstance();
     long mytimestamp=c.getTimeInMillis();
    String timeStamp=String.valueOf(mytimestamp);
    String myfile="hari"+timeStamp+".jpeg";

    dir_image=new File(Environment.getExternalStorageDirectory()+File.separator+
             "printerscreenshots"+File.separator+"image");
    dir_image.mkdirs();

    try {
        File tmpFile = new File(dir_image,myfile); 
        FileOutputStream fos = new FileOutputStream(tmpFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = fis.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }
        fis.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

       Log.v("hari", "screenshots:"+dir_image.toString());

    }
}catch(Exception e) {
    e.printStackTrace() ;
}

1
请编辑您的答案并格式化代码以使其易读。 - kleopatra
@harikrishnan,你的代码可以很好地将GLSurfaceView保存为位图。但是我想要保存的只是我在该SurfaceView上显示的位图。 - Shreyash Mahajan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接