I am developing an Android application, I need to embed the Zxing scanner. The application should allow the user to scan a QR Code and then store the QR code ID of the product and parse it from an XML file. As yet, I have used the simple code:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage(getPackageName());
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
What this code does is, scans the product and bring me back to the previous screen of my app. I haven't included the entire library from Zxing as I wanted the Barcode scanner to handle it, but it seems I have to do more than I already have done.
You need to make an onActivityResult method that will get the callback once barcode scanner is done. Inside there you will handle the code string and do whatever you like with it.
/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// contents contains whatever the code was
String contents = intent.getStringExtra("SCAN_RESULT");
// Format contains the type of code i.e. UPC, EAN, QRCode etc...
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Log.i("TAG",format + "\t" + contents);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel. If the user presses 'back' before a code is scanned.
Log.i("TAG","Canceled");
}
}
}
This example just logs the results, you'll need to expand upon it to do whatever you want with the info you get back from the scanner.
Better still, use the IntentIntegrator class supplied with the project. It wraps up all the details listed here and its documentation already tells you exactly how to integrate it into your app. It deals with things for you like getting the app installed if not already.
Related
I am trying to make a barcode scanning app. I am stuck at the point where I am able to scan the barcode but now I want to show the barcode image along with decoded barcode number and other details on the screen and then provide a button to proceed to next screen. How should I go about it? I am unable to understand should I call an intent to new activity or the layout view. If I call the new activity, how do I pass the barcode that's decoded and other details to new activity?
Help.
Want something like this after scanning a barcode:
you can get and use that barcode anywhere, as:
uid is a textview where i have added the result from ZXing (Zebra Crossing) library activity.:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
Log.e("test 1",String.valueOf(requestCode));
if (resultCode == RESULT_OK) {
Log.e("test 2",intent.getStringExtra("SCAN_RESULT_FORMAT"));
Log.e("test 3",intent.getStringExtra("SCAN_RESULT"));
Toast.makeText(getApplicationContext(), intent.getStringExtra("SCAN_RESULT"), Toast.LENGTH_LONG).show();
uid.setText(intent.getStringExtra("SCAN_RESULT")) ;
} else if (resultCode == RESULT_CANCELED) {
Log.e("test 4",String.valueOf(requestCode));
}
}
}
Over Image you are seeing in sample image is a generated barcode not a actual picture captured from the camera.
For this you can use iText is a great Java PDF library. They also have an API for creating barcodes. You don't need to be creating a PDF to use it.
BarcodeEAN codeEAN = new BarcodeEAN();
codeEAN.setCodeType(codeEAN.EAN13);
codeEAN.setCode("9780201615883");
Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);
I am working on an android camera-based app with use of Intent. After capturing a photo I can see that photo and two buttons appear - "Save" and "Cancel". What I want to do is to not wait for user to choose one of these two buttons, but start processing this photo and then depending on the result of processing do futher actions.
I've been doing it this way so far :
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
protected void startCameraActivity()
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
// the method below is my method for setting proper path for my image file
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult( intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}
this method is invoked when I launch my app. So I start my app with camera.
Then I take a photo. And I can choose "Save" or "Cancel". When I choose one this method is invoked :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
onPhotoTaken(); // processing...
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
After receiving proper resultCode I load that image from file and then start processing it.
And now my quesiton is : If I can get that image before onActivityResult method is invoked? It is invoked after clicking on one of these buttons.
( I want to do it the similar way google googles does it - user captures a photo and that photo is being processed right away )
You're going to need to implement your own picture taking activity, something along the lines of this (which includes source code at the end of the page).
It takes some time to set it up straight, compared to using simple Intent, but after that you have direct access to all camera features, including camera image even before it's made available to the calling activity.
I have three buttons that onlongpress bring up my new IntentIntegrator scans via Barcode Scanner. I successfully have/had it scanning and using the code scanned to do something with when it is only one button.
How can I pass a value or something that when "protected void onActivityResult" is called it will know which button it came from so I can do different things with it depending on which button was long pressed.
My current setup is like this:
button1.setOnLongClickListener(this);
button2.setOnLongClickListener(this);
button3.setOnLongClickListener(this);}
}
public boolean onLongClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
//do stuff with the scan. But I want to do different stuff depending on which button was pressed.
Two options:
One, you can modify the IntentIntegrator source code so that it takes different REQUEST_CODES as constructor parameters (or just add a setRequestCode parameter). Then in onActivityResult, check for which requestCode was returned (each of your buttons would return a different request code). This would be the recommended approach.
Second option is the hacky one: In your code, create a member variable that tracks which was the last button pressed (check whether the view passed in onLongClick matches button1,button2, or button3), and use that information in onActivityResult to choose what to do with the results passed back from barcode scanner.
The Android platform have a number of "ready easy use" dialog such as
ProgressDialog, DatePickerDialog and TimePickerDialog, these are fire and wait
boxes, that is, they handle UI, right data and return something.
Is there a similar dialogbox for the mediastorage ?
I want something like "AudioFilePickerDialog" which shows a UI to the user
where the user pick a audio file and return the path/uri to the audio file.
Do I need to build this dialog box up myself or does it exists somewhere ?
One of the few examples I have found is
Given an Android music playlist name, how can one find the songs in the playlist?
but this handles playlists.
/Stefan
I found a tutorial for something like a FileChooser here. You should be able to make it only show Music-Files (like .mp3).
Also, to browser the SDcard of your Android Device, you can use the standard Java File-class, like in normal Java.
Try this
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_MEDIA);//REQUEST_MEDIA is some const int to operate with in onActivityResult
here you'll be brought a dialog (activity tbh) to choose an audio from mediastore.
Handle the result in onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_MEDIA && resultCode == RESULT_OK) {
String audioID = data.getDataString();
...
}
}
I am trying to produce
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
};
public 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");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
Using the libraries therefore not needing to install the software, I have had a look at the application that comes with it, and added CaptureActivity.java to my project to see if I can locate the core scanning loop but it kept just requiring more and more files (ended up with about 28 in total) and after I had no errors it still didn't work.
I am not looking into doing anything fancy, just on a button click open the capture layout, scan a code, return the code.
There are a few examples on how to decode local files but not the actual scanning side of it to detect a actual barcode.
So, what I help with is embedding the scanning code
Thanks
I think you're mixing up two things. The code you have shows you are trying to integrate via Intent, in which case you do not need any project code in your application at all.
The source in android/ is a complete stand-alone app (Barcode Scanner) and it is not written for you to copy (although the Apache License permits it). What you are doing is not supported, recommended, or encouraged -- integrating via Intent is much easier.
Write your own app, perhaps looking to the Android source code for inspiration, and that uses the core/ library for scanning. The core scanning happens in DecodeThread.