如何从Windows Phone 7模拟器中读取QRcode

时间:2014-11-10 06:31:11

标签: c# windows-phone-7 zxing qr-code

我正在开发一个简单的QRcode阅读器/生成器应用程序。现在我可以生成QR码,并从WP7模拟器进行测试。但我无法测试QR码阅读器。所以我决定硬编码图像进行扫描。现在我的问题是,我不知道在哪里将硬编码的QR图像加载到QR码阅读器。

我的QR码阅读器代码是:

    private PhotoCamera _phoneCamera;
    private IBarcodeReader _barcodeReader;
    private DispatcherTimer _scanTimer;
    private WriteableBitmap _previewBuffer;


    public QRCodeScanner()
    {
        InitializeComponent();

    }
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        _phoneCamera = new PhotoCamera();
        _phoneCamera.Initialized += cam_Initialized;
        CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed;
        viewfinderBrush.SetSource(_phoneCamera);

        _scanTimer = new DispatcherTimer();
        _scanTimer.Interval = TimeSpan.FromMilliseconds(250);
        _scanTimer.Tick += (o, arg) => ScanForBarcode();

        base.OnNavigatedTo(e);
    }

    void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e)
    {
        _phoneCamera.Focus();
    }

    protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
    {
        _scanTimer.Stop();

        if (_phoneCamera != null)
        {
            _phoneCamera.Dispose();
            _phoneCamera.Initialized -= cam_Initialized;
            CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed;
        }
    }

    void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e)
    {
        if (e.Succeeded)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                _phoneCamera.FlashMode = FlashMode.Off;
                _previewBuffer = new WriteableBitmap((int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height);
                _barcodeReader = new BarcodeReader();
                _barcodeReader.TryHarder = true;
                _barcodeReader.ResultFound += _bcReader_ResultFound;
                _scanTimer.Start();
            });
        }
        else
        {
            Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show("Unable to initialize the camera");
            });
        }
    }

    void _bcReader_ResultFound(Result obj)
    {
        if (!obj.Text.Equals(tbBarcodeData.Text))
        {
            VibrateController.Default.Start(TimeSpan.FromMilliseconds(100));
            tbBarcodeType.Text = obj.BarcodeFormat.ToString();
            tbBarcodeData.Text = obj.Text;
        }
    }

    private void ScanForBarcode()
    {
        _phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
        _previewBuffer.Invalidate();
        _barcodeReader.Decode(_previewBuffer);

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        //Here i want to scan the hard coded QR code. 
    }

我的XAML: -

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="2,0,2,0">
        <Canvas x:Name="viewfinderCanvas" Height="372" VerticalAlignment="Top">
            <Canvas.Background>
                <VideoBrush x:Name="viewfinderBrush">
                    <VideoBrush.RelativeTransform>
                        <CompositeTransform
                        x:Name="viewfinderTransform"
                        CenterX="0.5"
                        CenterY="0.5"
                        Rotation="90"/>
                    </VideoBrush.RelativeTransform>
                </VideoBrush>
            </Canvas.Background>
        </Canvas>
        <Image Height="232" HorizontalAlignment="Left" Margin="173,381,0,0" Name="qrImage" Stretch="Fill" VerticalAlignment="Top" Width="296" Source="/NewExample;component/Images/qrcode.png" />
        <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="6,546,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />

        <StackPanel Margin="0,0,0,0" Height="150" VerticalAlignment="Bottom">
            <TextBlock x:Name="tbBarcodeType" FontWeight="ExtraBold" />
            <TextBlock x:Name="tbBarcodeData" FontWeight="ExtraBold" TextWrapping="Wrap" />
        </StackPanel>           
    </Grid>

请让我知道,我必须加载QR图像才能阅读。

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案

private void button1_Click(object sender, RoutedEventArgs e)
        {
            var img= new WriteableBitmap((BitmapSource)qrImage.Source);
            _barcodeReader.Decode(img);
        }

此处qrImage是硬编码的图像。当我点击按钮它读取QR码图像并显示QR码内容。