I am trying to build an app which uses input from a scanned barcode and parses it so it can be used as input to a database. I have been trying to work with the Zxing barcode scanner, but I am really struggling to integrate it into my app. I have followed a number of tutorials, none of which have given me any success. The closest I have got to achieving the required functionality was by following this tutorial: http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162 However, this requires that Barcode Scanner app be installed on the device, and I would rather my app has it's own scanning capabilities. Would someone please be able to point me towards an idiot-proof tutorial for Android Studio?
For integrating the Zxing library into your application add the following dependency into your build.gradle
file of your android project.
dependencies {
compile 'me.dm7.barcodescanner:zxing:1.7.1'
}
My project is developed on Eclipse.
At first, download the ZXing lib
and then add this lib into your app as library... [ Right click on your project->Properties... ]
Then write this code:
btnScan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, Toast.LENGTH_LONG).show();
}
}
});
And :
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
textViewFormat.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
textViewData.setText(intent.getStringExtra("SCAN_RESULT"));
Uri imageURI = intent.getData();
Bitmap bitmap;
try{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI);
scannedBitmap.setImageBitmap(bitmap);
} catch(Exception e){
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(), intent.getStringExtra("SCAN_RESULT_FORMAT") + ":" + intent.getStringExtra("SCAN_RESULT"), 5000).show();
} else if (resultCode == RESULT_CANCELED) {
textViewFormat.setText("");
textViewData.setText("Cancelled By user");
}
}
For full project download my git repo
Manifesto: You have to give permission into your android manifesto file:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".encode.EncodeActivity"
android:label="@string/app_name"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="com.google.zxing.client.android.ENCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- This allows us to handle the Share button in Contacts. -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/x-vcard" />
</intent-filter>
<!-- This allows us to handle sharing any plain text . -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
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.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.