簡體   English   中英

無法從UIKit.UIImage轉換為ZXing.LuminanceSource-Visual Studios 2017中的Xamarin iOS

[英]cannot convert from UIKit.UIImage to ZXing.LuminanceSource - Xamarin iOS in Visual Studios 2017

我看到的所有示例都是針對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;
    }

}

在@Jason的一些幫助下,我設法解決了該問題,謝謝Jason。 我從NuGet下載並安裝了ZXing.Net.Mobile和ZXing.Net.Mobile.Forms。 Xamarin-Visual Studios都需要。 刪除了所有攝像機代碼,並替換為3行代碼,此外,我還需要在button_TouchUpInside調用中添加async代碼。

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

每次都工作(因此很遠)。

暫無
暫無

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

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