Wednesday 19 November 2014

HB Blog 39: How To Integrate Barcode/QrCode Scanner For Android???

Mobile technology has seen vast change and has moved towards great future.Today most of the day to day life has grown taking technology in hand.
In this post, I would like to show barcode/QrCode scanner for android.

A barcode is an optical machine-readable representation of data relating to the object to which it is attached.QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode). 

How To Integrate Barcode/QrCode Scanner For Android?
There are two ways of Barcode/QrCode integration.
1)Using Traditional Application.
2)Using Library Integration.

1)Using Traditional Application:- 
In this way, you redirect from your application to standard barcode application on google play.
Barcode Scanner

Have a look on few code snippets,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//write this code on desire button click
try{
                    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for bar codes
                    startActivityForResult(intent, 0);
                }
                catch(Exception e){
                    Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
                    Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
                    startActivity(marketIntent);
                }

//And then catch result using onActivityResult
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = data.getStringExtra("SCAN_RESULT");
                System.out.println("contents:-"+contents);
                Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(contents));
                startActivity(marketIntent);
            }
            if(resultCode == RESULT_CANCELED){
                //handle cancel
            }
        }
    }

2)Using Library Integration:- 
In this way, you use the same library by ZXing Team to integrate barcode/QrCode scanner inside your application.
Firstly, download required support library files by ZXing Team from below link.
Download Support Library 
Secondly, add them to your libs folder in android project traditional way.
Then,Have a look on few code snippets,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//write this code on desire button click
 // set the last parameter to true to open front light if available
                IntentIntegrator.initiateScan(ZXingJarDemoActivity.this, R.layout.capture,
                        R.id.viewfinder_view, R.id.preview_view, true);

 //And then catch result using onActivityResult
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case IntentIntegrator.REQUEST_CODE:
                IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode,
                        resultCode, data);
                if (scanResult == null) {
                    return;
                }
                final String result = scanResult.getContents();
                if (result != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            txtScanResult.setText(result);
                        }
                    });
                }
                break;
            default:
        }
    } 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Also, add following lines in android manifest,

<supports-screens android:largeScreens="true"
        android:normalScreens="true" android:smallScreens="true"
        android:anyDensity="true" />
    <uses-feature android:name="android.hardware.camera" />

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />


<activity android:name="com.example.hbdemo.ZXingJarDemoActivity" android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Refer the below link for complete sample code:-
Download Sample Code
Download Apk File

No comments:

Post a Comment