我正在我的项目中实现Camera 2 API.我正在使用TextureView和这些代码行设置相机全屏预览大小:
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
这似乎是设备支持的最大预览尺寸.我不确定这个尺寸是否适用于所有设备并且适合其设备的纵横比而不会被拉伸.有人知道吗?
解决方法
如果您的相机分辨率,纹理视图和设备的显示尺寸不同,则必须调整尺寸.为此,您必须将TextureView放在FrameLayout中.以下代码适用于具有各种显示分辨率的所有设备.
如果要全屏预览,请选择显示尺寸.获取int DSI_height,int DSI_width全局变量.
DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); DSI_height = displayMetrics.heightPixels; DSI_width = displayMetrics.widthPixels;
从Camera2 API中选择所需的分辨率并指定大小imageDimension,全局获取私有大小imageDimension并使用
setAspectRatioTextureView(imageDimension.getHeight(),imageDimension.getWidth());
并使用以下逻辑
private void setAspectRatioTextureView(int ResolutionWidth,int ResolutionHeight ) { if(ResolutionWidth > ResolutionHeight){ int newWidth = DSI_width; int newHeight = ((DSI_width * ResolutionWidth)/ResolutionHeight); updateTextureViewSize(newWidth,newHeight); }else { int newWidth = DSI_width; int newHeight = ((DSI_width * ResolutionHeight)/ResolutionWidth); updateTextureViewSize(newWidth,newHeight); } } private void updateTextureViewSize(int viewWidth,int viewHeight) { Log.d(TAG,"TextureView Width : " + viewWidth + " TextureView Height : " + viewHeight); textureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth,viewHeight)); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。