我正在开发一个按钮点击时调用ZXing
扫描程序的应用程序。这工作正常,扫描代码后会弹出一个新的活动,该活动应该获得扫描的代码,所以我可以编辑它,我想从该产品中获得多少等等。但我不知道如何扫描码。以下是我到目前为止的情况:
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.app.AlertDialog;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
View scanButton = findViewById(R.id.scan_button);
scanButton.setOnClickListener(this);
View editButton = findViewById(R.id.about_button);
editButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.scan_button:
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);
break;
case R.id.about_button:
Intent about = new Intent(this.getApplicationContext(),About.class);
Bundle b = new Bundle();
b.putString("key","blablabla");
about.putExtras(b);
startActivityForResult(about, 0);
break;
case R.id.exit_button:
finish();
break;
}
}
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
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("contents",contents);
intent.putExtras(b);
startActivityForResult(result, 0);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
被调用的活动名为Result
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.Intent;
public class Result extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
View finishButton =findViewById(R.id.finish_button);
finishButton.setOnClickListener(this);
View nextButton =findViewById(R.id.next_button);
nextButton.setOnClickListener(this);
Bundle b = getIntent().getExtras();
String product = b.getString("contens").toString();
EditText et1 = (EditText) findViewById(R.id.edit_text);
et1.setText(product);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.finish_button:
finish();
break;
case R.id.next_button:
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);
finish();
break;
}
}
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
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("contents",contents);
intent.putExtras(b);
startActivityForResult(result, 0);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
答案 0 :(得分:1)
使用他们的集成JAR。它提供代码来处理您在上面尝试的所有操作。您可以看到IntentIntegrator
here的源代码,这里有a sample project证明其用途。