解决android 自定义camera变形的问题
程序员文章站
2022-05-08 19:43:19
...
主要是因为surfaceview的显示大小和camera预览Size不匹配导致的。
下面这一段,我也是借鉴别人然后修改传入的surfaceview宽高位置就可以了。
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();//获取所有支持的camera尺寸
Log.d("jxd","optionSize : mSurfaceView "+mSurfaceView.getWidth()+" * "+mSurfaceView.getHeight());
Camera.Size optionSize = getOptimalPreviewSize(sizeList, mSurfaceView.getHeight(), mSurfaceView.getWidth());//获取一个最为适配的camera.size
Log.d("jxd","optionSize : "+optionSize.width+" * "+optionSize.height);
parameters.setPreviewSize(optionSize.width,optionSize.height);//把camera.size赋值到parameters
mCamera.setParameters(parameters);
//通过SurfaceView显示预览
mCamera.setPreviewDisplay(mHolder);
//开始预览
mCamera.startPreview();
寻找合适的size
/**
* 解决预览变形问题
* @param sizes
* @param w
* @param h
* @return
*/
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
推荐阅读
-
解决自定义$(id)的方法与jquery选择器$冲突的问题
-
解决Android SearchView不显示搜索icon的问题
-
Android修改源码解决Alertdialog触摸对话框边缘消失的问题
-
Android中自定义的dialog中的EditText无法弹出输入法解决方案
-
Android view更改背景资源与padding消失的问题解决办法
-
Vue中android4.4不兼容问题的解决方法
-
Springboot解决ajax+自定义headers的跨域请求问题
-
Android studio 下的APK打包失败问题解决办法
-
Android的App启动时白屏的问题解决办法
-
Android Studio怎么解决导入项目的中文注释乱码的问题?