【问题标题】:cannot convert from UIKit.UIImage to ZXing.LuminanceSource - Xamarin iOS in Visual Studios 2017无法从 UIKit.UIImage 转换为 ZXing.LuminanceSource - Visual Studios 2017 中的 Xamarin iOS
【发布时间】:2017-12-19 21:07:29
【问题描述】:

我看到的所有示例都是针对 Xamarin Forms 的,没有针对 Xamarin for Visual Studios 的示例。我有一个使用 Xamarin for Visual Studio 开发的 iOS 应用程序,需要读取条形码。我从 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;
    }

}

【问题讨论】:

  • 您正在对条码拍照,然后尝试对其进行解码?为什么不让 ZXing 为您完成所有这些工作?
  • 嗯,我以为我是。我只是按照他们网站上给出的示例进行操作。那我该怎么办?
  • 看看github.com/Redth/ZXing.Net.Mobile - 第一个用法示例展示了如何使用 2-3 行代码进行扫描
  • 我正在为 iPad 编写代码,我认为这很重要,不是吗?我正在使用 ZXing.net。
  • 不,没关系。中兴网会处理的

标签: xamarin xamarin.ios visual-studio-2017 zxing zbar


【解决方案1】:

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

每次都有效(到目前为止)。

【讨论】:

    猜你喜欢
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2017-12-20
    • 1970-01-01
    相关资源
    最近更新 更多