Top Banner
Cosc 5/4730 Blackberry and Andriod QR codes
26

Cosc 5/4730

Feb 10, 2016

Download

Documents

fordon

Cosc 5/4730. Blackberry and Andriod QR codes. What is QR code?. A QR code (abbreviated from Quick Response code ) is a type of matrix barcode (or two-dimensional code) first designed for the automotive industry. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cosc 5/4730

Cosc 5/4730

Blackberry and AndriodQR codes

Page 2: Cosc 5/4730

What is QR code?

• A QR code (abbreviated from Quick Response code) is a type of matrix barcode (or two-dimensional code) first designed for the automotive industry.

• More recently, the system has become popular outside of the industry due to its fast readability and large storage capacity compared to traditional UPC barcodes.

• The code consists of black modules arranged in a square pattern on a white background.

• The information encoded can be made up of four standardized kinds ("modes") of data (numeric, alphanumeric, byte/binary, Kanji), or by supported extensions virtually any kind of data

From Wikipedia, http://en.wikipedia.org/wiki/QR_code

Page 3: Cosc 5/4730

Anatomy of a QR code

Page 4: Cosc 5/4730

QR app’s

• The Main api for QR codes comes from the ZXing (“Zebra Crossing”).

• Which is part of google.com code• http://code.google.com/p/zxing/

• Blackberry supports the APIs directly in the com.google.zxing, since API 6.0.0

• Android does not have any built-in API (for it’s own code…). Instead you need to install the barcode Scanner apk (in the market and on the zxing website)– Version 3.72 (as of Jan 25, 2012)

Page 5: Cosc 5/4730

Creating QR codes

• You can use XYing website to create QR code• http://zxing.appspot.com/generator/

Page 6: Cosc 5/4730

BLACKBERRYQR code

Page 7: Cosc 5/4730

QR codes

• Uses the com.google.zxing packages.– Allows you to create QR very quickly and easily.

– You can also scan for QR codes• Uses the camera and viewfinder, so some what

complex if you don’t already under the camera calls.

Page 8: Cosc 5/4730

Creating/displaying the QR code.• First delcare a QRCode variableQRCode qrCode = new QRCode();• This encodes the text with a low level (%7) of error correctionEncoder.encode(barcodeTextField.getText(), ErrorCorrectionLevel.L, qrCode);

• Where barcodeTextField is a the text to encode in the QRcode

• From there we get the actual data matrix and convert it into a bitmapByteMatrix barcode = qrCode.getMatrix();Bitmap bitmap = BarcodeBitmap.createBitmap(barcode, BARCODE_WIDTH);

– Where BARCODE_WIDTH is a constant 300.• Now display the barcode in a bitmapFieldbarcodeField.setBitmap(bitmap);

Page 9: Cosc 5/4730

Scanning a QR code.

• Requires use of the camera.– So if you’ll need to back to get camera lecture for

a lot of the information about what is going on with the viewfinder.

– We create a BarcodeScanner and BarcodeDecoder• The decoder is feed to the scanner as well as “hints”• We provide this information to the viewfinder as well

to find the QR code automatically.– IE the camera will focus, etc and “take the picture”.

Page 10: Cosc 5/4730

Coding it up.• First we create a hashtable to hold all of the hints that we can give the API about

how we want to scan a barcode to improve speed and accuracy.Hashtable hints = new Hashtable();

• The first thing going in is a list of formats. We could look for more than one at a time, but it's much slower.

Vector formats = new Vector();formats.addElement(BarcodeFormat.QR_CODE);

– This is where we would add other formats to look for.

hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);• We will also use the "TRY_HARDER" flag to make sure we get an accurate scanhints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

• We create a new decoder using those hintsBarcodeDecoder decoder = new BarcodeDecoder(hints);

Page 11: Cosc 5/4730

Coding it up. (2)

• Finally we can create the actual scanner with a decoder and a listener that will handle the data stored in the barcode. We put that in our view screen to handle the display.

scanner = new BarcodeScanner(decoder, new MyBarcodeDecoderListener() );barcodeScreen = new MyBarcodeScannerViewScreen(scanner);• Once we get here, all the barcode scanning infrastructure

should be set up, so all we have to do is start the scan and display the viewfinder

scanner.startScan();theApp.pushScreen(barcodeScreen); //the viewfinder screen

Page 12: Cosc 5/4730

BarcodeDecoderListener

• The listener is call when a the QR code has been decoded with the text string.

class MyBarcodeDecoderListener implements BarcodeDecoderListener {

public void barcodeDecoded(final String rawText) {//close the viewfinder//and decide what to do with the rawText//like launch a browser if it’s an http://…}

}

Page 13: Cosc 5/4730

You can use BB qrDemo code

• Which is located here:

• The res/img has a demo barcode img file for the simulator

Page 14: Cosc 5/4730

ANDROIDQR code

Page 15: Cosc 5/4730

First

• You will need to install the BarcodeScannerX.apk into the simulator– And download it first of course.– Adb.exe install BarcodeScanner3.72.apk – We’ll use intents to call it.

• It should noted, you can also import their code and then it looks a lot like the Blackberry code.– We are not covering this

Page 16: Cosc 5/4730

Encoding.

• Create the intent and call the activity.• So for a simple one, text encoding. Intent intent = new Intent( "com.google.zxing.client.android.ENCODE");intent.putExtra("ENCODE_TYPE", “TEXT_TYPE”);intent.putExtra("ENCODE_DATA", “http://www.cs.uwyo.edu);startActivity(intent);• The encoded QR will show on the screen.

Page 17: Cosc 5/4730

Scanning

• Again, create an intent, but we’ll need a result.Intent intent = new Intent( "com.google.zxing.client.android.SCAN");intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");

startActivityForResult(intent, 0);

Page 18: Cosc 5/4730

Scanning (2)

• Add the onActivityResult methodif (requestCode == 0) { //code used to call Scanner. if (resultCode == RESULT_OK) { String contents = intent.getStringExtra("SCAN_RESULT"); String format = intent.getStringExtra( "SCAN_RESULT_FORMAT"); // Handle successful scan} else if (resultCode == RESULT_CANCELED) { // Handle cancel}}

Page 19: Cosc 5/4730

All well and good, EXCEPT

• What happens if the BarcodeScanner app is not installed?– FORCE CLOSE!

• But there is code to handle it.– xying has two java class you can add to your

project. • IntentIntegrator.java and IntentResult.java

Page 20: Cosc 5/4730

IntentIntegrator

• It checks to make sure the app is installed.– If not, then it asks the user to get it from the

Market.• Now you code doesn’t force close.

Page 21: Cosc 5/4730

Create with the Integrator

• We call the integrator code, we included in the project

IntentIntegrator integrator = new IntentIntegrator( qrDemoActivity.this);

• Where qrDemoActivity is the name of your activity.

integrator.shareText(“http://www.cs.uwyo.edu” );• Again shows the qrCode or an error message if

there is a problem with the text.

Page 22: Cosc 5/4730

Scan with the Integrator

• Same idea as creating, but again we need a result

IntentIntegrator integrator = new IntentIntegrator(qrDemoActivity.this);

– Where qrDemoActivity is the name of your activity.

integrator.initiateScan( IntentIntegrator.QR_CODE_TYPES);• And wait for the result in onActivityResult

Page 23: Cosc 5/4730

Scan with the Integrator (2)• In the onActivityResult method //code to handle the intentintegrator, then IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); if (scanResult != null) { // handle scan result String contents = scanResult.getContents(); if (contents != null) {

logthis("[II] Scan result is "+ scanResult.toString()); } else {

logthis("[II] Scan failed or canceled"); }} //handle the other result codes as needed.

Page 24: Cosc 5/4730

You can use Android qrDemo code

• Which is located here:

Page 25: Cosc 5/4730

refrences

• http://zxing.appspot.com/generator• http://code.google.com/p/zxing/• http://supportforums.blackberry.com/t5/Java-

Development/How-to-use-the-Barcode-API/ta-p/574569

• http://devblog.blackberry.com/2010/10/barcode-api/

• http://stackoverflow.com/questions/9795501/how-do-i-use-zxings-qr-barcode-scanner-with-intentintegrator-and-intentresult

Page 26: Cosc 5/4730

QA&