Error with scanbarcode from absolute uri

May 4, 2011 at 7:28 PM

Hello,

for school i'm using the zxing barcode scanning library in visual studio 2010.

When I used the relative uri to scan an image from my project

( for example WP7BarcodeManager.ScanBarcode(new Uri("/WindowsPhonePivotApplication2;component/qrcode-0.jpg", UriKind.Relative), new Action<BarcodeCaptureResult>(this.Barcode_Results)); )

everything worked.

Now, we made some generated barcodes on our server for school, that change every few days. Therefor, I use : 

WP7BarcodeManager.ScanBarcode(new Uri("http://brianmares.dyndns.info:8080/fotos/pers1.jpg", UriKind.Absolute), new Action<BarcodeCaptureResult>(this.Barcode_Results));

 

but I get the error "System.ArgumentException : Cannot load selected image. Please make sure URI path is correct.

URI 'http://brianmares.dyndns.info:8080/fotos/pers1.jpg' cannot be loaded. Try using overloaded Scanbarcode wit BitmapImage parameter instead'.

 

When I use the BitmapImage overloaded method of scanbarcode, I get the error

"NullReferenceException (BarcodeImage must be set before you can call SetupBarcodeImages)."

 

What am I doing wrong? Can't I scan barcodes from internet references? Or do I have to store the image first in the app before trying to use it?

 

kind regards,

 

Wouter Denolf

Coordinator
May 5, 2011 at 10:22 PM

I haven't tried using a file from the web before, but I do know that there are a lot of problems that are caused by the BitmapImage class being empty (0 width and 0 height) until the image is downloaded from the web or loaded from memory. By default the bitmap class delays creation until it is loaded into an Image class, but the WP7BarcodeManager class tries to use the BitmapCreateOptions.None option to force the image to be loaded immediately. It appears this does not work for files that are downloaded from the web.

Instead you will need to follow this code to download the file first and then create an image from the result stream. You can then pass the bitmap in to the ScanBarcode method and it should work. For example, you can modify the btCamera_Click method from the sample application as follows:

	private void btnCamera_Click(object sender, RoutedEventArgs e)
        {
            start_progress();
            //WP7BarcodeManager.ScanBarcode(Barcode_Results); //Previously used to load the camera
            
            WebClient webClientImgDownloader = new WebClient();
            webClientImgDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientImgDownloader_OpenReadCompleted);
            webClientImgDownloader.OpenReadAsync(new Uri("http://brianmares.dyndns.info:8080/fotos/pers1.jpg", UriKind.Absolute));
        }

        void webClientImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            // NOTE: should check for errors first
            BitmapImage bi = new BitmapImage();
            bi.SetSource(e.Result);
            WP7BarcodeManager.ScanBarcode(bi, new Action<BarcodeCaptureResult>(this.Barcode_Results));
        }

You should add error handling code to make sure that the image was downloaded successfully before using the e.Result stream, but this should help get you started. I'll see about adding another overload that takes a URL and does the downloading for you in the next version.

Coordinator
May 5, 2011 at 10:25 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 6, 2011 at 3:10 PM

Thank you very much!

Indeed, now I can proceed with error handling and other stuff,

thanks for the code and the explanation!

 

kind regards from Belgium,

Wouter

Dec 26, 2011 at 6:28 AM
Edited Dec 26, 2011 at 6:29 AM

i think you should store the image first in the app before trying to use it. here is a web, visit this you will know how to solve this problem.

it mainly introduce net_barcode_reader.

:) because i can't tell you exactly...so you can view by yourself.

Good lucky!

Feb 28, 2012 at 7:39 AM

Sorry for that,Maybe,a barcode generation guide for NET. can be helpful. Hope I helped.

Mar 2, 2012 at 3:21 AM

I suggest you to try another barcode generator Here is a free barcode generator that I've been using , KEEPAUTOMATION   UPC-A Barcode Generator Component for .NET, quite easy !Hope I helped :)

Apr 11, 2012 at 12:31 PM

Simple 4 lines code for this -

Call this under your button click-

var streamResourceInfo = Application.GetResourceStream(new Uri("PhoneApp1;component/Images/code128-1.png", UriKind.Relative));
            BitmapImage bi = new BitmapImage();
            bi.SetSource(streamResourceInfo.Stream);
            WP7BarcodeManager.ScanMode = com.google.zxing.BarcodeFormat.CODE_128;
            WP7BarcodeManager.ScanBarcode(bi, new Action<BarcodeCaptureResult>(this.BarcodeResults_Finished));

(Note make sure 2 things- First, replace above image (code128-1.png) with your's and Secondly, mention what sort of format is it - here i used CODE_128)

And create a callback function -

public void BarcodeResults_Finished(BarcodeCaptureResult BCResults)
        {
            try
            {
                if (WP7BarcodeManager.LastCaptureResults.BarcodeImage != null)
                {
                    imgCapture.Source = WP7BarcodeManager.LastCaptureResults.BarcodeImage; //Display image
                }
                else
                {
                    //No image captured
                }
                if (BCResults.State == WP7_Barcode_Library.CaptureState.Success)
                {
                    txtResult.Text = BCResults.BarcodeText; //Use results
                }
                else
                {
                    Debug.WriteLine(BCResults.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(String.Format("Barcode Processing Error: {0}", ex.Message));
            }
        }

 

Thanks You!!

Apr 11, 2012 at 12:34 PM

And if want to read from camera, then do below -

            WP7BarcodeManager.ScanBarcode((result) =>
            {
                if (result.State == WP7_Barcode_Library.CaptureState.Success)
                {
                    MessageBox.Show(result.BarcodeText);
                }
                else
                {
                    MessageBox.Show(result.ErrorMessage);
                }
            });

Jan 13, 2016 at 2:57 PM
I wonder whether the barcode scanner I am testing these days can help with that?