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

问题描述 投票:0回答:1

我正在使用以下资源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以及如何将其链接到SelectLowestResolutionMatchingDisplayAspectRatioZxingHelperAndroid的方法。

c# xamarin.forms xamarin.android delegates zxing.net
1个回答
0
投票

首先在Xamarin.forms演示中声明一个接口IZXingHelper。

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

在Android中创建一个ZXingHelper.cs来实现它。

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

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

    }
}

MainPage.xaml.cs中的用法

 var options = new ZXing.Mobile.MobileBarcodeScanningOptions()
        {
            PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
            CameraResolutionSelector = DependencyService.Get<IZXingHelper>().SelectLowestResolutionMatchingDisplayAspectRatio
        };
© www.soinside.com 2019 - 2024. All rights reserved.