On Friday November 7th The update to my Walmart app had included adding a way for the users to contact me by email. So a user named Jennifer had wrote me an email saying that in the android walmart app there was a way for her to scan barcodes of items. She said that she would really like this feature. So i took to the lovely internet in search of a barcode reader API to implement into my app. I stumbled upon a nokia article about zxing barcode reader. They gave a lot of code on how to make it work so went to make it work.
It started off with making a user interface which contains a canvas and the canvas background with is gonna be a video brush. the videobrush is basically gonna redraw what you see through the camera lens. the extra text blocks are some hints on how to use the barcode.
let us go through some methods now and try to explain whats going on.
OnNavigatedTo method is whats called when the page is navigated to, whether it be from navigating forward or back from another page. but in this method we call new on our camera, and set up and event handler for when it is initialized. We also set up an event handler for the auto focusing of the camera. We also set up an event handler for the camera key being half pressed. Next we set the source of that canvas to our camera so now we can see the camera in the canvas like i stated above. we set up a dispatcher time so that every 250 milliseconds the bar code reader attempts to read the bar code. And last but not least we set up an event handler for if the canvas was tapped. when the canvas is tapped its going to focus the camera at the desired tapped location, we will see code for this later.
next is onNavigatedFrom is called when the user is leaving the page. in this method we just dispose of our camera so its still not in use and do the opposite of registering an event handler. thats actually my first time seeing the -= used with an event handler, pretty cool.
next we have focus_Tapped. this is called when the user taps the canvas. it first checks if the camera is null meaning it wasnt initialized. it then checks to see if the camera has the capability to be focused at any point. it then goes in a finds the points at which the canvas were touched and sets the brackets to where the finger touched. it also subtracts an offset. when the points are found it will then try to pass the x and y values to the camera through the focusAtPoint.
next is the CameraButton_shutterKeyHalfPressed which just calls the camera.focus method.
next is the photoCamera_AutoFocusCompleted event which just changes the visibility of the focus brackets to collapsed. if you look in the user interface code you will see that there is a text block that contains [ ]. yes you actually have to set the focus brackets by a text block which i would have never imagined and thats awesome.
next is the photoCamera_initialized this really just sets a couple of properties. first off we set the camera to not have any flash. we set the previewbuffer equal to a bitmap that takes the width and height of camera resolution. we initialize our barcodeReader. This is optional but you can set what type of bar codes that your barcode reader accepts. so for mines i set the UPC-A and UPC-E. i then passed that into the barcodeReader object. I also set the try harder option to true. which they say makes it work harder to try and figure out the barcode. and last but not least hook up the event hanler for resultfound.
next is the barcodeReader_Resultfound. this just checks if the text scanned is equal to the text in the barcodeData textbox. if it is different then do a quick vibration and change the text in the textbox, then navigate to the my search page.
lastly the scanForBarcode method which stores the arb values into the previewbuffer pixels, then we call invalidate which redraws the entire bitmap or requests to draw. then call the method for the barcodereader to decode the previewbuffer
And thats everything. well theres a section where you navigate to the search page. when navigating i pass through the upc number and put it into the search field behind the scenes and you end up with the result if it is found.