Not yet rated
Tags:



Problem

I want to add a QR Code Reader to my iOS app and i want it to run as fast as an app which is built in iPhone's or iPad's native language.

Solution

Adding the qr-reader-ane native extension to our app will solve our problem. It solved mine.

Detailed explanation

This recipe is an excerpt from my blog post available at http://www.nativext.com/ane-by-os/ios/qr-reader-native-extension/ Please refer to that post for full details.

NOTES:

  • This native extension is iOS only. So, it won't work on Android, Blackberry and any other devices.
  • This native extension is not working in the simulator and you can not compile it in-built compiler of Flash Pro. In order to compile the ipa, you need to write the command prompt. You can find the simple commands in the zip package of Brad Wallace at the link above. You can download it in the downloads section.

 

As a first step, we will install the native extension to Flash Pro. To do this, click "File > ActionScript Settings", go to "Library Path" tab, click "Browse to a Native Extension (ANE) file", select your *.ane file (in this case it is QRZBarExtension.ane), click "Open" and "OK". You are done. You added the native extension to your project.

In the next steps, we will write the codes to the main .as file of the project. Open the main actionscript file and add this 4 lines of code to the top to import needed libraries to the project :

import com.rancondev.extensions.qrzbar.QRZBar;
import com.rancondev.extensions.qrzbar.QRZBarEvent;
import flash.display.MovieClip;
import flash.events.*;

Later, lets define a variable to hold QR Code reader object :

private var qr:QRZBar;

Then we will write the function that starts qr code reading when you click a button :

protected function test(event:MouseEvent):void {
        trace("button clicked");
        qr = new QRZBar();
        qr.scan();
        qr.addEventListener( QRZBarEvent.SCANNED, scannedHandler );
}

Don't forget to attach this function to your SCAN button. Then as a last step, we will write the function that will work after scanner found a code :

protected function scannedHandler( event : QRZBarEvent ) : void {
        qr.removeEventListener( QRZBarEvent.SCANNED, scannedHandler );
        chosenlabel.text = event.result;
}

That's all. It is now ready to compile your app. Don't forget to use command prompt to publish your app. Here is the full sample code :

package  {
        import com.rancondev.extensions.qrzbar.QRZBar;
        import com.rancondev.extensions.qrzbar.QRZBarEvent;
        
        import flash.display.MovieClip;
        import flash.events.*;
        
        public class Example extends MovieClip {
                
                private var qr:QRZBar;
                
                public function Example()
                {
                }
                
                protected function test(event:MouseEvent):void
                {
                        trace("button clicked");
                        qr = new QRZBar();
                        qr.scan();
                        qr.addEventListener( QRZBarEvent.SCANNED, scannedHandler );
                }
                
                protected function scannedHandler( event : QRZBarEvent ) : void
                {
                        qr.removeEventListener( QRZBarEvent.SCANNED, scannedHandler );
                        chosenlabel.text = event.result;
                }
        }
        
}

Please refer to the full blog post on my blog to download Flex or Flash Pro samples and some sample command promts to compile your app.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes