Zxing CaptureActivity.handleDecode()
Zxing CaptureActivity.handleDecode()
我是Zxing新手,点击按钮想扫描二维码图片,这是我的MainActivity.java
private Button scan;
scan = (Button) findViewById(R.id.btn_scan);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(intent, SCAN_CODE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case SCAN_CODE:
Intent myIntent=getIntent();
Bundle bundle=myIntent.getExtras();
QR=bundle.getString("QR");
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
它会调用 CaptureActivity.handleDecode(),这是 CaptureActivity.java
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
//I want to get the text in the image.
String result = rawResult.getText();
Intent intent = new Intent();
intent.putExtra("QR", result);
if(result!=null && !"".equals(result))
setResult(RESULT_OK, intent);
else{
setResult(RESULT_CANCELED, intent);
}
finish();
}
但是有例外
enter image description here
我不知道为什么?
您应该使用 data.getStringExtra("QR")
,而不是
Intent myIntent = getIntent();
Bundle bundle = myIntent.getExtras();
QR = bundle.getString("QR");
其中 data
是 onActivityResult()
的参数。
并使用 !result.isEmpty()
而不是 !"".equals(result)
。
它更具可读性。
希望对您有所帮助!
我是Zxing新手,点击按钮想扫描二维码图片,这是我的MainActivity.java
private Button scan;
scan = (Button) findViewById(R.id.btn_scan);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(intent, SCAN_CODE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case SCAN_CODE:
Intent myIntent=getIntent();
Bundle bundle=myIntent.getExtras();
QR=bundle.getString("QR");
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
它会调用 CaptureActivity.handleDecode(),这是 CaptureActivity.java
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
//I want to get the text in the image.
String result = rawResult.getText();
Intent intent = new Intent();
intent.putExtra("QR", result);
if(result!=null && !"".equals(result))
setResult(RESULT_OK, intent);
else{
setResult(RESULT_CANCELED, intent);
}
finish();
}
但是有例外 enter image description here
我不知道为什么?
您应该使用 data.getStringExtra("QR")
,而不是
Intent myIntent = getIntent();
Bundle bundle = myIntent.getExtras();
QR = bundle.getString("QR");
其中 data
是 onActivityResult()
的参数。
并使用 !result.isEmpty()
而不是 !"".equals(result)
。
它更具可读性。
希望对您有所帮助!