Android CameraSource 调整大小预览
Android CameraSource resize preview
我有这个代码:
surfaceView = (SurfaceView)findViewById(R.id.camera_view);
textView = (TextView)findViewById(R.id.code_info);
barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource = new CameraSource.Builder(this, barcodeDetector).setRequestedPreviewSize( 800, 1280).build();
我尝试更改 "setRequestedPreviewSize" 但我不能...,我该怎么做?
抱歉英语不好,我是意大利人
如果你能提供更多的代码,我可以告诉你更多,但问题可能是你想要设置设备相机不支持的预览尺寸。
您可以通过名为 getOptimalPreviewSize 的 Google API 方法来决定预览大小。
https://android.googlesource.com/platform/development/+/1e7011fcd5eee3190b436881af33cd9715eb8d36/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.java
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (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 (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
您可以通过此获取支持的预览尺寸
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
然后将此参数连同宽度和高度一起传递给上述方法,它将为您提供最佳预览尺寸,设备的相机也支持。
我有这个代码:
surfaceView = (SurfaceView)findViewById(R.id.camera_view);
textView = (TextView)findViewById(R.id.code_info);
barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource = new CameraSource.Builder(this, barcodeDetector).setRequestedPreviewSize( 800, 1280).build();
我尝试更改 "setRequestedPreviewSize" 但我不能...,我该怎么做?
抱歉英语不好,我是意大利人
如果你能提供更多的代码,我可以告诉你更多,但问题可能是你想要设置设备相机不支持的预览尺寸。
您可以通过名为 getOptimalPreviewSize 的 Google API 方法来决定预览大小。 https://android.googlesource.com/platform/development/+/1e7011fcd5eee3190b436881af33cd9715eb8d36/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.java
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (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 (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
您可以通过此获取支持的预览尺寸 mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
然后将此参数连同宽度和高度一起传递给上述方法,它将为您提供最佳预览尺寸,设备的相机也支持。