I am using the following resource https://msicc.net/how-to-avoid-a-distorted-android-camera-preview-with-zxing-net-mobile/ to solve resoultion destortion of zxing barcode scanner. I arrived at the point where the method SelectLowestResolutionMatchingDisplayAspectRatio is implemented in the android project but I need to pass it to CameraResolutionSelectorDelegate as the author stated. To do that I created an Interface called IZXingHelper which should hold the delegate that I still have no idea how it should be written. Let me share my code snippet and explain where I am facing the issue.
public class ZxingHelperAndroid : IZXingHelper
{
//What code goes here?
public CameraResolution SelectLowestResolutionMatchingDisplayAspectRatio(List<CameraResolution> availableResolutions)
{
CameraResolution result = null;
//a tolerance of 0.1 should not be visible to the user
double aspectTolerance = 0.1;
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;
//calculatiing our targetRatio
var targetRatio = displayOrientationHeight / displayOrientationWidth;
var targetHeight = displayOrientationHeight;
var minDiff = double.MaxValue;
//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
foreach (var r in availableResolutions.Where(r => Math.Abs(((double)r.Width / r.Height) - targetRatio) < aspectTolerance))
{
//slowly going down the list to the lowest matching solution with the correct aspect ratio
if (Math.Abs(r.Height - targetHeight) < minDiff)
minDiff = Math.Abs(r.Height - targetHeight);
result = r;
}
return result;
}
}
and here exactly where I couldn't determine what to write to get it right:
zxing.Options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
CameraResolutionSelector = DependencyService.Get<IZXingHelper>().CameraResolutionSelectorDelegateImplementation
};
public interface IZXingHelper
{
//What code goes here?
}
I don't know how to implement the CameraResolutionSelectorDelegateImplementation in the interface and how to link it to SelectLowestResolutionMatchingDisplayAspectRatio method of ZxingHelperAndroid.
Create an interface IZXingHelper in the Xamarin.forms Demo.
public interface IZXingHelper
{
//CameraResolutionSelectorDelegateImplementation
CameraResolution SelectLowestResolutionMatchingDisplayAspectRatio(List<CameraResolution> availableResolutions);
}
Create ZXingHelper.cs in the .Android project to implement it.
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Essentials;
using ZXing.Mobile;
// Because the assembly dependency decoration is outside of the namespace,
// the namespace "using" must be added or be explicitly prefixed to the
// typeof parameter.
using ScorellViewDemo.Droid;
[assembly: Xamarin.Forms.Dependency(typeof(ZXingHelper))]
namespace ScorellViewDemo.Droid
{
public class ZXingHelper : IZXingHelper
{
public CameraResolution SelectLowestResolutionMatchingDisplayAspectRatio(List<CameraResolution> availableResolutions)
{
CameraResolution result = null;
//a tolerance of 0.1 should not be visible to the user
double aspectTolerance = 0.1;
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;
//calculating our targetRatio
var targetRatio = displayOrientationHeight / displayOrientationWidth;
var targetHeight = displayOrientationHeight;
var minDiff = double.MaxValue;
//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
foreach (var r in availableResolutions.Where(r => Math.Abs(((double)r.Width / r.Height) - targetRatio) < aspectTolerance))
{
//slowly going down the list to the lowest matching solution with the correct aspect ratio
if (Math.Abs(r.Height - targetHeight) < minDiff)
minDiff = Math.Abs(r.Height - targetHeight);
result = r;
}
return result;
}
}
}
Usage in MainPage.xaml.cs
var options = new ZXing.Mobile.MobileBarcodeScanningOptions()
{
PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
CameraResolutionSelector = DependencyService.Get<IZXingHelper>().SelectLowestResolutionMatchingDisplayAspectRatio
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.