在拍照预览页面,预览照片的拉伸问题主要与下面两个因素有关:
1,SurfaceProvider的大小和 Camera中的Preview的大小
手机camera的尺寸大小为25601920(横屏,比例为:1.333)预览尺寸大小为640480(横屏,比例为1.333)
手机SurfaceProvider大小为1280720(横屏,比例为:1.777)预览尺寸大小为960720(横屏,比例为1.777)
2,SurfaceProvider的宽高比例跟camera preview的宽高比例不一样才会导致打开camera后,照相机会出现拉伸的情况。
解决的方法就是计算SurfaceProvider尺寸比例跟camera预览尺寸比例相同。
需要在该回调方法中进行处理:
class SurfaceCallBack implements SurfaceOps.Callback {
@Override
public void surfaceCreated(SurfaceOps callbackSurfaceOps) {
if (callbackSurfaceOps != null) {
//在这里重新设置宽高
callbackSurfaceOps.setFixedSize(SCREEN_WIDTH,SCREEN_HEIGHT );
}
openCamera();
}
@Override
public void surfaceChanged(SurfaceOps callbackSurfaceOps, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceOps callbackSurfaceOps) {}
}
方案一:简单设置
在SurfaceCallBack中设callbackSurfaceOps.setFixedSize(height,width);
这里宽高与手机预览尺寸的宽高是反向比例设置的,这样就能大幅改善拉伸问题。
方案二:精确计算
可以大幅度减少误差,更精确的处理比例不匹配的问题。
callbackSurfaceOps.setFixedSize (size.width,size.height)
private Size getOptimalSize(CameraKit cameraKit, String camId, int screenWidth, int screenHeight) {
List<Size> sizes = cameraKit.getCameraAbility(camId).getSupportedSizes(ImageFormat.YUV420_888);
final double ASPECT_TOLERANCE = 0.1;
//竖屏screenHeight/screenWidth,横屏是screenWidth/screenHeight
double targetRatio = (double) screenHeight / screenWidth;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = screenWidth;
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
也可以看这篇文章参考:
https://blog.csdn.net/SYIF88/article/details/117257923?spm=1001.2014.3001.5501