• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps barcode and qr code programming

Sanjit singh

Newbie
Jun 4, 2016
24
0
m programming a caregiver app that involves barcode and qr code scanning for android. the main focus of the app is that it should only scan medication products. after it scans the meds full information should show, its name, information and should be saved in a folder on your phone to any specified location like gallery or anywhere. this is my code i have:

package com.example.testing;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {


static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";


@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void scanBar(View v){
try{
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}catch (ActivityNotFoundException e){
showDialog(MainActivity.this, "No Scanner Found",
"Download a scanner code activity?", "Yes", "No").show();

}


}

public void scanQR(View v){
try{
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}catch (ActivityNotFoundException e){
showDialog(MainActivity.this, "No Scanner Found",
"Download a scanner code activity?", "Yes", "No").show();
}
}

private static AlertDialog showDialog(final Activity act,
CharSequence title, CharSequence message,
CharSequence buttonYes,
CharSequence buttonNo) {

AlertDialog.Builder dowloadDialog = new AlertDialog.Builder(act);
dowloadDialog.setTitle(title).setMessage(message).setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {

@override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse("market://search?q=pname:" +
"com.google.zxing.client.android");

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try{
act.startActivity(intent);
} catch(ActivityNotFoundException e){

}

}
}).setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {

@override
public void onClick(DialogInterface dialog, int which) {


}
});

return dowloadDialog.show();

}
@override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
if(requestCode == 0){
if(resultCode == RESULT_OK){
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(this, "Content:" + contents + "Format:" + format,
Toast.LENGTH_LONG).show();
}
}
}


}

this is my xml file:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:eek:rientation="vertical"
tools:context="com.javacodegeeks.androidstartactivityforresultexample.ActivityOne" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_margin="20dp"
android:text="Scan"
android:textColor="#000000"
android:textSize="30dp" />
<Button
android:id="@+id/scanner"
android:layout_width="250dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:eek:nClick="scanQR"
android:text="QR Code"
android:textSize="18dp" >
</Button>

<Button
android:id="@+id/scanner2"
android:layout_width="250dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:eek:nClick="scanBar"
android:text="Bar Code"
android:textSize="18dp" >
</Button>
</LinearLayout>


Can someone help me with the focus of scanning for one specific product which is medications and medications only. other products that are not medicines should say invalid. Thank you
 
Does you meds have a special character attached to it? i.e. M-39484730387457 would be a med while another bar code would be this C-394ufirr for a customer. Then if the barcode has a M in it allow the scanner to scan. But if it does not then say invalid code.
what part of my code do i do that into. see im a beginner. i just started to learn
 
Upvote 0
First off lets figure out how your data is set up. Can you post an example of what one of the numbers look like that you are using to build the barcode.
Also are you using a QR code or a barcode. They are similar but we will have to handle them differently.
A QR code will have all the information in there so you will be able pull all of that out. A barcode can have that information but it will be encoded a little more
 
Upvote 0
First off lets figure out how your data is set up. Can you post an example of what one of the numbers look like that you are using to build the barcode.
Also are you using a QR code or a barcode. They are similar but we will have to handle them differently.
A QR code will have all the information in there so you will be able pull all of that out. A barcode can have that information but it will be encoded a little more
I am using both barcode and qr code:
This my code:



import android.app.Activity;

import android.app.AlertDialog;

import android.content.ActivityNotFoundException;

import android.content.DialogInterface;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Toast;



public class MainActivity extends Activity {





static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";





@override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



}



public void scanBar(View v){

try{

Intent intent = new Intent(ACTION_SCAN);

intent.putExtra("SCAN_MODE", "PRODUCT_MODE");

startActivityForResult(intent, 0);

}catch (ActivityNotFoundException e){

showDialog(MainActivity.this, "No Scanner Found",

"Download a scanner code activity?", "Yes", "No").show();


}



}


public void scanQR(View v){

try{

Intent intent = new Intent(ACTION_SCAN);

intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

startActivityForResult(intent, 0);

}catch (ActivityNotFoundException e){

showDialog(MainActivity.this, "No Scanner Found",

"Download a scanner code activity?", "Yes", "No").show();

}

}



private static AlertDialog showDialog(final Activity act,

CharSequence title, CharSequence message,

CharSequence buttonYes,

CharSequence buttonNo) {


AlertDialog.Builder dowloadDialog = new AlertDialog.Builder(act);

dowloadDialog.setTitle(title).setMessage(message).setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {


@override

public void onClick(DialogInterface dialog, int which) {

Uri uri = Uri.parse("market://search?q=pname:" +

"com.google.zxing.client.android");


Intent intent = new Intent(Intent.ACTION_VIEW, uri);

try{

act.startActivity(intent);

} catch(ActivityNotFoundException e){


}


}

}).setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {


@override

public void onClick(DialogInterface dialog, int which) {




}

});


return dowloadDialog.show();


}

@override

protected void onActivityResult(int requestCode, int resultCode, Intent intent){

if(requestCode == 0){

if(resultCode == RESULT_OK){

String contents = intent.getStringExtra("SCAN_RESULT");

String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

Toast.makeText(this, "Content:" + contents + "Format:" + format,

Toast.LENGTH_LONG).show();

}

}

}



}
xml file:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ffffff"

android:backgroundTint="@android:color/holo_blue_bright"

android:eek:rientation="vertical"

tools:context="com.javacodegeeks.androidstartactivityforresultexample.ActivityOne" >



<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center|top"

android:layout_margin="20dp"

android:text="Scan"

android:textColor="#000000"

android:textSize="30dp" />



<Button

android:id="@+id/scanner"

android:layout_width="250dp"

android:layout_height="80dp"

android:layout_gravity="center"

android:layout_margin="10dp"

android:backgroundTint="@android:color/holo_orange_dark"

android:gravity="center"

android:eek:nClick="scanQR"

android:text="QR Code"

android:textSize="18dp" >



</Button>



<Button

android:id="@+id/scanner2"

android:layout_width="250dp"

android:layout_height="80dp"

android:layout_gravity="center"

android:layout_margin="10dp"

android:backgroundTint="@android:color/holo_purple"

android:gravity="center"

android:eek:nClick="scanBar"

android:text="Bar Code"

android:textSize="18dp" >



</Button>

</LinearLayout>

a number. one example i have is 8901296026793. but that number is not a medicine product. right now it scan anything. i only want for it to able to scan medicine products.
 
Upvote 0
You have to have an identifier to tell the system that its meds. So do all meds start with a 8 or anything special?
What other barcodes does the product have on them. If its just one then that easy you don't have to worry about it then, but if there is multiple ones than you have to be able to differentiate between the two via a number or letter something that can tell the systems its a med.
 
Upvote 0
You have to have an identifier to tell the system that its meds. So do all meds start with a 8 or anything special?
What other barcodes does the product have on them. If its just one then that easy you don't have to worry about it then, but if there is multiple ones than you have to be able to differentiate between the two via a number or letter something that can tell the systems its a med.
as i said no i dont know what meds start with. i want it to have only one focus which are meds and i need help into telling my code how to do it. other numbers i have are 036632008404, 0826575000638, tec. random products are barcoded and qr coded. i just want it to have a focus on only scanning meds. nothing else. just meds.
 
Upvote 0
So its off the shelf meds. They won't have a special identifier on it, so you will need a database and look up the barcode number after the scan to get the information. So all you need from the scan is the number then link that number up to your database to download all the information you want off of that. Its pretty much going to be a walmart scanner that you will have to create.
So each barcode will act as the key to get the products information. That way you can just send the key to the database and access the information that you want from there.

Do you have a database of meds that your store is offering?
 
Upvote 0
in this medicine Nyquil there is only one barcode and a small qr code laid on the top
barcode is on the bottom
Take a look at this I think this is what you would want to do
http://www.mysamplecode.com/2011/09/android-barcode-scanner-using-zxing.html

You basically want to read a upc and get the information off of that
Thats fine but i dont ave the database of the meds my store is offering. i looked at the website and it is still the exact same code. that code doesnt have a spefic focus on scan. i just need help knowing wat code to use for medicine product scanning and where to put that code. the only focus the program should ahve for scanning is just medication products
 
Upvote 0
Upvote 0
You going to need a database to get the product information from the code.
Check out this guy here https://www.outpan.com
This guy apparently has a android plug in API so its worth a look
http://product.okfn.org

Here is also a few things to look at
http://opendata.stackexchange.com/q...database-of-all-products-with-ean-13-barcodes

and are you sure your scanning the right code? For the one you gave me its not nyquil its this
https://www.upcdatabase.com/item/8901296026793
so now how do i add the database to my code so when i scan med it goes to that database.
also lets say its scans something thats not a medicine. how doi make it print invalid product if the product it is barcode or qr code scanning is not a medicine.
 
Upvote 0
You will have to figure out which database is best for you. And once you scan a product if its is not in the database the API should send you a error message which means that the product is not in the database.
To add the database to your code you have to get an endpoint for it like a url. This way you can ask send a get request with the barcode in as a parameter to the database and ask to see if the number is in the database. The database should send back the information that you can then parse it out seeing it will most likely be a json response. Go and pick a database based off your needs and I will help you get it into your project.
 
Upvote 0
so now how do i add the database to my code so when i scan med it goes to that database.
also lets say its scans something thats not a medicine. how doi make it print invalid product if the
You will have to figure out which database is best for you. And once you scan a product if its is not in the database the API should send you a error message which means that the product is not in the database.
To add the database to your code you have to get an endpoint for it like a url. This way you can ask send a get request with the barcode in as a parameter to the database and ask to see if the number is in the database. The database should send back the information that you can then parse it out seeing it will most likely be a json response. Go and pick a database based off your needs and I will help you get it into your project.

product it is barcode or qr code scanning is not a medicine.
this is the datbase i like:
http://www.product-open-data.com/search/
so for htis database only medicine products from app should be scannable and saved to this database.
so now that i know which one i want how do i program it to my project and make sure its only medicine that are scanned. other products likes books, games or non medication product related should print me invalid scan.
 
Upvote 0
this is the datbase i like:
http://www.product-open-data.com/search/
so for htis database only medicine products from app should be scannable and saved to this database.
so now that i know which one i want how do i program it to my project and make sure its only medicine that are scanned. other products likes books, games or non medication product related should print me invalid scan.
so i know barcode will work will qr code work as well.
 
Upvote 0
this is the datbase i like:
http://www.product-open-data.com/search/
so for htis database only medicine products from app should be scannable and saved to this database.
so now that i know which one i want how do i program it to my project and make sure its only medicine that are scanned. other products likes books, games or non medication product related should print me invalid scan.
this is what i did to my code. it takes to to the database:
package com.example.testing;







import android.app.Activity;

import android.app.AlertDialog;

import android.content.ActivityNotFoundException;

import android.content.DialogInterface;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.widget.Toast;





public class MainActivity extends Activity {





static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";





@override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



}



public void scanBar(View v){

try{

Intent intent = new Intent(ACTION_SCAN);

intent.putExtra("SCAN_MODE", "PRODUCT_MODE");

startActivityForResult(intent, 0);

}catch (ActivityNotFoundException e){

showDialog(MainActivity.this, "No Scanner Found",

"Download a scanner code activity?", "Yes", "No").show();


}



}


public void scanQR(View v){

try{

Intent intent = new Intent(ACTION_SCAN);

intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

startActivityForResult(intent, 0);

}catch (ActivityNotFoundException e){

showDialog(MainActivity.this, "No Scanner Found",

"Download a scanner code activity?", "Yes", "No").show();

}

}



private static AlertDialog showDialog(final Activity act,

CharSequence title, CharSequence message,

CharSequence buttonYes,

CharSequence buttonNo) {


AlertDialog.Builder dowloadDialog = new AlertDialog.Builder(act);

dowloadDialog.setTitle(title).setMessage(message).setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {


@override

public void onClick(DialogInterface dialog, int which) {

Uri uri = Uri.parse("market://search?q=pname:" +

"com.google.zxing.client.android");


Intent intent = new Intent(Intent.ACTION_VIEW, uri);

try{

act.startActivity(intent);

} catch(ActivityNotFoundException e){


}


}

}).setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {


@override

public void onClick(DialogInterface dialog, int which) {




}

});


return dowloadDialog.show();


}

@override

protected void onActivityResult(int requestCode, int resultCode, Intent intent){

if(requestCode == 0){

if(resultCode == RESULT_OK){

String contents = intent.getStringExtra("SCAN_RESULT");

String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

Toast.makeText(this, "Content:" + contents + "Format:" + format,

Toast.LENGTH_LONG).show();

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.product-open-data.com/search/"));

startActivity(browserIntent);




} else if (resultCode == RESULT_CANCELED) {

// Handle cancel

Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);

toast.setGravity(Gravity.TOP, 25, 400);

toast.show();





}

}

}

}

but now how do i make it print the product number on the search engine on the site and if lets say to test it if i scan something thats not a medication product how do we make it say invalid product.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones