Redth / ZXing.Net.Mobile

Barcode Scanner for Xamarin.iOS, Xamarin.Android, UWP and Tizen
MIT License
1.07k stars 702 forks source link

ZXing.Net.Mobile scanner preview strechted #1034

Open FelipeValentim opened 2 years ago

FelipeValentim commented 2 years ago

Scanner preview is strechted and I want to know if there's a workaround for that.

I'm using that to get the best resolution:

var options = new MobileBarcodeScanningOptions()
{
        //TryHarder = true, //Gets or sets a flag which cause a deeper look into the bitmap.
        AutoRotate = false,
        UseFrontCameraIfAvailable = false,
        InitialDelayBeforeAnalyzingFrames = 100,
        CameraResolutionSelector = SelectLowestResolutionMatchingDisplayAspectRatio,
}

private CameraResolution SelectLowestResolutionMatchingDisplayAspectRatio(List<CameraResolution> availableResolutions)
{
        var displayOrientationHeight = DeviceDisplay.MainDisplayInfo.Orientation == DisplayOrientation.Portrait ?                         
        DeviceDisplay.MainDisplayInfo.Height : DeviceDisplay.MainDisplayInfo.Width;
        var displayOrientationWidth = DeviceDisplay.MainDisplayInfo.Orientation == DisplayOrientation.Portrait ?         
        DeviceDisplay.MainDisplayInfo.Width : DeviceDisplay.MainDisplayInfo.Height;

        var targetRatio = displayOrientationHeight / displayOrientationWidth;
        var targetHeight = displayOrientationHeight;

        //camera API lists all available resolutions from highest to lowest, perfect for us
        //making use of this sorting, following code runs some comparisons to select the lowest resolution that matches the 
        screen aspect ratio and lies within tolerance
        //selecting the lowest makes Qr detection actual faster most of the time

        var bestResolutions = from r in availableResolutions
                              let aspectRatio = (double)r.Width / r.Height
                              let aspectRatioDiff = Math.Abs(aspectRatio - targetRatio)
                              let heightDiff = Math.Abs(r.Height - targetHeight)
                              orderby aspectRatioDiff, heightDiff
                              select r;

        return bestResolutions.FirstOrDefault();
}

And when I try to open on android simulator it just crash, though it returns a valid resolution. I got this message: System.ArgumentNullException: 'Value cannot be null. Parameter name: handle'

When I try to open in my device, it works at first, and the image is not strechted, but when I get out of preview and get back, it just crash when I try to open the scanner a second time, even if I kill the app. Can someone help me?

vernetk commented 10 months ago

Hi,

Two links speak about that. https://stackoverflow.com/questions/59922499/how-to-pass-a-method-of-xamarin-android-specific-functionnality-to-a-xamarin-for https://msicc.net/how-to-avoid-a-distorted-android-camera-preview-with-zxing-net-mobile/