从 Universal Windows 应用程序中的图像解码条码
Decode Barcode from Image in Universal Windows App
我在 windows 平板电脑 (windows 10) 上实现了一个通用的 windows 应用程序。
这个平板电脑有摄像头,我想拍下二维码并解码。
所以我试过 Zxing.net,但不适用于通用 windows 应用程序。
有一种方法可以解码照片中的二维码。
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(size.Width, size.Height);
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
//QR code conversion from jepg and return string.
}
我访问了一些链接...例如:
https://github.com/Redth/ZXing.Net.Mobile
和
https://archive.codeplex.com/?p=zxingnet#trunk/Clients/WindowsRTDemo/Package.appxmanifest
但通用 windows 应用程序没有任何作用。
解决方案?
But nothing work for universal windows app
通过我这边的测试,你链接的 sample 确实有效。
由于您没有提供解码条形码的代码片段,因此我提供了可以在我这边正常运行的整个项目,您可以测试并尝试找出您的代码片段有什么问题。
首先安装 ZXing.uwp
nuget 包,并使用以下代码片段进行测试。
XAML
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Image x:Name="imgshow" Height="150" Width="150"></Image>
<TextBox x:Name="txtDecoderType" Height="50" Width="250"></TextBox>
<TextBox x:Name="txtDecoderContent" Height="50" Width="250"></TextBox>
<Button x:Name="btnscan" Click="btnscan_Click" Content=" scan"></Button>
</StackPanel>
代码隐藏
private async void btnscan_Click(object sender, RoutedEventArgs e)
{
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(size.Width, size.Height);
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
//QR code conversion from jepg and return string.
WriteableBitmap writeableBitmap;
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
writeableBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
writeableBitmap.SetSource(fileStream);
imgshow.Source = writeableBitmap;
}
// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();
// detect and decode the barcode inside the writeableBitmap
var barcodeReader = new BarcodeReader
{
AutoRotate = true,
Options = { TryHarder = true }
};
Result result = reader.Decode(writeableBitmap);
// do something with the result
if (result != null)
{
txtDecoderType.Text = result.BarcodeFormat.ToString();
txtDecoderContent.Text = result.Text;
}
}
}
此外,从1803开始,UWP还提供了条码扫描器的API,详情请参考this tutorial and the official sample.
我在 windows 平板电脑 (windows 10) 上实现了一个通用的 windows 应用程序。 这个平板电脑有摄像头,我想拍下二维码并解码。
所以我试过 Zxing.net,但不适用于通用 windows 应用程序。 有一种方法可以解码照片中的二维码。
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(size.Width, size.Height);
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
//QR code conversion from jepg and return string.
}
我访问了一些链接...例如: https://github.com/Redth/ZXing.Net.Mobile 和 https://archive.codeplex.com/?p=zxingnet#trunk/Clients/WindowsRTDemo/Package.appxmanifest
但通用 windows 应用程序没有任何作用。 解决方案?
But nothing work for universal windows app
通过我这边的测试,你链接的 sample 确实有效。
由于您没有提供解码条形码的代码片段,因此我提供了可以在我这边正常运行的整个项目,您可以测试并尝试找出您的代码片段有什么问题。
首先安装 ZXing.uwp
nuget 包,并使用以下代码片段进行测试。
XAML
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Image x:Name="imgshow" Height="150" Width="150"></Image>
<TextBox x:Name="txtDecoderType" Height="50" Width="250"></TextBox>
<TextBox x:Name="txtDecoderContent" Height="50" Width="250"></TextBox>
<Button x:Name="btnscan" Click="btnscan_Click" Content=" scan"></Button>
</StackPanel>
代码隐藏
private async void btnscan_Click(object sender, RoutedEventArgs e)
{
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(size.Width, size.Height);
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
//QR code conversion from jepg and return string.
WriteableBitmap writeableBitmap;
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
writeableBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
writeableBitmap.SetSource(fileStream);
imgshow.Source = writeableBitmap;
}
// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();
// detect and decode the barcode inside the writeableBitmap
var barcodeReader = new BarcodeReader
{
AutoRotate = true,
Options = { TryHarder = true }
};
Result result = reader.Decode(writeableBitmap);
// do something with the result
if (result != null)
{
txtDecoderType.Text = result.BarcodeFormat.ToString();
txtDecoderContent.Text = result.Text;
}
}
}
此外,从1803开始,UWP还提供了条码扫描器的API,详情请参考this tutorial and the official sample.