Reputation: 1879
I need to create an app that one of the feature will have an barcode scanner. I was searching for some code examples to make an barcode scanner but i haven't found any full example code.
Only thing that I found was an code example that works with Zxing app. But I don't want execute any secondary app. I want to have all-in-one.
Anyone knows some example?
Thanks.
Upvotes: 2
Views: 5245
Reputation: 2869
If you want to implement a barcode scanner inside your app without depending on other apps you can use ZXing Android Embedded, you just need to declare its dependecies in your gradle dependecies and use its features inside your app.
To use it add the following to your build.gradle files (project/module):
repositories {
jcenter()
}
dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'
compile 'com.android.support:appcompat-v7:23.1.0' // Version 23+ is required
}
android {
buildToolsVersion '23.0.2' // Older versions may give compile errors
}
Now in your code you start a scanning activity this way:
public void scanBarcode() {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan the barcode");
integrator.setCameraId(0); // Use a specific camera of the device
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();
}
and process the results this way:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null && scanResult.getContents() != null) {
String content = scanResult.getContents().toString();
// content = this is the content of the scanned barcode
// do something with the content info here
}
}
More info can be found on the ZXing Android Embedded github repo, link below.
Source: https://github.com/journeyapps/zxing-android-embedded
Upvotes: 0
Reputation: 1210
I know I am quite late to answer here but all the folks looking for an updated answer to this question, no more a need to depend on third party apis, Google offers Barcode Scanning APIs via Google Play Services 7.8. Refer to CodeLabs, Documentation, Github Sample for more information.
Upvotes: 2
Reputation: 32031
ZXing is open source! If you realy want to implement your own barcode scanner then have a look in the source.
You can browse the code online here, it is licensed as Apache Licence 2.0.
Upvotes: 4
Reputation: 3910
Zxing has a great Intent-based API, and is designed to be used as a secondary app. I would recommend checking to see if the user has the Zxing app installed, and if not, redirect them to the Google Play store to download it.
Upvotes: 3