我一直试图弄清楚一段时间,但由于某种原因,当我开始使用相机录制视频时,预览放大.我从以下示例中获取以下代码:
@Override public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) { Camera.Parameters myParameters = mCamera.getParameters(); List<Camera.Size> sizes = myParameters.getSupportedPreviewSizes(); Camera.Size myBestSize = getBestPreviewSize(sizes,width,height); if (myBestSize != null) { myParameters.setPreviewSize(myBestSize.width,myBestSize.height); myParameters.setVideoStabilization(false); mCamera.setParameters(myParameters); mCamera.startPreview(); mCamera.unlock(); } } private Camera.Size getBestPreviewSize(List<Camera.Size> sizes,int height) { final double ASPECT_TOLERANCE = 0.05; double targetRatio = (double) width / height; if (sizes == null) return null; Camera.Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = height; // Find 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); } } 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; }
但录制视频时,预览仍会放大.我尝试手动将预览大小设置为1280×720,但是它在我测试的所有设备上都不起作用,有些显示黑色的SurfaceView.
录制视频时有什么办法阻止预览放大吗? StackOverflow中的这些解决方案都不会似乎有效.
编辑:我尝试使用自己的onMesaure方法创建了一个自定义的SurfaceView,但是当我启动MediaRecorder时,我收到一个-19错误. (不顾赏金中的-16)
解决方法
我对您所谈论的缩放类型感到困惑,可能是一个软件缩放,或者可能是自动对焦,所以我会提出不同的潜在提示,导致最终的解决方案:
>实现mCamera.setZoomChangeListener,例如:
mCamera.setZoomChangeListener(new Camera.OnZoomChangeListener() { @Override public void onZoomChange(int i,boolean b,Camera camera) { Log.e("StackOverflow","Zoom just changed !"); // Inspect things from here } });
>禁用平滑缩放:
mCamera.stopSmoothZoom(); // My device doesn't support it,but maybe your device does
>检查所有的相机Camera.Parameters:
Log.e("StackOverflow","All Camera parameters :"); for(String s : mCamera.getParameters().flatten().split(";")) Log.e("StackOverflow",s + "\n");
>然后切换可以缩放相关的任何东西,例如:
Camera.Parameters parameters = mCamera.getParameters(); parameters.setZoom(100); // My device support some values in the range 100 | 400 parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); parameters.setVideoStabilization(false); // Don't forget to add the new parameters to the camera : mCamera.setParameters(parameters);
问候