如何从Windows Phone 7 Emulator中读取QRcode

本文关键字:Emulator 读取 QRcode Phone Windows | 更新日期: 2023-09-27 18:29:53

我正在开发一个简单的QRcode Reader/Generator应用程序。现在我可以生成一个二维码,并通过WP7模拟器进行测试。但我不能测试二维码阅读器。因此,我决定对图像进行硬编码以进行扫描。现在我的问题是,我不知道在哪里加载硬编码的二维码图像到二维码阅读器。

我的二维码阅读器代码是:

    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>

请告诉我,我必须在哪里加载二维码图像才能阅读。

如何从Windows Phone 7 Emulator中读取QRcode

最后我找到了解决方案

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

这里qrImage是硬编码的图像。当我点击按钮时,它会读取二维码图像并显示二维码内容。