Questions about decoding UPC

Mar 21, 2012 at 6:48 PM

Hi Micheal, I'm trying to use your library to decode qr code and upc code. The qr code can be decoded easily, however, for upc code, most of the time, I can't get any result. Following are some code pieces I use for decoding: 

var multiReader = 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);
 
multiReader.Hints = hints;
 
multiReader.decodeWithState(binBitmap);

 

Do you see any problems here? Is this the official way to decode UPC by your library? Thanks! 

In case I miss something, I paste the whole code here, it basically gets image stream from camera and tries to decode it:

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);

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

Coordinator
Mar 26, 2012 at 6:37 PM

Back from vacation I can give you an answer.

I can't check your code directly because I don't have a Windows Phone.
But with the help of the emulator I made a simple windows phone app which shows the usage of the library. It works for static pictures but I can't test the camera access because the emulator doesn't provide a real access to my webcam.

You can get the source here:
https://zxingnet.svn.codeplex.com/svn/trunk/Clients/WindowsPhoneDemo

Please make some sample pictures of the barcodes you want to scan. I will look at them and try to find the cause why you don't get a result.