RSS

Barcode/QR Code Scanner And Encoder using PhoneGap, Android and Eclipse

09 Jul

Hi,

Thanks for visiting.

This content has been moved to new domain.
https://developerswing.blogspot.com/

Pease use the below URL to view the content:
Barcode/QR Code Scanner And Encoder using PhoneGap, Android and Eclipse

Thanks

How to use phonegap-plugins?

Hi all, in this tutorial I’ll show you how to use android phonegap plugins with eclipse.

First create an android project using eclipse and phonegap. If you are unable to do this please go through Hello World! with PhoneGap, Android and Eclipse tutorial.

Here I have created a project called ‘MyBarcodeScanner’.

barcode

So to use phonegap-plugins first you have to download it. Go to the following link given below and download the zip.
https://github.com/phonegap/phonegap-plugins

Now extract the phonegap-plugins-master.zip folder and open the it, go to the android directory,
here you will find all the plugings which you can use in your application. For this application I have used BarcodeScanner plugin, so go to BarcodeScanner directory and the version I have used is 2.2.0 as I am using Cordova 2.7.0 (If you are using Cordova 2.2.0 or greater please use the 2.2.0 directory).

Now I will slow you how to add phonegap plugin to your project.

1. First you have to add a ‘LibraryProject‘ into Eclipse. So go to File -> New -> Project -> Android -> Android Projects From Existing Code -> Next.

2. Now you have to browse the LibraryProject’s location (i.e. /phonegap-plugins-master/Android-Sw/phonegap-plugins-master/Android/BarcodeScanner/2.2.0/LibraryProject).
also I have checked copy projects into workspace.

barcode

3. Now right click on MyBarcodeScanner project go to properties. In the Android section under Library click the Add button and select the library i.e. CaptureActivity.

barcode

4. To use the plugin, move barcodescanner.js (/phonegap-plugins-master/Android/BarcodeScanner/2.2.0/assets/barcodescanner.js) to your project’s www folder and include a reference to it in your html files.

5. Create a folder called ‘com/phonegap/plugins/barcodescanner‘ within your project’s src folder i.e. MyBarcodeScanner/src.

barcode

6. And copy the BarcodeScanner.java(/phonegap-plugins-master/Android/BarcodeScanner/2.2.0/src/com/phonegap/plugins/BarcodeScanner.java) file into that new folder (MyBarcodeScanner/src/com/phonegap/plugins/barcodescanner/BarcodeScanner.java).

barcode

7. In your MyBarcodeScanner/res/xml/config.xml file add the following lines inside the tag.

 <!-- Used to initialize cordova class -->

8. Add the following activity to your AndroidManifest.xml file. It should be added inside the tag.


<!-- ZXing activities -->

9. Also you need to add the following permission in your AndroidManifest.xml file.


Now create a file called index.html into yor www folder and add the following code inside it.


		My Barcode/QR Code Scanner

			body{
				 background:#888888 none repeat scroll 0 0;
			}
			input[type='text']{
				width:18em;
				border:1px solid black;
			}
			input[type='button']{
				width:20em;
				border:1px solid black;
				color:#ffffff;
				font-family:"Times New Roman",Times,serif;
				font-size:0.9em;
				font-weight:bold;
				background:#737CA1;
			}

			function scanCode(){
				window.plugins.barcodeScanner.scan(
					function(result){
						alert("Scanned Code: " + result.text
						+ ". Format: " + result.format
						+ ". Cancelled: " + result.cancelled);
					},
					function(error){
						alert("Scan failed: " + error);
					}
				);
			}

			function encodeData(){
				var data = document.getElementById("data").value;
				if (data != ''){
					window.plugins.barcodeScanner.encode(
						BarcodeScanner.Encode.TEXT_TYPE, data,
						function(success){
							alert("Encode success: " + success);
						},
						function(fail){
							alert("Encoding failed: " + fail);
						}
					);
				}
				else{
					alert("Please enter some data.");
					return false;
				}
			}

		<h3>Barcode/QR Code Scanner And Encoder</h3>

		<br /><br />

		Data : <br />
		<br /><br />

Now run the android project and you will get the following output given below.

barcode

qrcode

 
84 Comments

Posted by on July 9, 2013 in PhoneGap Android Eclipse

 

Tags: , , , , ,

84 responses to “Barcode/QR Code Scanner And Encoder using PhoneGap, Android and Eclipse

  1. maher bg

    February 10, 2015 at 1:57 PM

     
  2. Nii Rayden

    February 24, 2015 at 12:04 PM

    Thank you very much for this tutorial. I am working on a final year project which on mainly on the use of qr codes to verify products. I followed this tutorial and it works however it does not display the scan results Can you please help

     
  3. ahmed

    March 30, 2015 at 11:59 AM

    thank you for the

    I’m using phone gap 2.9.0 and phone gap barcode scanner 2.2.0 when i make a scan i get alert message say “scanned code: xxxxxx, format EAN_8. cancelled: false “

     
  4. Santhosh Billu

    May 22, 2015 at 1:40 PM

    Is it support to all Mobile OS? like bada. Balckberry, windows phone so on

     
  5. kamanzi abubakar

    July 29, 2015 at 11:25 PM

    hey….can you please send me full source for that qr scanner…..my email …..kamanziabubakar75@gmail.com

     
  6. ameizah

    August 4, 2015 at 3:41 PM

    thank you very much for this tutorial. I am working on a final year project which on mainly on the use of qr codes to verify products. I followed this tutorial and it works however it does not display the scan results Can you please help?

     
  7. Sergio

    August 24, 2015 at 2:58 PM

    Is it possible to choose which camera to use? Front camera or back camera?

     
  8. sachinW

    October 15, 2015 at 2:59 PM

    Hi Mayukh, i have followed your tutorial without any errors. But after building apk file i faced some issues.
    1) My app works only on my avds which are having android version 4.0.4 or lower.Greater Android os emulators or mobile device does not work.

    2)I installed this app on my device Samsung gt-7582 having android jellybean 4.2.2. It gets installed and starts very well , but when i click on scan button it does not do anything . It does not give any error on my device. After looking in logcat i found that my mobile throwing error at ddms ” v10-15 19:17:45.869: E/Web Console(32709): Uncaught TypeError: Object [object Object] has no method ‘exec’:984 ” .

    So can you or any one else explain me upto which version of android this app will work on correctly, and what should i do if i want to make it work for newer versions of android.

    Note that this app works on android emulator having android 4.0.4 . I have tried on yourwave android.

    So, as per my diagnostic it is the problem related with android version and compatibility of this app means cordova.js version or phonegap version .
    Please comment if you find something . Also you can send me working project to me on ‘sachinware813@gmail.com’ if possible, so that i can compare it and dig into it.

    Thanx . Please reply ..

     

Leave a comment