How to create barcode and QR code in android

1455 views.

Barcode & QR code

Add Comment
1 Answer(s)
899 views.

Please find the code for QR Code Scanner

 

In gradle you can add these first

 

 dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:appcompat-v7:23.4.0'
 compile 'com.google.android.gms:play-services:7.8.0'
 compile 'me.dm7.barcodescanner:zxing:1.8.4'
}
 <?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: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="androindian.qrcodescanner.MainActivity"
 android:orientation="vertical">
<Button
 android:layout_height="wrap_content"
 android:layout_width="match_parent"
 android:text="QRScanner"
 android:onClick="QrScanner" />
</LinearLayout>
 
 package androindian.qrcodescanner;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
 private ZXingScannerView mScannerView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 public void QrScanner(View view){
 mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
 setContentView(mScannerView);
 mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
 mScannerView.startCamera(); // Start camera
 }
 @Override
 public void onPause() {
 super.onPause();
 mScannerView.stopCamera(); // Stop camera on pause
 }
 @Override
 public void handleResult(Result result) {
 Log.e("handler", result.getText()); // Prints scan results
 Log.e("handler", result.getBarcodeFormat().toString()); // Prints the scan format (qrcode)
 // show the scanner result into dialog box.
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setTitle("Scan Result");
 builder.setMessage(result.getText());
 AlertDialog alert1 = builder.create();
 alert1.show();
 // If you would like to resume scanning, call this method below:
 // mScannerView.resumeCameraPreview(this);
 }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="androindian.qrcodescanner">
 <uses-permission android:name="android.permission.CAMERA" />
 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
 <activity android:name=".MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 <meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
 </application>
</manifest>
 
Answered on July 19, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.