The fact that we always would like to developing an application which be able to "embedding" this scanner in it, not warning that our device has not installed Barcode Reader application. Fortunately, there is a library developed by Dushyanth Maguluru which based on ZXing and ZBar, which be able to Barcode Scanner views, help us to develop a simple application which can scan Barcode/QR code itself.
Adding Barcode reader library to Android Studio Project
In order to use this library, the simplest way is adding it's dependency to your application level
build.gradle
:
dependencies {
compile 'me.dm7.barcodescanner:zxing:1.9' //barcode reader dependency
compile 'com.android.support:appcompat-v7:24.1.1'
}
Project main activity
- Start the scanning barcode activity as a
sub-Activity
byIntent
. - Retrieve scanning results: barcode format and content by overriding
onActivityResult()
.
MainActivity.java
And it's layout:
package info.devexchanges.barcodescanner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private final static int REQUEST_SCANNER = 1;
public final static String FORMAT = "format";
public final static String CONTENT = "content";
private TextView content;
private TextView format;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
View btnScan = findViewById(R.id.btn_scan);
format = (TextView) findViewById(R.id.format);
content = (TextView) findViewById(R.id.content);
setSupportActionBar(toolbar);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ScannerActivity.class);
startActivityForResult(intent, REQUEST_SCANNER);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Activity.RESULT_OK) {
content.setText(data.getStringExtra(CONTENT));
format.setText(data.getStringExtra(FORMAT));
}
}
}
activity_main.xml
<?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:orientation="vertical"
tools:context="info.devexchanges.barcodescanner.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_gravity="top"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_scan"
android:text="Scan Barcode"
android:layout_margin="@dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/format"
android:gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content"
android:gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Scanning Barcode/QR code Activity
MainActivity
code above, when Button
pressed, ScannerActivity
will be launch as a sub-Activity
.
The Barcode scanner view is initialized by a
ZXingScannerView
object. In onCreate()
, declaring it like this:
ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
scannerView = new ZXingScannerView(this);
contentFrame.addView(scannerView);
The most important work is retrieving scanning result, so your ScannerActivity
must implements ResultHandler
interface and override handleResult(Result result)
method. Moreover, start scanning by invoke this code:
scannerView.startCamera();
And if you want to stop this scanning, call closing camera method:
scannerView.stopCamera();
This is full code for this activity:
ScannerActivity.java
And it's layout:
package info.devexchanges.barcodescanner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView scannerView;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_scanner);
ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
scannerView = new ZXingScannerView(this);
contentFrame.addView(scannerView);
}
@Override
public void onResume() {
super.onResume();
scannerView.setResultHandler(this);
scannerView.startCamera();
}
@Override
public void onPause() {
super.onPause();
scannerView.stopCamera();
}
@Override
public void handleResult(Result rawResult) {
//Call back data to main activity
Intent intent = new Intent();
intent.putExtra(MainActivity.FORMAT, rawResult.getBarcodeFormat().toString());
intent.putExtra(MainActivity.CONTENT, rawResult.getText());
setResult(Activity.RESULT_OK, intent);
finish();
}
}
activity_scanner.xml
Running this application, we'll have this screen:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Clicking button, we'll start scanning:
Scanning Barcode |
An example after scanning a QR code: