zxing不能读取upc

时间:2012-03-23 02:39:42

标签: c# silverlight windows-phone-7 barcode zxing

我正在尝试在Windows手机上实现条形码扫描功能。我设置了一个计时器,每隔一个间隔,我使用照相机api从相机获取一个帧并使用zxing(http://zxingnet.codeplex.com/)对其进行解码。我基本上遵循这两篇文章中的步骤。 http://jonas.follesoe.no/2011/07/22/qr-code-scanning-on-windows-phone-75-using-zxlib/  http://www.m0sand.com/henningms/?p=324 但问题是,对于qr代码,应用程序可以非常快速地解码。但对于upc代码,解码需要很长时间,而且很多时候,它不会返回任何结果。以下是我的代码。 这是xaml文件:

<phone:PhoneApplicationPage 
    x:Class="Shopping.View.BarcodeScan"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <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>
        <!--
        <Rectangle x:Name="_scanViewTop"
                   Margin="0"
                   Height="200"
                   Width="600"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Top"
                   Fill="Black" />
        <Rectangle x:Name="_scanViewBottom"
                   Margin="0"
                   Height="200"
                   Width="600"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Bottom"
                   Fill="Black" />
        -->
        <!--<ListBox Margin="10" x:Name="_matchesList" FontSize="30" FontWeight="ExtraBold" />-->
    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using System.Windows.Navigation;
using Microsoft.Devices;
using Shopping.Helper;
using ZXing;
using ZXing.Common;

namespace Shopping.View
{
    public partial class BarcodeScan : PhoneApplicationPage
    {
        private readonly DispatcherTimer _timer;

        private PhotoCameraLuminanceSource _luminance;
        private MultiFormatReader _reader;
        private PhotoCamera _photoCamera;

        public BarcodeScan()
        {
            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 MultiFormatReader();
            var formats = new List<BarcodeFormat> { BarcodeFormat.QR_CODE, BarcodeFormat.EAN_13, BarcodeFormat.EAN_8, BarcodeFormat.UPC_A };
            var hints = new Dictionary<DecodeHintType, Object>();
            hints.Add(DecodeHintType.POSSIBLE_FORMATS, formats);
            hints.Add(DecodeHintType.TRY_HARDER, true);
            _reader.Hints = hints;

            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.decodeWithState(binBitmap);
                if (result != null)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show(result.Text);
                    });
                }
            }
            catch
            {
            }
        }
    }
}

有什么想法吗?非常感谢!

0 个答案:

没有答案