Android is popular mobile phone OS. Sometime developer want to adding QR Code Scanner/Reader in the application they buid. I want to share my experience creating QR Code Reader in android using ZXing Barcode Scanner.
ZXing ("zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. ZXing project can be found on github https://github.com/zxing/zxing/.
Tutorial using ZXing in android I found great here. http://karanbalkar.com/2013/12/tutorial-65-implement-barcode-scanner-using-zxing-in-android/.
Step 1
Download ZXing. I download from here. Because I use from ZXing project master is not working.
https://gitorious.org/rowboat/external-zxing/source/7620644768ffc235607b3a94671e49518c18686f:qr_scanner
Step 2
Extract downloading file (rowboat-external-zxing folder) then import folder qr_scanner to eclipse.
Step 3
Create folder libs to the root project, then copy file core.jar from folder core (in rowboat-external-zxing folder) paste to folder libs. The project look like this. Set CaptureActivity project as library. (right-click) Properties -> Android -> Check Is Library.
Step 4
Create new android project application. (right click) Project –> Properties –> Android and add the ZXing project (CaptureActivity) as a library.
Step 5
Add the following code
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.project.tester.MainActivity" >
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan"/>
</LinearLayout>
MainActivity.java
package com.project.tester;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView result;
Button scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result=(TextView)findViewById(R.id.result);
scan=(Button)findViewById(R.id.scan);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
result.setText("Format: "+format+"\nContent: "+contents);
Toast.makeText(getApplicationContext(), "Format: "+format+"\nContent: "+contents,Toast.LENGTH_SHORT).show();
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
// Log.i("App","Scan unsuccessful");
Toast.makeText(getApplicationContext(), "Scan unsuccessful",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.tester"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Complete code here(Dropbox)
Good Luck
No comments:
Post a Comment
Silahkan