I'm using ZXing Library as a library to my Android project. However whenever a barcode is scanned the ZXing Capture activity doesn't provide the result to my activity, it just stands there as if the code had been scanned normally via the application.
My current code is:
discount.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(DiscountActivity.this, CaptureActivity.class);
intent.setAction("com.google.xzing.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) {
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.discount_dialog);
dialog.show();
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
This code is wrong, and more complex than it needs to be. This is all you should be doing: http://code.google.com/p/zxing/wiki/ScanningViaIntent
Related
I am currently working with qrcode
I added Zxing library and also core.jar.
When I am trying to scan a document not getting any results.
qrscan_button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,
"Scan Result = " + data.getStringExtra("SCAN_RESULT"),
Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
}
}
}
I added CaptureActivity class in Manifest.
I am using Zxing 2.1
The code below works for me. However, how could I embed it with other layouts from another activity?
Basically, The screen will be splitted in two parts:
Fist one will contain a header and shared buttons to use in other screens;
Second one will be the Barcode scanner or any other screen.
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
}
}
}
thanks
There isn no way to do this. You are invoking a third-party app and can't control how it looks.
In my application I have scan button which scan qr code. Code is like this:
btnScan.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1);
ClearForm();
//if (!CheckCHFID())return;
pd = ProgressDialog.show(EnquireActivity.this, "", getResources().getString(R.string.GetingInsuuree));
new Thread(){
public void run(){
getInsureeInfo();
pd.dismiss();
}
}.start();
}
});
Now the problem is before I scan the code it starts finding the information which is getInsureeInfo(); How can I control that it should execute only after user scans the code successfully?
Thanks in advance.
you need to move the part that you want to happen after the scan to the onActivityResult() method.
/*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.
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel. If the user presses 'back' before a code is scanned.
}
}
}
Also I think you are going to have to use a Handler to send a message from the work thread to the main thread when it is time to hide your progress dialog. I don't think it will let you call dismiss on it from the background thread. That is just a hunch though, not tested.
put it in OnActivityResult method ,ovveride it.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1:
if (resultCode == RESULT_OK) {
//put your stuff here....
break;
}
}
}
Is it possible to recover the result of the creation of a QR Code ZXing in my own Activity (my xml file)?
Not sure if this is what you are asking for, but you you can scan a barcode using ZXING from one of your activities like this:
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}
catch (ActivityNotFoundException e) {
}
To do something with the barcode include this:
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");
Toast.makeText(this, contents, Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
You need to have the ZXING barcode app installed on your device.
This is my current code:
public void onClick(View v) {
final Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
final Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
final Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
pickIntent.putExtra(Intent.EXTRA_INTENT, camIntent);
pickIntent.putExtra(Intent.EXTRA_INTENT, gallIntent);
pickIntent.putExtra(Intent.EXTRA_TITLE, "Select Source");
startActivityForResult(pickIntent, 0);
if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
} else {
dialog = ProgressDialog.show(CreatePod.this, "Uploading",
"Please wait...", true);
//new ImageUploadTask().execute();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast toast = Toast.makeText(this,"camera cancelled", 10000);
toast.show();
return;
}
// lets check if we are really dealing with a picture
if (requestCode == 0 && resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");
//setContentView(R.layout.main);
imgView.setImageBitmap(b);
// save image to gallery
String timestamp = Long.toString(System.currentTimeMillis());
MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp);
}
}
This gives the options Gallery and Camera(Actually, it shows an unsupported device in the place of camera. If I click on it, it gives NullPointerException and crashes. Is this the right way to do it? Or should I use PackageManger? If Yes, then how?
Looking of your code, I think you have a lack of basic android knowledge,
Miss-use of onActivityResult(), its a method of Activity class, take out it from onClick()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
}
Just check basic android activity's method and how to use them.
Update:
Look at Android-Activity