简体   繁体   English

相机预览显示在android中受到挤压

[英]Camera Preview showing squeezed in android

My intent is to have a square Camera Preview. 我的目的是拥有一个方形的Camera Preview。

I do not have a 1:1 ratio Camera preview available (I am in an Nexus 5 emulator), so my optimal size available is 640 x 480, so to accommodate a square preview I am setting the SurfaceView size to be equal to the aspect ratio but offsetting the margin offscreen by half the difference to leave only the cropped square on screen. 我没有1:1的摄像头预览功能(我在Nexus 5仿真器中),因此我的最佳可用尺寸为640 x 480,因此为了容纳正方形预览,我将SurfaceView尺寸设置为与宽高比相等比例,但将屏幕外的边距偏移一半的差异,从而仅在屏幕上保留裁切后的正方形。 The surface view itself is correctly sized and offset, but the camera preview itself is still displaying the squeezed preview within the square instead of displaying partially offscreen in the correct aspect ratio. 曲面视图本身的大小和偏移正确无误,但相机预览本身仍在正方形内显示压缩的预览,而不是以正确的纵横比部分显示在屏幕外。

Any thoughts? 有什么想法吗? Or better yet, call me stupid and point me in the direction of a better solution. 或者更好的是,称我为愚蠢的人,并指出更好的解决方案。

Code to set the size of the SurfaceView 设置SurfaceView大小的代码

Camera.Parameters parameters = camera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, imageHolder.getMeasuredWidth(), imageHolder.getMeasuredHeight());

parameters.setPreviewSize(optimalSize.width, optimalSize.height);

int screenWidth = imageHolder.getMeasuredWidth();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(surfaceView.getMeasuredWidth(), surfaceView.getMeasuredHeight());

int ratio = screenWidth / optimalSize.height;

params.width = optimalSize.width * ratio;
int offset = params.width - screenWidth;
if(offset > 0) {
   offset = offset/2;
}

params.setMargins(-offset,0,0,0);
surfaceView.setLayoutParams(params);
camera.setParameters(parameters);

camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();

This is how the preview looks currently 这是当前预览的外观

相机预览受到挤压

This is how the photo looks in the same sized square after its captured 这就是照片拍摄后在相同大小的正方形中的样子

在此处输入图片说明

I think the easiest solution is just to mask out those parts of the camera preview which you don't want to see. 我认为最简单的解决方案是掩盖您不想看到的摄像机预览部分。 Just put a view on top. 只需将视图放在顶部即可。 Other way would be to do some realtime image processing, which will require you to deal with the NDK. 其他方法是进行一些实时图像处理,这将需要您处理NDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM