Java 如何将GLSURFACHEVIEW保存到纹理/缓冲区/位图
我正在用GLSurfaceView为Android开发应用程序。有一刻,我不得不用它在那一刻的图像替换我的GLSURFACHEVIEW。问题是,如何获得正确的图像?我使用了以下代码:Java 如何将GLSURFACHEVIEW保存到纹理/缓冲区/位图,java,android,opengl-es,bitmap,Java,Android,Opengl Es,Bitmap,我正在用GLSurfaceView为Android开发应用程序。有一刻,我不得不用它在那一刻的图像替换我的GLSURFACHEVIEW。问题是,如何获得正确的图像?我使用了以下代码: v.setDrawingCacheEnabled(true); v.measure(View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpe
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;
但它总是返回黑色位图
也许我们可以制作位图以外的东西(也可以放在GLSURFACHEVIEW上)?我不认为它在
GLSURFACHEVIEW上是这样工作的。帧缓冲区可能位于GPU内部,CPU无法直接访问
您可以使用将图像渲染为纹理,然后使用glReadPixels
将数据下载到缓冲区,并将缓冲区转换为位图
将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() ;
}
MyRenderer类:
@凌驾
公共框架(GL10 gl){
试一试{
int w=表面的宽度;
int h=曲面的高度;
Log.i(“hari”,“w:+w+”----h:+h);
int b[]=新int[(int)(w*h)];
int bt[]=新int[(int)(w*h)];
IntBuffer=IntBuffer.wrap(b);
缓冲器位置(0);
GLES20.glReadPixels(0,0,w,h,GLES20.GL_RGBA,GLES20.GL_无符号_字节,缓冲区);
对于(int i=0;i16)&0xff;
国际公共关系=(pixHi Thomas,谢谢你的回答。你能推荐任何示例代码或链接吗?@Thomas我正在做同样的事情来保存位图。但我只想获取位图部分,GLSurfaceView的其他部分。如何做到这一点?如果这与此处询问的问题不同,请打开一个新问题。请编辑你的答案并格式化代码t。)哦,成功了readable@harikrishnan您的代码可以将GLSURFACHEVIEW保存为位图。但我只想保存显示在该SURFACHEVIEW上的位图。