在Visual Studios 2017中,无法从Uikit.uiimage转换为Zxing.luminancesour



我看到的所有示例都是针对Xamarin形式的,对于Xamarin而言,对于Visual Studios来说,没有任何示例。我有一个正在使用Xamarin开发的iOS应用程序,用于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;
    }
}

我在@Jason的一些帮助下设法解决了问题,谢谢Jason。我从nuget下载并安装了zxing.net.mobile和zxing.net.mobile.forms。您都需要Xamarin -Visual Studios。删除了所有相机代码,并用3行代码替换,此外,我需要将async添加到我的button_touchupinside呼叫中。

在AppDelegate完成:

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

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

最新更新