2012-12-26 49 views
2

我正在使用GLSurfaceView爲Android開發應用程序。有一刻,我必須在那一刻用我們的GLSurfaceView圖像取代它。問題是,如何讓圖像正確?我用這個代碼:如何將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中)?

回答

1

我不認爲它的工作方式與GLSurfaceView。幀緩存可能存在於GPU內部,它不能直接在CPU上訪問。

您可以使用framebuffer object將圖像渲染爲紋理,然後使用glReadPixels將數據下載到緩衝區並將緩衝區變爲Bitmap

+0

嗨托馬斯,謝謝你的回答。你可以請建議任何示例代碼或鏈接.. – harikrishnan

+0

@Thomas我正在做同樣的事情來保存位圖。但我只想獲取位圖部分,即該GLSurfaceView的其他部分。怎麼做? –

+0

如果與此處詢問的不同,請爲此打開一個新問題。 – 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

+0

@harikrishnan您的代碼可以正常工作以將GLSurfaceView保存爲位圖。但是我想只保存位於該SurfaceView上的位圖。 –

相關問題