Android Camera开发入门 第七篇: Camera2相机 预览功能实现

本课程内容由 @小驰笔记 出品,欢迎关注,获取更多交流信息~
欢迎访问个人博客:www.xiaochibiji.com
显示需要借助surface,一般采用surfaceview或者textureview;
相关重要流程:

1、自定义TextureView

private AutoFitTextureView mTextureView;

2、Activity OnResume的时候,判断mTextureView 当前状态。如果是available状态,则直接打开camera,否则设置surfaceTexture监听

if (mTextureView.isAvailable()) {            openCamera(mTextureView.getWidth(), mTextureView.getHeight()); } else {            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); }

3、surfaceTexture监听中,SurfaceTextureAvailable的时候,去执行opencamera的操作

privatefinal TextureView.SurfaceTextureListener mSurfaceTextureListener            = new TextureView.SurfaceTextureListener() {        @Override        public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {            openCamera( width, height);        }        @Override        public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {            configureTransform(width, height);        }        @Override        public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {            returntrue;        }        @Override        public void onSurfaceTextureUpdated(SurfaceTexture texture) {        }    };

4、设置surface target

Surface surface = new Surface(texture);// We set up a CaptureRequest.Builder with the output Surface.mPreviewRequestBuilder		= mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);mPreviewRequestBuilder.addTarget(surface);

5、 创建会话中,传入surface

mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),                    new CameraCaptureSession.StateCallback() {}

相关视频持续录制中…

推荐阅读:

博客大更新_

关于我

今年,做好一件事情

一篇文章带你了解Android 最新Camera框架

深圳上班,

从事Android Camera相关软件开发工作,

记录生活和工作的点滴,

点击关注“小驰笔记”,期待和你相遇~

posted @ 2022-09-01 23:02  小驰行动派  阅读(16)  评论(0编辑  收藏  举报  来源