我将渲染图像作为jpeg文件保存到我的库中时遇到问题。我正在应用来自android EffectFactory的照片效果,并将其渲染到glSurfaceView上。从现在起,我已经设法从我的glSurfaceView截取图像的截图。问题在于我不想仅仅截取屏幕截图,因为这样的图像会失去其大部分原始尺寸和质量,因为当原始图像是例如原始图像时。 1944×2592但设备屏幕只有768×1038,在我的情况下它不再是原版了。另一个问题是,当我拍摄截图时,我还有我的glSurfacView的黑色部分,其中图像没有显示在我保存的图像上,因为在许多情况下图像不会填满整个glSurfaceView。从现在开始,我尝试使用此代码:
private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl)
throws OutOfMemoryError {
int bitmapBuffer[] = new int[w * h];
int bitmapSource[] = new int[w * h];
IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
intBuffer.position(0);
try {
gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
int offset1, offset2;
for (int i = 0; i < h; i++) {
offset1 = i * w;
offset2 = (h - i - 1) * w;
for (int j = 0; j < w; j++) {
int texturePixel = bitmapBuffer[offset1 + j];
int blue = (texturePixel >> 16) & 0xff;
int red = (texturePixel << 16) & 0x00ff0000;
int pixel = (texturePixel & 0xff00ff00) | red | blue;
bitmapSource[offset2 + j] = pixel;
}
}
} catch (GLException e) {
return null;
}
return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}
任何人都可以告诉我如何从图像原始大小的渲染图像中获取位图/ jpeg?是否可以使用gl.glReadPixels?或者我能以某种方式直接从帧缓冲区获取图像吗?重要的是我能够以原始尺寸获得图像。
答案 0 :(得分:0)
public static Bitmap createBitmap(int width,int height){
// int width=bitmap.getWidth();
// int height =bitmap.getHeight();
int screenshotSize = width * height;
ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
bb.order(ByteOrder.nativeOrder());
GLES20.glReadPixels(5, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, bb);
int pixelsBuffer[] = new int[screenshotSize];
bb.asIntBuffer().get(pixelsBuffer);
bb = null;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
bitmap.setPixels(pixelsBuffer, screenshotSize-width, -width,0, 0, width, height);
pixelsBuffer = null;
short sBuffer[] = new short[screenshotSize];
ShortBuffer sb = ShortBuffer.wrap(sBuffer);
bitmap.copyPixelsToBuffer(sb);
//Making created bitmap (from OpenGL points) compatible with Android bitmap
for (int i = 0; i < screenshotSize; ++i) {
short v = sBuffer[i];
sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
}
sb.rewind();
bitmap.copyPixelsFromBuffer(sb);
// if(savepicture!=null){
// savepicture.onPictureSaved(bitmap);
// // new SaveTask(bitmap, FileUtils.getFileDir().getPath(),"IMG"+System.currentTimeMillis()+".png", savepicture).execute();
// screenshot = false;
// }
return bitmap;
}
&#13;