c# - Windows Phone 8 上的二维码扫描

标签 c# windows-phone-8 qr-code barcode-scanner scanning

一段时间以来,我一直在努力寻找一个关于如何使用 Windows Phone 8 生成 QR 码的不错的教程。不幸的是,所有这些(至少我找到的那些,这是一个很大的负担)都是为 WP7 而设计的但没有用。

其中一些需要一个 PhotoLuminance 对象,这在 ZXing.net 库中不可用。

我认为我最大的问题是我不知道如何从相机中检索 ImageStream,在旅途中然后每隔一秒左右扫描一次。

我需要一些流畅的东西,而不必启动相机任务:)。

现在,我正在使用 VideoBrush 组件将图像捕捉到一个矩形中,因此可以从相机中检索数据。

camera = new PhotoCamera(CameraType.Primary);
viewfinderBrush.SetSource(camera);

我将此作为最后的手段,我真的希望有人有示例代码或关于我应该如何处理此问题的想法

最佳答案

使用 ZXing.Net 库尝试下面给出的代码。

XAML

<Grid x:Name="grdCamera">
    <Rectangle x:Name="_previewRect" 
           Margin="0" 
           Height="800" 
           Width="600" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center">
        <Rectangle.Fill>
            <VideoBrush x:Name="_previewVideo">
                <VideoBrush.RelativeTransform>
                    <CompositeTransform  
                    x:Name="_previewTransform" CenterX=".5" CenterY=".5" />
                </VideoBrush.RelativeTransform>
            </VideoBrush>
        </Rectangle.Fill>
    </Rectangle>
</Grid>

C#

private readonly DispatcherTimer _timer;
private PhotoCameraLuminanceSource _luminance;
private QRCodeReader _reader;
private PhotoCamera _photoCamera;

//Constructor
public ScanPage()
{
    InitializeComponent();

    _timer = new DispatcherTimer();
    _timer.Interval = TimeSpan.FromMilliseconds(250);
    _timer.Tick += (o, arg) => ScanPreviewBuffer();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    _photoCamera = new PhotoCamera();
    _photoCamera.Initialized += OnPhotoCameraInitialized;
    _previewVideo.SetSource(_photoCamera);

    CameraButtons.ShutterKeyHalfPressed += (o, arg) => _photoCamera.Focus();

    base.OnNavigatedTo(e);
}

private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
{
    int width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
    int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);

    _luminance = new PhotoCameraLuminanceSource(width, height);
    _reader = new QRCodeReader();

    Dispatcher.BeginInvoke(() =>
    {
        _previewTransform.Rotation = _photoCamera.Orientation;
        _timer.Start();
    });
}

private void ScanPreviewBuffer()
{
    try
    {
        _photoCamera.GetPreviewBufferY(_luminance.PreviewBufferY);
        var binarizer = new HybridBinarizer(_luminance);
        var binBitmap = new BinaryBitmap(binarizer);
        var result = _reader.decode(binBitmap);
        Dispatcher.BeginInvoke(() => MessageBox.Show(result.Text));
    }
    catch
    {
    }
}

关于c# - Windows Phone 8 上的二维码扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351732/

相关文章:

javascript - 如何让二维码阅读器识别我生成的二维码?

java - ZXing QR 扫描仪在第二次从手机运行后无法运行

c# - 如何创建带有 "Yes"、 "No"选项和 DialogResult 的消息框?

javascript - cordova phonegap wp8 svg 在图像标签中损坏

c# - 为什么对字典使用隐式集合初始值设定项会导致 C# 中出现运行时错误?

c# - 在 WP8 中支持 XNA?

c# - 如何在 Google Drive API 中取消上传?

php - php 中的二维码扫描结果

c# - 在单个数据表中合并数据集表

c# - Session_Start 在 Chrome 中被调用得太频繁