【发布时间】: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