无法从 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 的。我有一个 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"
相机图片return代码:
[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 AND 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;
}
每次都有效(到目前为止)。
我看到的所有示例都是针对 Xamarin Forms 的,没有针对 Xamarin for 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"
相机图片return代码:
[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 AND 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;
}
每次都有效(到目前为止)。