Hi , i am doing a project which my android enable mobile use to scan the bar-code using its in build camera. When i search on this topic i found that there is a great free and open source application call ZXing. This application allow users to access the Scan class using Intent. I am still studying about develop android application development.But after search on the internet i found the solution.Now i have created a simple application on how to do id. I created sample video of demonstration on my Huawei U8180 device.
You must install the ZXing to your device from android market.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg"> <Button android:text="SCAN NOW" android:id="@+id/btnSearch" android:layout_width="150px" android:gravity="center_vertical|center_horizontal" android:layout_height="100dp" android:layout_gravity="center_vertical|center_horizontal|center" android:textStyle="bold" android:textSize="25dp" android:layout_marginTop="150dp"> </Button> </LinearLayout>
Now my main class like this.
package com.br; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Main extends Activity { /** Called when the activity is first created. */ Button myButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton =(Button)findViewById(R.id.btnSearch); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { 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); } }); } public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == 0) { if (resultCode == RESULT_OK) { //String result =intent.getAction(); String contents = intent.getStringExtra("SCAN_RESULT"); String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); //Toast andEggs = Toast.makeText(Main.this, contents, Toast.LENGTH_SHORT); //andEggs.show(); AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create(); alertDialog.setTitle("Barcode Results"); alertDialog.setMessage(contents+"\n"+format); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.setIcon(R.drawable.barcode); alertDialog.show(); // Handle successful scan } else if (resultCode == RESULT_CANCELED) { // Handle cancel } } } }
Now my AndroidManifest.xml file look like this.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.br" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.CAMERA"></uses-permission> <application android:icon="@drawable/barcode" android:label="@string/app_name"> <activity android:name=".Main" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" 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.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
I think now you can understand how to use ZXing library in our project.This is the greatest library i ever saw in my life for mobile application development.
Sources