Implement Barcode scanning on android in only 12 steps.
1. Creating new projectFile -> New -> Android project -> Then Press finish button
2. it will create project hierarchy given below
3. Create Layout File
- activity_main.xml layout file name
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/startScanning"
android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="70dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:onClick="onStartBrScan" android:layout_marginTop="60dp" android:text="Start Barcode Scan" />
<TextView
android:id="@+id/scannedbr"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
4. Implement call back methods and add REQUEST_CODE_BARCODE = 3000.
package com.example.barcodescanner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
/*
* Add this line in the activity which will initiate barcode to scanner
*/
private final int REQUEST_CODE_BARCODE = 3000;
private TextView mBCScanTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBCScanTxt = (TextView)findViewById(R.id.scannedbr);
}
public void onStartBrScan(View view){
}
}
5. Download this project and add it as a library project. Right click on project select properties-> android-> scroll down -> press add button -> select "com.jwetherell.quick_response_code.CaptureActivity".
5. Download this project and add it as a library project. Right click on project select properties-> android-> scroll down -> press add button -> select "com.jwetherell.quick_response_code.CaptureActivity".
6. Library will be added to your project now as below.
7. Come back to your project,and modify onStartScan callback method as below.
public void onStartBrScan(View view){
startActivityForResult(new Intent(MainActivity.this,
DecoderActivity.class), REQUEST_CODE_BARCODE);
}
8. Add onactivityresult method.
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (data != null) {switch (requestCode) {case REQUEST_CODE_BARCODE:mBCScanTxt.setText(data.getExtras().getString("ISBN"));break;default:break;}}
}
9. Add camera permission to the AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
10. Add the below activity in the AndroidManifest.xml file under the application tag.Modified AndroidManifest.xml is given below.
<activity
android:name="com.jwetherell.quick_response_code.CaptureActivity"
android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/icon"
android:label="@string/capture_name"
android:screenOrientation="landscape" android:stateNotNeeded="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" > </activity> <activity android:name="com.jwetherell.quick_response_code.DecoderActivity"
android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/icon"
android:label="@string/decoder_name"
android:screenOrientation="landscape" android:stateNotNeeded="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" > </activity> <activity android:name="com.jwetherell.quick_response_code..EncoderActivity"
android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/icon"
android:label="@string/encoder_name"
android:screenOrientation="landscape" android:stateNotNeeded="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" > </activity>
11. Modified AndroidManifest.xml is given below.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.barcodescanner"
android:versionCode="1" android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application
android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.example.barcodescanner.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.jwetherell.quick_response_code.CaptureActivity"
android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/icon"
android:label="@string/capture_name"
android:screenOrientation="landscape" android:stateNotNeeded="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" > </activity> <activity android:name="com.jwetherell.quick_response_code.DecoderActivity"
android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/icon"
android:label="@string/decoder_name"
android:screenOrientation="landscape" android:stateNotNeeded="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" > </activity> <activity android:name="com.jwetherell.quick_response_code..EncoderActivity"
android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/icon"
android:label="@string/encoder_name"
android:screenOrientation="landscape" android:stateNotNeeded="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" > </activity> </application>
</manifest>
12. Congrates :) You are done now .