简体   繁体   English

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

[英]How to read QRcode from Windows Phone 7 Emulator

I am developing a simple QRcode Reader/Generator app. 我正在开发一个简单的QRcode读取器/生成器应用程序。 Now I Can generate a QR code, and tested from WP7 emulator. 现在,我可以生成QR码,并通过WP7仿真器进行了测试。 But I can not test QR code reader. 但是我无法测试QR码阅读器。 So that I decided to hard code the image for scanning. 因此,我决定对图像进行硬编码以进行扫描。 Now my problem is, I don't know where to load the hard coded QR image to QR Code Reader. 现在我的问题是,我不知道将硬编码QR图像加载到QR Code Reader的位置。

My QR Code Reader Code is: 我的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. 
    }

My XAML:- 我的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>

Please let me know, Where i have to load the QR image to read . 请让我知道,我必须在哪里加载QR图像以进行阅读。

Finally I found the Solution 终于我找到了解决方案

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

Here qrImage is the hardcoded Image. 这里qrImage是硬编码的图像。 When i click the button It read the QR code Image and shows the QR code content. 当我单击按钮时,它将读取QR码图像并显示QR码内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM