This project has moved. For the latest updates, please go here.

windows phone 8, no recognize the barcodes

Jul 12, 2014 at 5:06 PM
hi to all,
I am trying to use the barcode lib without success.
if keeps trying to recognize the barcode.
MS build in barcode reader has no problem at all

my code is 99% copy of your example

        private readonly PhotoChooserTask photoChooserTask;
        private readonly BackgroundWorker scannerWorker;

        private DispatcherTimer timer;
        private PhotoCameraLuminanceSource luminance;
        private IBarcodeReader reader;
        private PhotoCamera photoCamera;
        private readonly WriteableBitmap dummyBitmap = new WriteableBitmap(1, 1);


        public ItemsPanPage()
        {
            InitializeComponent();

            // prepare Photo Chooser Task for the open button
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += (s, e) => { if (e.TaskResult == TaskResult.OK) ProcessImage(e); };

            // prepare the backround worker thread for the image processing
            scannerWorker = new BackgroundWorker();
            scannerWorker.DoWork += scannerWorker_DoWork;
            scannerWorker.RunWorkerCompleted += scannerWorker_RunWorkerCompleted;

            //foreach (var x in typeof(BarcodeFormat).GetFields())
            //{
            //    if (x.IsLiteral)
            //    {
            //        BarcodeType.Items.Add(x.GetValue(null));
            //    }
            //}

            // open the default barcode which should be displayed when the app starts
            var uri = new Uri("/images/35.png", UriKind.Relative);
            var imgSource = new BitmapImage(uri);
            BarcodeImage.Source = imgSource;
            imgSource.ImageOpened += (s, e) => scannerWorker.RunWorkerAsync(new WriteableBitmap((BitmapImage)s));

        }

        void scannerWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // processing the result of the background scanning
            if (e.Cancelled)
            {
                BarcodeContent.Text = "Cancelled.";
            }
            else if (e.Error != null)
            {
                BarcodeContent.Text = e.Error.Message;
            }
            else
            {
                var result = (Result)e.Result;
                DisplayResult(result);
            }
        }

        static void scannerWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // scanning for a barcode
            e.Result = new BarcodeReader().Decode((WriteableBitmap)e.Argument);
        }

        private void ProcessImage(PhotoResult e)
        {
            // setting the image in the display and start scanning in the background
            var bmp = new BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
            BarcodeImage.Source = bmp;
            scannerWorker.RunWorkerAsync(new WriteableBitmap(bmp));
        }


        private void CameraButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (photoCamera == null)
            {
                photoCamera = new PhotoCamera();
                photoCamera.Initialized += OnPhotoCameraInitialized;
                previewVideo.SetSource(photoCamera);

                CameraButtons.ShutterKeyHalfPressed += (o, arg) => photoCamera.Focus();
            }

            if (timer == null)
            {
                timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1500) };
                if (photoCamera.IsFocusSupported)
                {
                    photoCamera.AutoFocusCompleted += (o, arg) => { if (arg.Succeeded) ScanPreviewBuffer(); };
                    timer.Tick += (o, arg) => { try { photoCamera.Focus(); } catch (Exception) { } };
                }
                else
                {
                    timer.Tick += (o, arg) => ScanPreviewBuffer();
                }
            }

            BarcodeImage.Visibility = System.Windows.Visibility.Collapsed;
            previewRect.Visibility = System.Windows.Visibility.Visible;
            timer.Start();
        }


        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (timer != null)
            {
                timer.Stop();
                timer = null;
            }
            photoCamera = null;
        }

        private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
        {
            var width = Convert.ToInt32(photoCamera.PreviewResolution.Width);
            var height = Convert.ToInt32(photoCamera.PreviewResolution.Height);

            Dispatcher.BeginInvoke(() =>
            {
                previewTransform.Rotation = photoCamera.Orientation;
                // create a luminance source which gets its values directly from the camera
                // the instance is returned directly to the reader
                luminance = new PhotoCameraLuminanceSource(width, height);
                reader = new BarcodeReader(null, bmp => luminance, null);
            });
        }


        private void ScanPreviewBuffer()
        {
            if (luminance == null)
                return;

            photoCamera.GetPreviewBufferY(luminance.PreviewBufferY);
            // use a dummy writeable bitmap because the luminance values are written directly to the luminance buffer
            var result = reader.Decode(dummyBitmap);
            Dispatcher.BeginInvoke(() => DisplayResult(result));
        }

        private void DisplayResult(Result result)
        {
            if (result != null)
            {
                //BarcodeType.SelectedItem = result.BarcodeFormat;
                BarcodeContent.Text = result.Text;
                MessageBox.Show("Barcode found " + System.Environment.NewLine + result.Text);
            }
            else
            {
                //BarcodeType.SelectedItem = null;
                BarcodeContent.Text = "No barcode found.";
            }
        }

Coordinator
Aug 4, 2014 at 6:13 PM
Did you try different orientations of the device? The image orientation can be a problem.
You can try the reader option AutoRotate = true.
Another error can occur if the width and height values of the luminance source doesn't match
the resolution of the PreviewBufferY (for example the width and the height value are changed
after the intialization or the width is used as the height value and vice versa).