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.