通用应用位图中的 ZXing
本文关键字:ZXing 位图 应用 | 更新日期: 2023-09-27 18:36:17
我有一个较旧的 ZXing 代码,它可以很好地与Bitmaps
一起使用,但在我的 Win10 通用应用程序中,我正在使用BitmapImage
。你能给我一个想法,如果ZXing也接受其他BitmapImage
,或者我如何达到它的工作原理?
当我在 Win10 应用程序的类中使用 using System.Drawing;
时,我在编译时收到错误:
System.MarshalByRefObject 未找到
这是我的旧工作代码:
private string DecodeQRCodeFromBitmap(System.Drawing.Bitmap Picture)
{
dynamic luminanceSource = new ZXing.BitmapLuminanceSource(Picture);
dynamic binarizer = new ZXing.Common.HybridBinarizer(luminanceSource);
ZXing.BinaryBitmap bb = new ZXing.BinaryBitmap(binarizer);
ZXing.Result br = readCode2.decode(bb);
if (br != null) {
Interaction.MsgBox(br.ToString);
return Convert.ToString(br.Text);
} else {
return string.Empty;
}
}
新的不起作用的代码:
private string DecodeQRCodeFromBitmap(BitmapImage image)
{
dynamic luminanceSource = new ZXing.BitmapLuminanceSource(image);
dynamic binarizer = new ZXing.Common.HybridBinarizer(luminanceSource);
ZXing.BinaryBitmap bb = new ZXing.BinaryBitmap(binarizer);
ZXing.Result br = readCode.decode(bb);
if (br != null)
{
return Convert.ToString(br.Text);
}
else {
return string.Empty;
}
}
下面是在 Windows 通用应用中解码条形码的小示例。它是 vb.net,但您可以将其转换为 c# ...或者在工作中问我...
Imports Windows.Media.Capture
Imports Windows.Storage.Streams
Imports Windows.Media.MediaProperties
Public NotInheritable Class MainPage
Inherits Page
Private capture As MediaCapture
Private Async Function TakePhoto() As Task
Try
capture = New MediaCapture
Await capture.InitializeAsync
captureElementPreview.Source = capture
Await capture.StartPreviewAsync
Dim res As VideoEncodingProperties = capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview)
Dim iep As ImageEncodingProperties = ImageEncodingProperties.CreateBmp
iep.Height = res.Height
iep.Width = res.Width
Dim barcodereader As New ZXing.BarcodeReader With {.AutoRotate = True}
Dim wb As New WriteableBitmap(res.Width, res.Height)
Dim result As ZXing.Result = Nothing
While result Is Nothing
Using stream = New InMemoryRandomAccessStream()
Await capture.CapturePhotoToStreamAsync(iep, stream)
stream.Seek(0)
Await wb.SetSourceAsync(stream)
result = barcodereader.Decode(wb)
End Using
End While
Scanresult.Text = result.Text
Await capture.StopPreviewAsync
Debug.WriteLine(Scanresult.Text)
Catch ex As Exception
Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString())
End Try
End Function
Private Async Sub buttonCapture_Click(sender As Object, e As RoutedEventArgs)
Await TakePhoto()
End Sub
End Class
您需要从 zxing 库的子文件夹 ''winrt 导入 zxing.winrt.dll。我从堆栈溢出中的另一篇帖子中获取此代码。很多事情可以更好,但它适用于第一步......
问候弗兰克