【问题标题】:QR Scan on Windows Phone 8Windows Phone 8 上的 QR 扫描
【发布时间】:2014-01-25 14:25:16
【问题描述】:

一段时间以来,我一直在努力寻找一个不错的教程,介绍如何使用 Windows Phone 8 扫描二维码。不幸的是,所有这些(至少我发现的那些,这是一个很大的负担)都是为 WP7 设计的并没有工作。

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

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

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

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

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

我把它作为最后的手段,我真的希望有人有一个示例代码或者我应该如何处理这个的想法

【问题讨论】:

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


    【解决方案1】:

    使用 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
        {
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 2016-08-24
    相关资源
    最近更新 更多