Access to QR code coordinate in ZXing on Android - android

I'm developing a ZXing project in Eclipse:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view)
{
Log.d("test", "button works!");
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);
b1 = (Button)findViewById(R.id.button111);
}
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");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
I found the finder pattern coordinates are in the Detector class (getTopLeft and getTopRight and getBottomLeft) but when I want to access them I'm facee with errors in the code. I don't know how I can access these variables. For example, I define a public static FinderPattern topLeft1 in the Detector class and in the processFinderPatternInfo method I add topLeft1=topLeft.
After startActivity for the result I add:
float m1=Detector.topLeft1.x;
m2=Detector.topLeft1.y;
b.setText("(x,y)="+m1+","+m2);
but when I run the program and click the button to scan: I get:
unfortunately zxing has stopped
I need 10 reputation to post an image and I can't post logcat image :-(
Can any one help me soon?
Thanks a lot

I solved my problem.I must write OnActivityResult class out of OnCreate Method! That's all!
thank you all.

Related

Use the result of a QR code scan to open an image in a new android activiy

I have a program for an android application which has 2 activies MainActivity and CarteActivity. I have a button on the MainActivity layout (activity_main.xml) which launches a QR code scanner (already programmed using zxing). The second activity layout (activity_carte.xml) has an ImageView which code is :
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/carte0" />
`
In my drawable folder I copied some pictures named carte1, carte2, carte3...
I also generated a few qrcodes which result is also carte1, carte2, carte3... (as a text)
Now I want that when I scan these qrcodes, the CarteActivity appears and the ImageViewer displays the picture corresponding to the code (when i scan the code which returns carte1 as a result, the picture carte1 appears in the ImageViewer), but I don't know how to.
I noticed that in the MainActivity the result of the scan is memorized in the string "contents" :
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");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
`
But I don't know how to use it properly.
Could you help me with that ? I'm sure it's not that hard but I can't find how to do it.
Change your MainActivity call ass follows
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");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
Intent startNewActivityOpen = new Intent(MainActivity.this, CarteActivity.class);
startNewActivityOpen.putExtra("QRContents", contents);
startActivity(startNewActivityOpen);
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
}
Then do changes on CarteActivity as follows
import android.content.Intent;
import android.widget.ImageView;
public class CarteActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carte);
Intent bgIntent = getIntent();
String contents = bgIntent.getStringExtra("QRContents");
ImageView img = (ImageView)findViewById(R.id.imageView1);
if(contents.equals("carte1")){
img.setImageResource(R.drawable.carte1);
}
if(contents.equals("carte2")){
img.setImageResource(R.drawable.carte2);
}
if(contents.equals("carte3")){
img.setImageResource(R.drawable.carte3);
}
}
#Override
public void onBackPressed() {
Intent startNewActivityOpen = new Intent(CarteActivity.this, MainActivity.class);
startActivity(startNewActivityOpen);
}
}

Trying to read barcodes with Zxing, but it seems the onActivityResult is not beeing called

as the title says, I'm trying to scan 1D barcodes, so far I have thet following code:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test(View view){
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "1D_CODE_MODE");
startActivityForResult(intent, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
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();
TextView uno = (TextView) findViewById(R.id.textView1);
uno.setText(contents);
Toast.makeText(this, "Numero: " + contents, Toast.LENGTH_LONG).show();
Log.d("SEARCH_EAN", "OK, EAN: " + contents + ", FORMAT: " + format);
} else {
Log.e("SEARCH_EAN", "IntentResult je NULL!");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e("SEARCH_EAN", "CANCEL");
}
}
}
}
And of course, I have both IntentResult and IntentIntegrator added to the project.
So, the scanner is beeing called correctly when a button is pressed and it seems to scan the code perfectly (it says "Text found" after it scans it), but it seems that the onActivityResult is not called, since the TextView is not beeing updated and the Toast is not appearing.
Any idea on what the mistake could be?
Thanks in advance!
Your first mistake is not using IntentIntegrator.initiateScan(), replacing it with your own hand-rolled call to startActivityForResult().
Your second mistake is in assuming that IntentIntegrator.REQUEST_CODE is 0. It is not.
Hence, with your current code, you are sending out a request with request code of 0, which is coming back to onActivityResult() with request code of 0, which you are ignoring, because you are only looking for IntentIntegrator.REQUEST_CODE.
Simply replace the body of your test() method with a call to initiateScan(), and you should be in better shape. Here is a sample project that demonstrates the use of IntentIntegrator.
I resolve your same problem so.
public class MainActivity extends Activity {
private TextView tvStatus, tvResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.tvStatus = (TextView) findViewById(R.id.tvStatus);
this.tvResult = (TextView) findViewById(R.id.tvResult);
Button scanBtn = (Button) findViewById(R.id.btnScan);
scanBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_FORMATS", "QR_CODE_MODE");
startActivityForResult(intent,
IntentIntegrator.REQUEST_CODE);
} catch (Exception e) {
Log.e("BARCODE_ERROR", e.getMessage());
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanResult != null) {
this.tvStatus.setText(scanResult.getContents());
this.tvResult.setText(scanResult.getFormatName());
}
}
}
The onActivityResault function must be overridden. just add an #Override before the function declaration and it will be solved.

zxing barcode - how to embed the scanner(intent) with other layouts

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.

How do I start thread only after first activity is finished?

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;
}
}
}

ZXing Library not providing Intent result

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

Categories

Resources