Android GLSurfaceView模糊效果

本文探讨了Android中使用GLSurfaceView时遇到的部分手机出现黑屏的问题,并邀请读者分享更好的解决办法,关注GLSurfaceView的显示优化和效果调整。
摘要由CSDN通过智能技术生成
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.opengl.GLException;
import android.opengl.GLSurfaceView;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.opengles.GL10;


public class GLSurfaceViewBlur {

    public interface BlurCallback {
        public void onBlur();
    }

    /**
     * @param context       上下文
     * @param glSurfaceView 底层view控件
     * @param upperView     需要模糊背景的控件
     */
    public static void doBlur(final Context context, final GLSurfaceView glSurfaceView, final ImageView upperView, BlurCallback callback) {
        glSurfaceView.queueEvent(new Runnable() {
            @Override
            public void run() {
                EGL10 egl = (EGL10) EGLContext.getEGL();
                GL10 gl = (GL10) egl.eglGetCurrentContext().getGL();
                Bitmap snapshotBitmap = createBitmapFromGLSurface(0, 0, glSurfaceView.getWidth(), glSurfaceView.getHeight(), gl);
                upperView.post(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(context)
                                .load(snapshotBitmap).apply(new RequestOptions()
                                .transforms(new BlurTransformation(context, 25)))
                                .into(new SimpleTarget<Drawable>() {
                                    @Override
                                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                        upperView.setImageDrawable(resource);
                                        if (callback != null) {
                                            callback.onBlur();
                                        }
                                    }
                                });
                    }
                });
            }
        });


    }

    private static 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 {
 
要将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结合使用。在实际应用中,您可能需要根据自己的需求进行修改和定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值