2016-02-13 72 views
0

我有这样的代码:的Android CameraSource调整预览

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

我可以告诉你更多,如果你能提供更多的代码,但问题可能是你想要设置不被支持的预览大小设备的相机。

您可以使用名为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()。

然后将此参数与宽度和高度一起传递给上述方法,它将为您提供最佳预览尺寸,同时也支持设备的相机。