我有这段代码:
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”但我不能......,我该怎么做?
抱歉英语不好,我是意大利人答案 0 :(得分:1)
如果您可以提供更多代码,我可以告诉您更多信息,但问题可能是您要设置设备相机不支持的预览尺寸。
您可以通过Google API方法决定预览尺寸,称为getOptimalPreviewSize。 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();
然后将此参数以及宽度和高度传递给上面的方法,它将为您提供最佳的预览尺寸,也可以由设备的相机支持。