我需要构建一个可识别QR码的应用,而不必强迫用户安装其他应用。将来,我还需要在识别代码之前操作扫描图像(我扫描的代码会反转颜色)。
尝试从这些链接中了解提示和教程:
Integrating the ZXing library directly into my Android application
Embedding ZXing in android app
http://karanbalkar.com/2013/12/tutorial-65-implement-barcode-scanner-using-zxing-in-android/
创建一些基本代码并运行应用程序后,单击“扫描”按钮,出现No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }
我做了什么:
创建新项目
将core-3.2.1.jar复制到libs /
添加意图调用和结果处理
我添加的意图/结果代码:
private Button scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan= (Button)findViewById(R.id.btnScan);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
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
MultiFormatWriter writer = new MultiFormatWriter();
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.i("App","Scan unsuccessful");
}
}
}
如何启动意图?我做错了什么?
答案 0 :(得分:2)
你应该像这样启动扫描:
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
//in case you want to customize a bit.
integrator.setPrompt("Scan a QR/Bar code");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.initiateScan();
}
接收这样的结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String data = scanResult.getContents();
// use this data
} else {
// error
}
break;
}
}
}
编辑1:
将此项添加到您应用的build.gradle
dependencies
:
compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'
答案 1 :(得分:0)
尝试以下链接,对我有用: