Like Us on Facebook

How to Make Barcode Scanning Application in Android

Hello everyone , as  per  specific demand  I am going to share How to Make Barcode Scanner Application in Android , we will use the ZXing (Zebra Crossing) library to carry out barcode scanning within an Android app. We will call on the resources in this open source library within our app, retrieving and processing the returned results. By importing the ZXing integration classes into our app, we can make user scans easier and focus our development efforts on handling the scan results. This Apps give you  Barcode Scanning As well as QRCode.



Prerequisites:
 
  • JDK 6.0 or above
  • Android Studio
  • Zxing library



Steps to follow:  
 
  1. Create a New Android Studio project name BarcodeScan.
  2. Select minimum  API level 14 so it will run Maximum of Android device that are Active on google Play then click next.
  3. Select Blank Activity and your Activity name is MainActivity click next > click finish.
  4. Inside MainActivity Class write the following code;
 
 public class MainActivity extends Activity {
 /** Called when the activity is first created. */
 static final String SCAN "com.google.zxing.client.android.SCAN";
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
  }

 public void ScanBar(View v){
 try{
 Intent in = new Intent (SCAN);
 in.putExtra("SCAN_MODE""PRODUCT_MODE");
 startActivityForResult(in, 0);
 }catch (ActivityNotFoundException e) {
 // TODO: handle exception
 
showDialog(MainActivity.this"No Scanner Found""Download a scanner code activity?""Yes""No").show();
 }
 }
    
 public void ScanQR(View v){

 try{
  Intent in = new Intent (SCAN);
  in.putExtra("SCAN_MODE""QR_CODE_MODE");
  startActivityForResult(in, 0);
  }catch (ActivityNotFoundException e) {
   // TODO: handle exception
  
showDialog(MainActivity.this"No Scanner Found""Download a scanner code activity?""Yes""No").show();    
  }
  }
    
  private Dialog showDialog(final Activity act, CharSequence title,
  CharSequence message, CharSequence Yes,  CharSequence No) {
      // TODO Auto-generated method stub
     
  
AlertDialog.Builder download = new AlertDialog.Builder(act);
  download.setTitle(title);
  download.setMessage(message);
  download.setPositiveButton(Yes, new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int i){
  // TODO Auto-generated method stub
  
Uri uri = Uri.parse("market://search?q=pname:" "com.google.zxing.client.android");
  Intent in = new Intent(Intent.ACTION_VIEW, uri);  
  try{
  act.startActivity(in);
  }catch(ActivityNotFoundException anfe){
          
  }
  }
  });
 download.setNegativeButton(No, new DialogInterface.OnClickListener() {
 @Override
  public void onClick(DialogInterface dialog, int i) {
  // TODO Auto-generated method stub
  
}
  });
  return download.show();
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent in) {
      // TODO Auto-generated method stub
  
if(requestCode ==0){
  if(resultCode == RESULT_OK){
  String contents = in.getStringExtra("SCAN_RESULT");
  String format =  in.getStringExtra("SCAN_RESULT_FORMAT") ;
  Toast toast = Toast.makeText(this"Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
  toast.show();
  }
  }
  }
  }
 
 

5.Inside activity_main.xml  write the following.
 
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  
xmlns:tools="http://schemas.android.com/tools"
  
android:id="@+id/container"
  
android:layout_width="match_parent"
  
android:layout_height="match_parent"
  
android:background="#ffffff"
  
android:orientation="vertical"
  
tools:context=".MainActivity" >
<TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center|top"
  android:layout_margin="20dp"
  android:text="Scan"
  android:textColor="#000000"
  android:textSize="30sp" />
<Button
  android:id="@+id/scanner"
  android:layout_width="250dp"
  android:layout_height="80dp"
  android:layout_gravity="center"
  android:layout_margin="10dp"
  android:gravity="center"
  android:onClick="scanQR"
  android:text="QR Code"
  
android:textSize="18sp" >
</Button>
<Button
  android:id="@+id/scanner2"
  android:layout_width="250dp"
  android:layout_height="80dp"
  android:layout_gravity="center"
  android:layout_margin="10dp"
  android:gravity="center"
  android:onClick="scanBar"
  android:text="Bar Code"
  android:textSize="18dp" >
</Button>
</LinearLayout>

 
 

 6.Inside activity_main.xml  write the following.
 
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  
xmlns:tools="http://schemas.android.com/tools"
  
android:id="@+id/container"
  
android:layout_width="match_parent"
  
android:layout_height="match_parent"
  
android:background="#ffffff"
  
android:orientation="vertical"
  
tools:context=".MainActivity" >
<TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center|top"
  android:layout_margin="20dp"
  android:text="Scan"
  android:textColor="#000000"
  android:textSize="30sp" />
<Button
  android:id="@+id/scanner"
  android:layout_width="250dp"
  android:layout_height="80dp"
  android:layout_gravity="center"
  android:layout_margin="10dp"
  android:gravity="center"
  android:onClick="scanQR"
  android:text="QR Code"
  
android:textSize="18sp" >
</Button>
<Button
  android:id="@+id/scanner2"
  android:layout_width="250dp"
  android:layout_height="80dp"
  android:layout_gravity="center"
  android:layout_margin="10dp"
  android:gravity="center"
  android:onClick="scanBar"
  android:text="Bar Code"
  android:textSize="18dp" >
</Button>
</LinearLayout>

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also