我在将渲染图像作为 jpeg 文件保存到我的图库时遇到问题。我正在应用 android EffectFactory 中的照片效果并将其渲染在 glSurfaceView 上。从现在开始,我已经成功地从 glSurfaceView 中截取了图像的屏幕截图。问题是我不想只截取屏幕截图,因为这样图像会丢失其原始尺寸和质量,因为当原始图像例如1944 × 2592,但设备屏幕只有 768 × 1038 在我的情况下,它不再是原来的。另一个问题是,当我截取屏幕截图时,我的 glSurfaceView 也有黑色部分,其中图像未显示在我保存的图像上,因为在很多情况下图像不会填充整个 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 是否有可能?或者我可以以某种方式直接从帧缓冲区获取图像吗?重要的是我可以得到原始尺寸的图像。
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;
}
为了解决这个问题,可以使用另一种方法来获取图像。您可以使用 PixelCopy 方法从视图复制图像,而不是使用 glReadPixels。
以下是如何使用 PixelCopy:
public void takeScreenshot(SurfaceView view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
PixelCopy.request(view, bitmap, new PixelCopy.OnPixelCopyFinishedListener() {
@Override
public void onPixelCopyFinished(int copyResult) {
if (copyResult == PixelCopy.SUCCESS) {
// Do something with the bitmap, e.g., save it to a file or display it in an ImageView
} else {
// Handle the error, e.g., show a toast with an error message
}
}
}, new Handler());
}
不要忘记在 AndroidManifest.xml 文件中添加声明以启用 PixelCopy:
<uses-permission android:name="android.permission.READ_FRAME_BUFFER"/>