3

我正在使用以下资源https://msicc.net/how-to-avoid-a-distorted-android-camera-preview-with-zxing-net-mobile/来解决 zxing 条码扫描器的分辨率损坏问题。我到达了在 android 项目中实现方法 SelectLowestResolutionMatchingDisplayAspectRatio 的地方,但我需要将它传递给 CameraResolutionSelectorDelegate,正如作者所说。为此,我创建了一个名为 IZXingHelper 的接口,它应该包含我仍然不知道应该如何编写的委托。让我分享我的代码片段并解释我面临的问题。

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;
        }
    }

在这里,我无法确定要写什么才能正确:

zxing.Options = new ZXing.Mobile.MobileBarcodeScanningOptions
            {
                CameraResolutionSelector = DependencyService.Get<IZXingHelper>().CameraResolutionSelectorDelegateImplementation
            };
public interface IZXingHelper
{
 //What code goes here?
}

我不知道如何在接口中实现CameraResolutionSelectorDelegateImplementation,以及如何将其链接到ZxingHelperAndroid的SelectLowestResolutionMatchingDisplayAspectRatio方法。

4

1 回答 1

3

在 Xamarin.forms Demo 中创建接口 IZXingHelper。

public interface IZXingHelper
{
    //CameraResolutionSelectorDelegateImplementation
    CameraResolution SelectLowestResolutionMatchingDisplayAspectRatio(List<CameraResolution> availableResolutions);
}

在.Android项目中创建ZXingHelper.cs来实现。

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;
    }
  }
}

MainPage.xaml.cs 中的用法

 var options = new ZXing.Mobile.MobileBarcodeScanningOptions()
 {
   PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
   CameraResolutionSelector = DependencyService.Get<IZXingHelper>().SelectLowestResolutionMatchingDisplayAspectRatio
 };
于 2020-01-27T02:54:20.723 回答