如何使用 DependencyService 和接口委托将 xamarin.android 特定功能的方法传递给 xamarin.forms?

How to pass a method of xamarin.android specific functionnality to a xamarin.forms using DependencyService and a delegate of Interface?

我正在使用以下资源 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 以及如何 link 它到 SelectLowestResolutionMatchingDisplayAspectRatio ZxingHelperAndroid.

方法

在Xamarin.formsDemo中创建接口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
 };