I have been learning Android application development lately and decided to write my first application, but I donโt want to build anything which is already being detailed in any tutorial.
Also I wanted to build something which I can later use in my projects. I am working with a huge e-commerce company and we are always looking for innovative ways to manage inventory. Products are identified by SKUs and EAN numbers which are printed on the product labels as barcodes. We have laser based barcode scanners used by fulfillment team to track and manage inventory. So I decided to make a simple barcode scanning app which will display the barcode and barcode format.
After some R&D online I came across a brilliant opensource library named ZXing ( Zebra crossing). ZXing is written in Java and it supports 1D/2D barcode images. A perfect match for my simple app. I am using Android Studio for my application development not eclipse because it is the official IDE and personally I found it more easy to work with.
Step 1 : Create a blank project in android studio
There are a lot of tutorials available online which can tell you how to create a project on android studio, so I am not detailing the process here. For reference click here.
We will use the camera to scan the barcodes, so we have to add permission for camera access in the AndroidManifest.xml file. Add the following lines to the AndroidManifest.xml file. This file is located at your-project-folder/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
Step 2 : Add ZXing library in your project
There are two ways of utilizing this library in your project. Either you use the official ZXing application and simply invoke the scanning intent. This will require the ZXing application pre-installed on user’s device. Or we can embed this library right into our app. We will use the later approach and embed the library right into our app. So user does not have to install the scanner app.
I have used ZXing Android Minimal which is basically an Android project which has already embedded the ZXing application. The beauty of developing with Android Studio is I can add this library as a dependency via gradle and Android studio will automatically download it.
So for the Android studio newbies you have to edit the build.gradle file located under your app not the one on the root. Put the below code in your build.gradle file.
// Added by raj
repositories {
mavenCentral()
maven {
url "http://dl.bintray.com/journeyapps/maven"
}
}
// Added by raj ends
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
// Added by raj
// 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'
// Added by raj ends
}
Step 3 : Update layout
We will create a simple scan button and assign a onclick function. We will also create two Text Views to display scanned barcode and the format. Now open your main activity layout. Android studio will open it in design mode. Switch to text mode and add the following code.
<Button android:id="@+id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/scan"
android:onClick="scanNow" />
<TextView
android:id="@+id/scan_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_centerHorizontal="true"
android:layout_below="@id/scan_button" />
<TextView
android:id="@+id/scan_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_centerHorizontal="true"
android:layout_below="@id/scan_format" />
Now open your resource string.xml file located under app/src/main/res/values/string.xml and add the following code
<string name="scan">Scan</string>
This will create the UI of our app
Step 4 : Scan the barcode
If you check the code we added in layout file we have assigned an onclick function to our button
android:onClick=”scanNow” />
Now open your activity’s main java class and add this method into it. You can find this in Android studio in left window under java folder. Now add the below function
public void scanNow(View view){
Log.d("test", "Scan button works!");
}
Android studio will detect the dependencies and suggest you what all classes required to be imported import these classes. Now Run you application after clicking on scan button you will see “Scan button works!” message in logcat.
Now we will scan the barcode using the intentintegrator classes which we have added to our project by mentioning the dependencies in step 1. We will also add another function to handle scan result and display it on screen. We will also add a condition to handle the case when user cancel the scan and show an appropriate message. Below is the code.
/**
* event handler for scan button
* @param view view of the activity
*/
public void scanNow(View view){
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan a barcode");
integrator.setResultDisplayDuration(0);
integrator.setWide();ย // Wide scanning rectangle, may work better for 1D barcodes
integrator.setCameraId(0);ย // Use a specific camera of the device
integrator.initiateScan();
}
/**
* function handle scan result
* @param requestCode
* @param resultCode
* @param intent
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
// display it on screen
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
}else{
Toast toast = Toast.makeText(getApplicationContext(),"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
Now Run your application and scan barcodes.
You can checkout the source code here
If you want to integrate the barcode scanner in a fragment instead of activity then checkout my other post Android Barcode Scanner in Fragment
The scanner will launch in Horizontal Orientation, If you want to open it in Vertical Orientation then we need to modify the zxing library code. You can checkout Android barcode scanner vertical Orientation
You can also checkout Android QRCode scanner to know how to scan QRCodes
158 Responses to “Android Barcode scanner”
February 5, 2015
Aman Gargit is simply awesome.
A Good approach towards different Idea.
February 23, 2015
VictorGreat tutorial! It worked for me but I want to scan QR Codes too. Can you help me modify it to be able to scan QR Codes?
Thank You
March 4, 2015
Rajinder DeolHi Victor,
I have modified the code to scan QR codes, check my post : https://rajislearning.com/android-qr-code-scanner/
April 27, 2015
FathinI am trying this now for my final year project. i let you know if i can do this. can i have your email in case i want to ask you something regarding my project? thank you in advance. ๐
April 27, 2015
Rajinder DeolHi Fathin,
you can post your queries here I will reply it asap.
April 28, 2015
Fathini have downloaded the ZIP form the ZXing Android Minimal link u gave above and extract it. but i do not know how to include into android studio. can u help me? i am very new with all this.
April 28, 2015
Rajinder DeolHi Fathin,
Android studio uses gradle to build the apk and you can add this library as a dependency in gradle. Your project will have build.gradle file. Go to your project folder and under app directory you will find build.gradle file.
Read the “STEP 2” section of my article on what to add in the build.gradle file. When you will build your project Android studio will automatically download this library and include it in your project so that you can use it.
Lemme know if you still have any issue.
April 28, 2015
Fathinits me again. i have done as what u told. and it worked ! thank you. but i have another issue here. On this link https://github.com/journeyapps/zxing-android-embedded#custom-layout , under Custom Layout, it said that i need to provide two different layouts if i am using both zxing-android-embedded and zxing-android-legacy. how to provide both layout? is it i need to put both layout on main activity layout? sorry for too much asking.
April 26, 2017
TieuYeuhi Fathin, can i have your final project and source code?
i have a final project like this but i’m not IT student
please give me a help
May 8, 2017
vidyaHi Raj I am trying to flashlight option on toolbar.
I am trying to create using below link
https://github.com/journeyapps/zxing-android-embedded/blob/master/sample/src/main/java/example/zxing/CustomScannerActivity.java
https://github.com/journeyapps/zxing-android-embedded/blob/master/sample/src/main/java/example/zxing/ToolbarCaptureActivity.java
but I am getting exception
I/System.out: start flash activity
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.com.android_barcode_scanner, PID: 8655
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.com.android_barcode_scanner/in.com.android_barcode_scanner.FlashActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)
at android.app.ActivityThread.access$900(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:340)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at in.com.android_barcode_scanner.FlashActivity.onCreate(FlashActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6112)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)ย
at android.app.ActivityThread.access$900(ActivityThread.java:168)ย
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)ย
at android.os.Handler.dispatchMessage(Handler.java:102)ย
at android.os.Looper.loop(Looper.java:135)ย
at android.app.ActivityThread.main(ActivityThread.java:5753)ย
at java.lang.reflect.Method.invoke(Native Method)ย
at java.lang.reflect.Method.invoke(Method.java:372)ย
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)ย
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)ย
I/Process: Sending signal. PID: 8655 SIG: 9
Application terminated.
April 27, 2015
VivekCan I get the source code?
April 28, 2015
Rajinder DeolHi Vivek,
I have added all the code in the article, the purpose was you should try it by urself. However if you still need the code I will add it on GitHub and share the link soon.
April 28, 2015
Rajinder DeolHi Vivek,
you can get the source code here : https://github.com/rajdeol/android-barcode-scanner
April 28, 2015
Rajinder DeolHi Fathin,
You need to create two layouts if you want to support legacy phones as well. Both the layouts will be created in your layout folder and you will reference them while creating scan intent.
For reference I have uploaded my code on git hub https://github.com/rajdeol/android-barcode-scanner
you will do the changes in your main activity under “scanNow” function
May 1, 2015
Fathindid u mean that the both layouts already created in my layout folder? where can i find my layout folder?
May 4, 2015
Rajinder DeolHI Fathin,
Layouts will tell Android how should the barcode scanner screen should look like when we click on scan button.
As of now the default layout is shown camera is turned on and orientation of device is changed to horizontal with a rectangular box in the middle to scan barcode.
You can modify that screen by creating layout. You can find the layout folder in my code on Github : https://github.com/rajdeol/android-barcode-scanner/tree/master/app/src/main/res/layout
May 4, 2015
Fathini’m glad to read your response. thank you Raj.
but i kinda confused here.i need to provide two different layouts if i am using both zxing-android-embedded and zxing-android-legacy right? where do i create layout for zxing-android-embedded and where for zxing-android-legacy? i mean i just know the layout file is activity_home.xml only. sorry too much asking.
May 4, 2015
Rajinder DeolYou need to create layouts which will be in the same folder where activity_home.xml is.
Then you will add there reference in your activity class code. A good point to understand how to create layout is http://developer.android.com/guide/topics/ui/declaring-layout.html
May 3, 2015
FathinHai Raj. I have refer to your code on GitHub. my barcode scanner use the version v2.3.0. so, there’s a slight difference than yours. am i suppose to refer the ‘initial commit’? i hope u can guide me. ๐
May 4, 2015
Rajinder DeolHi Fathin,
the version of library does not effect anything in my code. The code is of a simple application to scan barcode using defaults.
You can change or extend the code if you want. You can clone Master branch and use it as you want.
May 5, 2015
FathinHai Raj. about the layouts, i think i’m gonna stick with your layout first because i am running out of time. now, i am trying to install into my phone. can i run the application direct into my phone or should i try using the emulator first? i am using ASUS Zenfone4.
May 7, 2015
Rajinder DeolHi Fathin,
The example will work on both emulator and device. However you should try it first on emulator just to be sure.
April 28, 2015
VivekHello Sir,
I tried myself but after barcode read is completed it is showing an error like Unfortunately app has closed.
I believe the mistake is with the
formatTxt.setText(“FORMAT: ” + scanFormat);
contentTxt.setText(“CONTENT: ” + scanContent);
can you guide me
April 29, 2015
Rajinder DeolHi Vivek,
you need to make sure both the text views are created in your layout check : https://github.com/rajdeol/android-barcode-scanner/blob/master/app/src/main/res/layout/activity_home.xml
line number 32 to 47
Also make sure a reference to these two layouts is declared in your activity class check : https://github.com/rajdeol/android-barcode-scanner/blob/master/app/src/main/java/in/whomeninja/android_barcode_scanner/HomeActivity.java
Line number 18, 25,26, 83,84
Regards
Rajinder
May 1, 2015
VivekHello Sir,
It worked. Thanks
I have a question. What is the code should I have to use to auto open the camera to barcode. I mean, don’t want to use the Scan button.
Thanks in advance
Vivek
May 4, 2015
Rajinder DeolHi Vivek,
you can call the “scanNow” function directly in “onCreate” function.
Remove the “Scan” button from layout and remove “View” argument from “scanNow” function to make it work.
ref file :https://github.com/rajdeol/android-barcode-scanner/blob/master/app/src/main/java/in/whomeninja/android_barcode_scanner/HomeActivity.java
May 7, 2015
Fathinhai Raj,the barcode scanner worked on my phone. do you know how to make the barcode scanner read my database? it is like this. when the barcode scanner scanned a barcode, the screen will show type of barcode, the barcode of the product, when the barcode product is in my database, then screen will show ‘Verified’. do you know how to do it?
May 7, 2015
Rajinder DeolHi fathin,
the answer is not that simple. First of all where is your database situated?. If it is on your webserver than you have to make a http call to your server to verify the barcode.
I am in process of publishing a post explaining how to make secure calls to your server.However for other storage options you can read : http://developer.android.com/guide/topics/data/data-storage.html
May 8, 2015
Fathinokay. let me know if u have published that post. im waiting. thank you so much. ๐
May 18, 2015
Rajinder DeolHi Fathin,
sorry for the late reply I was traveling to Sydney. I have published the post. You can check it out at :https://rajislearning.com/writing-secure-rest-webservices-with-php-for-android/
May 8, 2015
Fathinabout the database. if the database i created on my own (because only two products in my database as this is only my prototype for my final year project), where should i create the database? and how to make my barcode scanner app read my database?
May 18, 2015
Rajinder DeolYou can create the database on the device itself for the sake of prototype, but in real life scenario the database will be on a remote server.
Ideally you should create your database on remote server. Get shared hosting from godaddy or some other hosting provider and create your MYSQL database there.
Then use my code at : https://rajislearning.com/writing-secure-rest-webservices-with-php-for-android/
to access this database using PHP.
March 19, 2017
SOBIATHANK YOU SIR,GREAT TUTORIAL,I NEED HELP
March 19, 2017
Rajinder DeolHi Sobia,
thanks for the kind words, how can I help you ?
May 21, 2015
PrincipianteHi. Is posible to modify UI of zxing application? If yes, do you know how? Or have you got any imagination how would be posible to change it? Thank you in advance.
May 21, 2015
Rajinder DeolHi Principiante,
Author of the zxing library has updated the version and current version has removed layout APIs.
But instead they have exposed lower level controls by which you can decide your own layout.
check : https://github.com/journeyapps/zxing-android-embedded#customization
I still have to try this update.
June 1, 2015
Iqra RasteeThe Scanner gives me different result every time.
Can you help
June 2, 2015
Rajinder DeolHi Iqra,
you can try with commercial barcodes the ones on corn flakes boxes or on flat surfaces. Barcodes on round surfaces will not work properly like on white boad markers or pens. Or for testing try this image http://www.barcodesaustralia.com/wp-content/uploads/2011/10/samplejpg.jpg
June 29, 2015
Oluwawibe ChristianHi Rajinder, it works like magic. Thank s for saving me a lot of time and energy. Thumbs up
July 20, 2015
jel2xA very very Big Thanks!!! to you Mr. Rajinder Deol, you’ve save my time, Ive been searching this guide the whole time, and you provide a more detail guide thanks for sharing your knowledge again. ๐
July 24, 2015
BiltonJust liked this post on FB. Thanks for the truly amazing read.
July 25, 2015
werPlease Help!!! With This code, how can i add another activity that will input/encode stings and will generate it into qr codes ..
July 27, 2015
Rajinder DeolHi Wer,
you can check my post on QR code here : https://rajislearning.com/android-qr-code-scanner/
July 29, 2015
SanketHi. I am a noob here. please help.
I need to incorporate specific functionalities, like the camera detecting the bar code(live) and enclose it in a rectangle. Also to intuitively direct the user to shift the camera so that the procedure is not much of a burden. Is it advisable to turn on the phone torch(flash) while detecting? If yes,how can it be done to the exact amount of light required for successful scanning, at that moment of time?
July 31, 2015
JackHi, my application design is in portrait mode, is it possible to set the barcode camera to portrait mode instead of landscape mode?
August 1, 2015
Rajinder DeolHi Jack,
landscape mode give user a better scan area to scan Barcode, specially on mobile phones.
However if you want to display in portrait mode check my post android-barcode-scanner-vertical-orientation-and-camera-flash.
March 21, 2016
KrishI commented the integrator.setWide(),but still it is showing in landscape mode.Is there any other alternatives ? I have to implement it in portrait mode.
February 15, 2017
Rajinder DeolHi krish,
the current library does not easily support portrait mode and integrator.setWide() will not help much.
However I have written a new post to achieve this, please read : https://rajislearning.com/android-barcode-scanner-vertical-orientation-and-camera-flash/
August 6, 2015
Marcelo RuizHello Thanks so much for the tutorials, when I open the scanner it show some numbers, is there anyway I can remove it from the scanner (camera view) or what the numbers mean?
http://imgur.com/BXHrAFD
August 11, 2015
Buad Beyhi!Awesome tutorial!I have troubles when I try to do not with
ActionBarActivity but with Fragment! I get stuck with IntentIntegrator integrator = new IntentIntegrator(this);
when i launch it it get stuck when it finish scanning
Can U help me??
Thank You!
August 29, 2015
The Digital BridgesThis post must have got a little while to write. Good stuff.
September 3, 2015
FarahHi! Sorry for taking your time. I have problems at step 4(main java class) which are cannot resolve symbol ‘r’, ‘log’ and ‘view’. What does it mean? I’m very new in android studio and currently make progress on my final year project. i really hope u can help me. Tq!
November 23, 2015
Rajinder DeolHi Farah,
sorry for the late reply, this error generally comes when your project dependencies are not compiles.
Rebuild your project and it should resolve them automatically.
September 16, 2015
AlexanderHola Rajinder Deol, al ejecutar la aplicaciรณn recibo estos errores en el mรฉtodo scanNow
“Cannot resolve method”
integrator.setResultDisplayDuration(0);
integrator.setWide();
No encuentra esos dos mรฉtodos… Saludos…
September 18, 2015
detectiveI used this codes on my tablet but the test app never scans… ๐
November 23, 2015
Rajinder DeolHi Can you provide the details of error your getting.
Also have you compiled and build the project on Android studio coz it has dependencies on third party libraries which will be downloaded when you build it in android studio
September 26, 2015
Ajay MelwaniIn Step 2, where exactly do we add the code. Do we add it in GradleScripts>build.gradle(Project:BarcodeScanningApp) or GradleScripts>build.gradle(Module:app)??
In Step 4, where do we add the code
public void scanNow(View view){
Log.d(“test”, “Scan button works!”);
}
?. Should we add it under
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);??
and where do we add the rest of the codes under Step 4??
Thanks in advance and waiting for your prompt reply.
November 23, 2015
Rajinder DeolHi Ajay,
I have added the complete project code on GitHub check this URL : https://github.com/rajdeol/android-barcode-scanner/blob/master/app/build.gradle#L37
It has the build.gradle file where you have to add the dependencies. However the repository has full project code so you can reference rest of the files to get better picture. Moreover drop me a comment if you still have an issue.
October 28, 2015
oakley sunglasses outletThanks very interesting blog!
October 30, 2015
SachinThanx a lot really helped me a lot to install my app without any further barcode scanning app
November 11, 2015
MarkusHello Raj,
firstly, thank you for your guide on how to create this kind of an app. It worked for me.
Now I wonder how could it be possible to catch the scanned barcode content and save it either manually or automatically into the device or a server.
I’ve just started my journey with programming, so very in-the-basics information could be useful..
Warm wishes,
Markus
November 23, 2015
Rajinder DeolHi Markus,
All the code to read the barcode is available in my github repository click-here.
If you go to this file https://github.com/rajdeol/android-barcode-scanner/blob/master/app/src/main/java/in/whomeninja/android_barcode_scanner/HomeActivity.java#L79 and look at line number 79 you will see the code receiving the barcode.
I am using this and displaying it in the test field you can use it to send it back to a server and save in a database. Try and create a simple PHP page at any website and post this value to that PHP page. You will find plenty of tutorials which will guide you how to handle POST request on PHP page and save the data in database.
Just for precaution read my blog post on writing secure REST API to make you PHP page which receives POST request from mobile more secure.
December 27, 2015
Cielesboudeuses.Jimdo.comYeah, the more 3CX gentle phones will try this, together with to the cellular apps
January 31, 2016
MeghanaHi, i am new to Android application development. Could this Bar code scanner code be changed to recognize other shapes like basic geometric shapes, arrows, playing cards and so on. How would that be done? Also will the zxing library be used then or some other library has to be used?
February 1, 2016
Rajinder DeolHi Meghana,
There are libraries available which can recognise shapes but to use them with android phones those libraries need to be ported as Java APIs so that you can interact with them. Like passing the image and fetching response from it.
There is already a library called opencv which you can check at opencv.org
Let me know if it helps
January 31, 2016
MeghaHi, can this code be tweaked for recognizing other shapes? Please help
February 25, 2016
AdolfoI do believe all of the ideas you’ve introduced for your
post. They are really convincing and will certainly work.
Nonetheless, the posts are very quick for starters.
Could you please prolong them a little from subsequent time?
Thanks for the post.
March 12, 2016
KinjalHow to redirect to webpage after scanning Barcode??
March 13, 2016
Rajinder DeolHi kinjal,
This is a mobile app not a web browser so you cannot redirect user. However you can still open a webpage in app using webviews.
But if you have to open a URL and you don’t want to write code for webviews, you can open the url in web browser like chrome for android.
Here is a quick example how to do it http://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application
March 14, 2016
KinjalI want to open a webview according the barcode result.It is possible?
March 16, 2016
khalilHI ,why barecode is not detected by my phone,result is usually null :'(
March 16, 2016
khalilRiahihi ,why barre code is not deteted by my phone,result is null return
April 7, 2016
chetan joshihi sir,
it is showing barcode scanner not installed
April 30, 2016
Rajinder DeolHi chetan
Have you compiled this project from my GitHub on your local ..? If yes what is the error you are getting in your error log
April 28, 2016
Amruthahi!
iam working on a project wherein we have to send the scanned barcode UPC(Universal product code) to Mysql db.How do I do this? Kindly guide:)
April 30, 2016
Rajinder DeolHi Amrutha,
You can send the scanned barcode back to web service which can then save it in a MySQL database.
If you need further assistance drop me a mail at deol.rajinder at gmail
June 7, 2016
anas nainHi raj,
Thanks for the posting this article.
Could you tell me how to show scanner screen inside some custom view like frame view?
June 24, 2016
Iris KeeseThis is really good tips i will link this information to my blog, keep posting!
June 30, 2016
SolidworksThanks so much for the blog post. Awesome.
August 22, 2016
DhruvThe “onActivityResult” doesn’t work in Fragment,do you have a solution for that ?
August 23, 2016
Rajinder DeolHi Dhruv,
I have not tried it on fragmets yet, but the problem is already addressed on stackoverflow here
let me know if this helps.
Rajinder
December 12, 2016
Rajinder DeolHi Dhruv,
Please read my post https://rajislearning.com/android-barcode-scanner-in-fragment/
December 12, 2016
MarcinHi Rajinder,
very nice tutorial – thank you.
Quick question – how to keep scanning screen in portrait mode?
I’ve tried to set it up in AndroidMainfest but it doesn’t work, can you advice please?
December 12, 2016
Rajinder DeolHi Marcin,
Keeping the screen in portrait mode is not that simple and require quite a bit of changes in the current library
I am in the middle of doing that and will write a post soon.
February 15, 2017
Rajinder DeolHi Marchin,
I have written a detailed post on how to change the scanning screen in portrait mode please read : https://rajislearning.com/android-barcode-scanner-vertical-orientation-and-camera-flash/
January 6, 2017
jitenSorry, the Android camera encountered a problem. You man need to restart the device.
I got his message. so I restarted device but still getting that issue and not able to scan barcode.
January 6, 2017
Rajinder DeolHi Jiten,
Sometimes your app need to have camera permissions. Add the following into your Androidmanifest.xml file :
<uses-permission android:name=”android.permission.CAMERA” />
this file is located at /app/src/main/AndroidManifest.xml
Let me know if it works, if you still have the issue then share the logcat log message as well.
December 17, 2017
MattI had to follow these instructions as well to fix the camera bug:
For Android 6+, because of the “permission” issue, If you got the message “Sorry, the camera encountered a problem. You may need to restart the device.”, go to Settings – Apps – find “your app name” – select Permissions and switch on “Camera”.
Source:
https://stackoverflow.com/questions/9028735/getting-camera-error-in-zxing-barcode-application
December 17, 2017
Rajinder DeolThanks for your valuable suggestions Matt.
March 6, 2018
Abuzar AnsariThank u so much matt, it works perfectly fine
January 23, 2017
SuchiHi Rajinder Deol,
To scan the barcode I am using zxing library what ever you mentioned I tried Like that only I got it
But I want to apply small modification like when I am trying to scan the camera flashlight should be on ..once the scanning is over the flashlight should be off automatically,
I am unable to get this,
Will you please help me
January 24, 2017
Rajinder DeolHi Suchi,
I am glad that my post is helpful to you, we are using Intent-integrator in this app to launch barcode scanner.
Intent has fixed methods to be used and unfortunately it does not have a method to specify camera flash.
I am in the middle of writing another post to add some modifications to this basic app. I can include camera flash control in the post as well.
I am planning to publish the post over the weekend, if you can wait till then it will be great.
If you need the changes a little early then feel free to drop me an email on deol.rajinder at gmail dot com.
January 23, 2017
SuchiHello hii
Please give me the solution for the above problem(camera flashlight) since two days I am trying
I used the code same what ever you mentioned in github link(https://github.com/rajdeol/android-barcode-scanner/blob/master/app/src/main/java/in/whomeninja/android_barcode_scanner/HomeActivity.java) and I used the gradle file is also same
Please help meee bosss
January 24, 2017
SuchiHi ,I am using the same code ,,it helps me alot but i want to add an extra feature as I am trying to operate flashlight while scanning the barcode
But I am unable to get this
Will you please help me ,how it will be possible
May 10, 2017
VidyaHi suchi,
try this
https://github.com/journeyapps/zxing-android-embedded/blob/master/sample/src/main/java/example/zxing/CustomScannerActivity.java
May 21, 2017
Rajinder DeolHi Suchi,
I have added flash-on and Flash-off buttons on the scan screen you can read the full details at Android Barcode Scanner vertical orientation (portrait mode) and camera flash
January 26, 2017
IgorPlease tell me how to add the ability to read the EAN / UCC-2 code?
January 26, 2017
IgorClarify.
This EAN-13 bar code with an additional 2 or 5 characters
eg 1234567891234 00123
I need a scanner that reads only these 2 format (15 or 18 characters)
January 27, 2017
Rajinder Deolhey Igor,
this scanner will read all the barcode formats including EAN-13 and ISBN number on books.
January 27, 2017
IgorGood afternoon
Thanks for the answer. I tried to scan the barcode of EAN-13, but the result – 13 characters in a number, not 15.
(4820020830281 03).
February 8, 2017
Rajinder DeolAs of now it is scanning EAN-13 character only. I can do some research to find if there is a full barcode reader.
March 2, 2017
ChaitanyaI tried including the camera permissions in the android manifest file, still I am getting the message on pressing the scan button saying “Sorry the android Camera encountered a problem. You may need to restart the device.”
Any help will be much appreciated to solve this
Thank You
March 2, 2017
Rajinder DeolHi Chaitanya,
are you testing the app on a device or on emulator. Try it on a device I am not sure if emulator and launch a camera.
Can you share code in your Androidmenifest file as well please.
March 3, 2017
Chaitanya BhureHi
The issue is sorted now I downgraded the target sdk version and it works perfectly now. Thank you so much.
Can you help me with how to integrate google search with the barcode data received? Like what is the product based on the decoded barcode number which we get in content text view. Any help will be much appreciated.
April 1, 2017
Sahabat DeveloperHI, How to camera scanner auto focus and on light, because if barcode small cannot read.
Thank You..
April 2, 2017
Rajinder DeolHi Sahabat,
auto focus is a camera feature and it is turned on by default if your phone supports, also you can turn on the flash by using volume up and volume down buttons. If you want the camera flash to be on by default then please read my post : https://rajislearning.com/android-barcode-scanner-vertical-orientation-and-camera-flash/
April 5, 2017
VidyaHi Raj,
Nice tutorial. I have one question . Is there any other option to keep flashlight ON like we can provide button to start flashlight instead of pressing volume UP button,because every user is not aware of how to start flashlight in app. Need help.
April 5, 2017
Rajinder DeolHi Vidya,
yes it can be added, I have written another post to modify screen orientation of scanner and turning on the flash by default. Check https://rajislearning.com/android-barcode-scanner-vertical-orientation-and-camera-flash/
Also the exact code is at my github here : https://github.com/rajdeol/android-barcode-scanner-bulk-scan-with-flash/blob/master/app/src/main/java/in/whomeninja/android_barcode_scanner_bulk_scan_with_flash/TorchOnCaptureActivity.java#L25
If it is still too confusing then I will write a post soon to add this on scan screen.
April 6, 2017
VidyaHi Raj,
Thanks for reply.
If it is possible then please post with this change.
April 10, 2017
Inderjeet KaurHi Raj,
I have downloaded the link of source code from the Github I import it in Android Studio and from last 2 hours I am trying to build this… But it takes too much time to import. Can you tell me what is happening ?? downloading of the gradle libraries is done automatically and it is updating indices .. Waiting to run this code. please help
Thanks
April 11, 2017
Rajinder DeolHi Inderjeet,
when you build the project first time it will take some time to download the libraries as well as SDK version mentioned in the manifest file. If it is taking too long then look into the logcat and see which task is running. Let me know if you need more help.
April 11, 2017
vidyaRaj Do u know any third party api to validate product using barcode?
to validate product i have to create my own database and server side database. Am I right?
April 11, 2017
Rajinder DeolI have no clue if any free API is available to share product barcode, for your app if you have product data already then you can import it into a database and then call it from your app.
April 21, 2017
LucyHi, I would like to use the zxing barcode scanner inside my app without calling an activity. What I want is to change fragments only, to pass to the user He/She is not openning any activity, because my app is made of fragments just. This is to make sure the bottom menu I have always to be shown. How can I do this?
April 21, 2017
Rajinder DeolHi Lucy,
you can use the barcode scanner in a fragment, I have written a post on it. Please read Android Barcode Scanner in Fragment
April 24, 2017
vidyaHi Raj,
can you Please provide above example for flashlight with button?
April 26, 2017
Rajinder DeolHi Vidya,
I didn’t get chance to start it as I was travelling, I will do it this weekend and share the link.
April 26, 2017
VidyaOk raj. If i want to show alert dialog box if there was any error in scanning barcode then where i have to fixed it?
April 27, 2017
Rajinder DeolHi Vidya,
it is already being handled, I show a toast when there is an error in scanned result check the code on my github at https://github.com/rajdeol/android-barcode-scanner/blob/master/app/src/main/java/in/whomeninja/android_barcode_scanner/HomeActivity.java#L87
April 29, 2017
cristianHi Rajinder, many thanks for your tutorial, you save me a looooot of time.
My small question is: can I limit the scan to only Code39 to have a faster scan?
On this line
“integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);”
I cannot see the single codes, just group of them.
But I remeber on Mobile Vision library there was this possibility.
Thanks in any case!
May 1, 2017
Rajinder DeolHi Christian,
yes you can set the Barcode formats to One_D_Code_Types. In the original Library there is a list of all supported 1D codes at : https://github.com/zxing/zxing/blob/master/android-integration/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java#L126
If you want to just implement one type then you can override and pass a list with just one value to your intent integrator.
May 16, 2017
VidyaHi raj,
I got solution for flashlight.
If we want to implement flashlight on button.
In zxing there is one Class
“DecoratedBarcodeView” which contains setTorchOn and setTorchOff method.
Just call them.
I implement it for my toolbar option like
if (id == action_flash) {
try {
if (isFlashOn) {
barcodeView.setTorchOff();
isFlashOn = false;
} else {
barcodeView.setTorchOn();
isFlashOn = true;
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(“start flash activity”);
}
May 21, 2017
Rajinder DeolHi Vidya,
Great effort, I have also used the same methods and added buttons on the scan screen. You can read Android Barcode Scanner vertical orientation (portrait mode) and camera flash for full details.
June 11, 2017
kymhey can one create a list view, save data of scan and also calculate total.
June 13, 2017
Rajinder DeolHi Kym,
yes you can do bulk scan and display the scanned item in a list with count. I am in middle of writing post for bulk scan I can add this to help you out.
June 22, 2017
SureshHi sir,
I am using One_D_Code_Types in our app and my issue is that sometime scanner gives wrong content number so how can solve this issue.
June 23, 2017
Rajinder DeolHi Suresh,
this library processes barcode from the captured image, so, sometimes if the image is not that clear it might generate wrong results. You can try a different phone with a better camera and turn the camera flash on while scanning and check if that helps.
Read Android Barcode Scanner vertical orientation (portrait mode) and camera flash to add controll for camera flash.
July 18, 2017
poryathere are two “scanNow” functions in mainActivity.
and this is a Gross error .
what is the solution?
July 18, 2017
Rajinder DeolHi Porya,
I write posts here to help everyone and in return I expect them to at least ask proper questions.
I am not too sure if you have read the post or even had a look at the code. If you look at the code the mainActivity is actually HomeActivity and if you look at the source code which is publicly available at my github at here, there is just one “scanNow” function.
Moving on, there is no harm in asking any question even if it seems stupid or silly. I always try to answer all the questions to my best ability but being disrespectful and saying “this is a Gross error” shows how ignorant you are.
You asked “what is the solution?” well the solution is pay attention and read, link to the source code is there in the post.
July 19, 2017
poryaMy intention was to leave this comment in disrepute.
When I’m reading this post, I expect the solution to show me right here, rather than referring elsewhere.
Also, compare the code in this page with the github codes and you will see the obvious difference between them easily.
And thanks for using the stupid and .ignorant words
July 19, 2017
Rajinder DeolPorya,
Asking questions is not stupid or a bad thing but asking question without reading is stupid and using disrespectful adjectives in your question is ignorant.
This blog post is to only focus on barcode scanning and you would have know it if you have read the post, also github is to give you full code of the application so that you can clone it and build it on your system to test.
If you are new to Android Application development then this is not the right post to start with, you need to understand the basics of Android app development and then you can follow this post. You can read more about android app development by clicking here.
July 19, 2017
poryahello
please tell me how to get information about the products?
July 19, 2017
Rajinder DeolThis application only scans barcode and tell you the barcode number. Product data is saved by individual companies in their own database.
September 7, 2017
tatugreat code tnx a lot
December 7, 2017
Aakarsh RaiHi Raj,
Can be on Flashlight when IntentIntegrator Activity takes place without using of volume up button is this possible?
December 9, 2017
Rajinder DeolHi Aakarsh,
yes it is possible, I have written a detailed post on this, please read Android Barcode Scanner vertical orientation (portrait mode) and camera flash.
December 11, 2017
Aakarsh RaiDear Raj,
I appreciate your effort but i want to on flash light on create in Activity do not want to click on button that u provide.the logical u used same as volume up button….there is any or other logic to on flashlight on create without using of click any button..
Thank to Wonderful tutorial surely help needed one.. You are simply awesome ๐
December 11, 2017
Rajinder DeolHey Aakarsh,
thanks for the appreciation, flash should only be turned on when scan is in progress and I have added section titled as “Turn the flash on by default on scan” in the post Android Barcode Scanner vertical orientation (portrait mode) and camera flash. It will turn the flash on when the scan starts, no need to press the volume up button.
However if you want to turn the flash on at anytime you can use the Camera API, there is a detailed answer on stackoverflow which you can use
December 11, 2017
Aakarsh RaiDear Raj,
I use all of those stuff that u provide me they are not working for me when i use CameraAPI before initiateScan() process that trun on flashlight for while after that Zxing lib take place and scanner opens flashlight off..After All Process done According to Zxing Functionality… i am so much tried to do that stuff all kind of things i use in my code but nothing do my work perfectly ๐
December 11, 2017
Aakarsh RaiDear Raj,
I use all of those stuff that u provide me they are not working for me when i use CameraAPI before initiateScan() process that trun on flashlight for while after that Zxing lib take place and scanner opens flashlight off..After All Process done According to Zxing Functionality… i am so much tried to do that stuff all kind of things i use in my code but nothing do my work perfectly ๐
December 16, 2017
Rajinder DeolHi Aakarsh,
can you share your code via github or bitbucket, I can have a look and suggest something. However you can use both, turning on the flash by Camera API and then using my code to turn on the flash by default. Also turning the flash on again in the onActivityResult function.
December 13, 2017
adminhow to barcode match get json data in android ? plz help me …..
December 14, 2017
Aakarsh Raipublic void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == IntentIntegrator.REQUEST_CODE) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
String str = intent.getStringExtra(“SCAN_RESULT”);
}
}
}
In onActivityResult u get Scan Barcode Data in scanResult Variable ..
December 26, 2017
admingive me barcode match code and how to match barcode ….please
December 28, 2017
yasinhello sr iam students of campus .i want use barcode to store the information of items so how i handle this works….how to develop please please help me
December 29, 2017
Rajinder DeolHi yasin,
first of all try and run the barcode scanner app on your device, this will give you an idea of how app runs. Barcode is just a code, it will not give you the detail of the items.
December 29, 2017
Pravinhi im new to android studio i want to scan barcode using camera can u help me
December 29, 2017
Rajinder DeolHi Pravin,
Learn the basics of android studio and build the app detailed in the post on your device. This app will scan barcode using device camera.
Best place to learn is follow documentation on the Android Studio website here is the link https://developer.android.com/studio/intro/index.html
January 4, 2018
PravinHi sir i build the barcode scanner app but in that app i want to store some barcode values and items. i dont know how to add help me
January 4, 2018
Rajinder DeolHey Pravin,
you first need to create an activity with input fields to add item data. You can save that item data in local storage. Go through my post on Aadhar card reader. There is code for saving and reading data from local storage which you can use.
February 7, 2018
sunil aletierror :
getMenuInflater().inflate(R.menu.menu_home, menu);
cannot resolve menu_home
help me bro
February 8, 2018
Rajinder DeolHi Sunil,
can you share more details, which file or line number where you are getting this error ?
April 3, 2019
MemoryThanks for the great tutorial, but i need help i’m developing an android barcode scanner and i want to link it to xampp database. I managed to do the code following your tutorial and i created a php file but the problem is that when i scan the barcode it retrieve an empty activity.
April 4, 2019
Rajinder DeolHi Memory,
can you share more details? are you getting any error in the logcat. Also can you share your code. If possible push your code into a git repository (https://bitbucket.org/) and share it with me so that I can have a look.
April 4, 2019
MemoryThank you Rajinder let me share the code with you. There are no errors in my logcat.
April 4, 2019
MemoryThank you Rajinder let me share the code with you. There are no errors in my logcat.
June 6, 2019
Shiva PanchalHi
I have a requirement which is for my company need.
I have to scan the barcode and save its data into one excel sheet and save that excel sheet at some place. Can you please share the code for this.
I am totally new to the Android development.
June 23, 2019
Rajinder DeolHi Shiva,
The code you need is already available in parts in my two posts Android Barcode scanner and Android Aadhaar Card Scanner.
You will still need some understanding of Android development if you want to build the solution which you are looking for.