Android 中 GLSurfaceView 截图

29 篇文章 5 订阅
18 篇文章 21 订阅

GLSurfaceView 截取图像的时候,往往传统的方法并不行得通,我们发现使用 GLSurfaceView.getDrawingCache() 等方法得到的往往是一张纯黑的图,这是由于 GLSurfaceView 和 SurfaceView 一样都有一块透明的缓存区域,所以我们截取的往往只是这块透明的缓存区域。

 

方法一:

下面这个方法是在 stackoverflow 上看到的一个方法,亲测可行

    private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl) {
        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);
    }

截图的代码

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        // 获取GLSurfaceView的图片并保存
        if (isTakePicture) {
            Bitmap bmp = createBitmapFromGLSurface(0, 0, mOutputWidth,
                    mOutputHeight, gl);
            isTakePicture = false;
    }

因为onDrawFrame方法会被一直调用,所以我们需要控制一下截图的逻辑条件

 

方法二:

这个方法也是我们项目中用到的一些截取图片的方法,也是可以用的

private ByteBuffer mCaptureBuffer;

@Override
public void onDrawFrame(GL10 arg0) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    surfaceTexture.updateTexImage();
    draw();
    if (isTakePic) {
        mCaptureBuffer.rewind();
        GLES20.glReadPixels(0, 0, mOutputWidth, mOutputHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,
                mCaptureBuffer);
        isTakePic = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                mCaptureBuffer.rewind();
                mBitmap.copyPixelsFromBuffer(mCaptureBuffer);
                String imageName = "Image_" + System.currentTimeMillis() + ".jpg";
                BitmapUtils.saveBitmap(mBitmap, "/sdcard/face/", imageName);
            }
        }).start();
    }
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    mOutputWidth = width;
    mOutputHeight = height;
    mCaptureBuffer = ByteBuffer.allocate(mOutputWidth * mOutputHeight * 4);
    mBitmap = Bitmap.createBitmap(mOutputWidth, height, Config.ARGB_8888);
    GLES20.glViewport(0, 0, width, height);
    mFilter.onOutputSizeChanged(width, height);
}

 

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
要将GLSurfaceView绑定到Camera2,您需要使用TextureView而不是GLSurfaceView。这是因为GLSurfaceView不支持与Camera2 API一起使用。 以下是一些步骤,可以帮助您在Android将TextureView与Camera2 API结合使用: 1. 在布局文件添加一个TextureView: ``` <TextureView android:id="@+id/texture_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 在您的活动类获取TextureView的引用: ``` TextureView textureView = findViewById(R.id.texture_view); ``` 3. 创建一个CameraDevice.StateCallback来处理CameraDevice的状态变化: ``` private CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(@NonNull CameraDevice camera) { // CameraDevice已经打开,可以开始预览 } @Override public void onDisconnected(@NonNull CameraDevice camera) { // CameraDevice已经断开连接 } @Override public void onError(@NonNull CameraDevice camera, int error) { // CameraDevice遇到错误 } }; ``` 4. 获取CameraManager的实例,并使用它来打开相机: ``` CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); String cameraId = cameraManager.getCameraIdList()[0]; // 获取第一个后置摄像头 cameraManager.openCamera(cameraId, cameraStateCallback, null); ``` 5. 在CameraDevice.StateCallback的onOpened方法,设置一个SurfaceTextureListener来处理TextureView的缓冲区更新: ``` @Override public void onOpened(@NonNull CameraDevice camera) { SurfaceTexture surfaceTexture = textureView.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight()); Surface previewSurface = new Surface(surfaceTexture); try { CaptureRequest.Builder previewRequestBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); previewRequestBuilder.addTarget(previewSurface); camera.createCaptureSession(Arrays.asList(previewSurface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { try { CaptureRequest previewRequest = previewRequestBuilder.build(); session.setRepeatingRequest(previewRequest, null, null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { // 配置失败 } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } ``` 6. 在SurfaceTextureListener,创建一个OpenGL ES上下文,并将其与TextureView关联: ``` textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { // 创建OpenGL ES上下文 EGL10 egl = (EGL10) EGLContext.getEGL(); EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); egl.eglInitialize(display, null); int[] attribList = { EGL10.EGL_RED_SIZE, 8, EGL10.EGL_GREEN_SIZE, 8, EGL10.EGL_BLUE_SIZE, 8, EGL10.EGL_ALPHA_SIZE, 8, EGL10.EGL_DEPTH_SIZE, 0, EGL10.EGL_STENCIL_SIZE, 0, EGL10.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, EGL10.EGL_NONE }; EGLConfig[] configs = new EGLConfig[1]; int[] numConfigs = new int[1]; egl.eglChooseConfig(display, attribList, configs, 1, numConfigs); int[] contextAttribList = { EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; EGLContext context = egl.eglCreateContext(display, configs[0], EGL10.EGL_NO_CONTEXT, contextAttribList); // 将OpenGL ES上下文与TextureView关联 EGLSurface eglSurface = egl.eglCreateWindowSurface(display, configs[0], surface, null); egl.eglMakeCurrent(display, eglSurface, eglSurface, context); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }); ``` 注意:上述步骤仅提供了一个基本的示例,用于将TextureView与Camera2 API结合使用。在实际应用,您可能需要根据自己的需求进行修改和定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值