Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate ZXing Library to Android Studio for Barcode Scanning?

I've been looking all over internet how to include zxing library to my project, and I found this tutorial: http://blog.dihaw.com/integrating-zxing-in-your-android-app-as-standalone-scanner/

But when I reach the point that you need to check for BeepManager to add the R import I get all kinds of errors in my project (Even on the MainActivity) that it couldn't find R.

Also I found this one https://github.com/journeyapps/zxing-android-embedded/blob/master/README.md which seemed a lot easier because it was auto integrated by gradle, but when I sync it pops an error that it couldn't find the files.

Any help would be appreciated :) I'm new to Android Studio.

EDIT:

I added the settings of the 2nd method (the one with the gradle settings) to my build.gradle and 4 error pop up:

Error:Failed to find: com.embarkmobile:zxing-android-legacy:2.0.0  Error:Failed to find: com.google.zxing:core:3.0.1  Error:Failed to find: com.embarkmobile:zxing-android-integration:2.0.0  Error:Failed to find: com.embarkmobile:zxing-android-minimal:2.0.0 

Any help?

---ANSWER---

To fix this problem I needed to disable Offline Work on Gradle.

  • Android Studio's settings>Gradle>Uncheck 'offline work'
like image 604
Mauricio Silva Avatar asked Jan 08 '15 23:01

Mauricio Silva


People also ask

How do I scan a barcode with ZXing Android?

On click of button_scan_qr_code , CaptureActivity will start scanning using default camera. Once it scans any QR code, it sends back the result to onActivityResult the MainActivity . ZXing also provides online QR Code Generator. Enter the required fields, generate and scan it to get the results.

How do I use ZXing library on Android?

In order to use the Zxing library in our application we need to add it's dependency in our application's gradle file. For adding the dependency Go to Gradle Scripts > build. gradle(Module: app) and add the following dependencies. After adding the dependency you need to click on Sync Now.

How do you use a ZXing barcode?

Pressing the button linked to mScan would launch directly into the ZXing barcode scanner screen (or crash if ZXing isn't installed). Once a barcode has been recognised, you'll receive the result in your Activity , here in the contents variable.

How do I scan a QR code with Android studio?

To test this app, run the app in Android Studio and install it on a device. Once the app is installed, make sure that you have enabled the Camera permission for the app in Settings if it is not enabled already. Next, find a QR code and tap the ImageButton to scan it.


1 Answers

You need add the following to your build.gradle file:

repositories {     mavenCentral()      maven {         url "http://dl.bintray.com/journeyapps/maven"     } }  dependencies {     // Supports Android 4.0.3 and later (API level 15)     compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'      // Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.     // If you only plan on supporting Android 4.0.3 and up, you don't need to include this.     compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'      // Convenience library to launch the scanning and encoding Activities.     // It automatically picks the best scanning library from the above two, depending on the     // Android version and what is available.     compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'      // Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.     // This mostly affects encoding, but you should test if you plan to support these versions.     // Older versions e.g. 2.2 may also work if you need support for older Android versions.     compile 'com.google.zxing:core:3.0.1' } 

My build.gradle file like this:

apply plugin: 'com.android.application'  android {     compileSdkVersion 21     buildToolsVersion "21.1.2"      defaultConfig {         applicationId "com.myapplication2"         minSdkVersion 16         targetSdkVersion 21         versionCode 1         versionName "1.0"     }     buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'         }     } }  repositories {     mavenCentral()      maven {         url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"     } }  dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     compile 'com.android.support:appcompat-v7:21.0.3'     // Supports Android 4.0.3 and later (API level 15)     compile 'com.embarkmobile:zxing-android-minimal:2.0.0@aar'      // Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.     // If you only plan on supporting Android 4.0.3 and up, you don't need to include this.     compile 'com.embarkmobile:zxing-android-legacy:2.0.0@aar'      // Convenience library to launch the scanning and encoding Activities.     // It automatically picks the best scanning library from the above two, depending on the     // Android version and what is available.     compile 'com.embarkmobile:zxing-android-integration:2.0.0@aar'      // Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.     // This mostly affects encoding, but you should test if you plan to support these versions.     // Older versions e.g. 2.2 may also work if you need support for older Android versions.     compile 'com.google.zxing:core:3.0.1' } 

The code is here.

Also, for how to use it, please refer my answer on the Stackoverflow here

It contains method and also simple code that I have tested.

like image 100
bjiang Avatar answered Sep 25 '22 08:09

bjiang