Alessandro Zago
Alessandro Zago

Reputation: 803

Android CameraSource resize preview

i have this code :

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();

i try to change the "setRequestedPreviewSize" after but i can't... , how can i do it?

sorry for bad english, i'm italian

Upvotes: 1

Views: 3365

Answers (1)

Anil
Anil

Reputation: 553

I can tell you more if you can provide more of the code, but the problem might be that you want to set a preview size that is not supported by the device's camera.

You can decide on a preview size from the Google API method, called 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;
}

You can get the supported preview sizes with this mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();

Then pass this parameter along with the width and height to the method above, and it will give you the best preview size, also supported by the device's camera.

Upvotes: 1

Related Questions