簡體   English   中英

如何使用 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 以及如何將其鏈接到ZxingHelperAndroid 的SelectLowestResolutionMatchingDisplayAspectRatio 方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM