无法从UIKit.UIImage转换为ZXing.LuminanceSource - 2017年Visual Studio中的Xamarin iOS

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

我看到的所有例子都是针对Xamarin Forms,对于Xamarin来说,Visual Studios没有任何内容。我有一个iOS应用程序我正在使用Xamarin开发Visual Studio并需要读取条形码。我从NuGet将ZBar下载到Visual Studio 2017并安装它,没问题。我访问相机并拍摄图像(条形码),没问题。但是,似乎无法将从相机捕获的UIKit.UIIMage转换为“ZXing.LuminanceSource”,因此可以对其进行解码。如果有人可以帮我指出正确的方向,我会很感激。我所拥有的代码相当简单,取自下载中包含的ZBar示例:

IBarcodeReader scanPage = new BarcodeReader();

var result = scanPage.Decode(theImage); // the image is public and is set to the image returned by the camera.  It's here I get the error in intellisense "cannot convert from UIKit.UIImage to ZXing.LuminanceSource"

相机图片返回码:

    [Foundation.Export("imagePickerController:didFinishPickingImage:editingInfo:")]
    public void FinishedPickingImage(UIKit.UIImagePickerController picker, UIKit.UIImage image, Foundation.NSDictionary editingInfo)
    {
        theImage = MaxResizeImage(image, 540f, 960f);
        picker.DismissModalViewController(false);

    }

    [Foundation.Export("imagePickerControllerDidCancel:")]
    public void Canceled(UIKit.UIImagePickerController picker)
    {
        DismissViewController(true, null);
    }

    public static UIImage MaxResizeImage(UIImage sourceImage, float maxWidth, float maxHeight)
    {
        var sourceSize = sourceImage.Size;
        var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
        if (maxResizeFactor > 1) return sourceImage;
        var width = maxResizeFactor * sourceSize.Width;
        var height = maxResizeFactor * sourceSize.Height;
        UIGraphics.BeginImageContext(new CGSize((nfloat)width, (nfloat)height));
        sourceImage.Draw(new CGRect(0, 0, (nfloat)width, (nfloat)height));
        var resultImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        return resultImage;
    }

}
xamarin xamarin.ios visual-studio-2017 zxing zbar
1个回答
0
投票

我设法在@Jason的帮助下解决了这个问题,感谢杰森。我从NuGet下载并安装了ZXing.Net.Mobile和ZXing.Net.Mobile.Forms。你需要Xamarin - Visual Studios。删除了所有相机代码并替换为3行代码,另外我需要将async添加到我的button_TouchUpInside调用中。

在AppDelegate FinishedLaunching中:

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        // Override point for customization after application launch.
        // If not required for your application you can safely delete this method

        // Add this line to initialize ZXing

        ZXing.Net.Mobile.Forms.iOS.Platform.Init();

        return true;
    }

将按钮代码更改为此,将按钮async更改为await扫描结果:

    async partial void ScanBarCode_TouchUpInside(UIButton sender)
    {
       // Create scanner
        var scanner = new ZXing.Mobile.MobileBarcodeScanner();

        // Store result of scan in var result, need to await scan
        var result = await scanner.Scan();

        // display bar code number in text field
        Textbox_BarCode.Text = result.Text;
    }

每次都工作(到目前为止)。

© www.soinside.com 2019 - 2024. All rights reserved.