public class MainActivity extends Activity {
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("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public boolean onTouchEvent(final MotionEvent event) {
IntentIntegrator integrator = new IntentIntegrator();
integrator.initiateScan(null);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
TextView tv = (TextView) findViewById(R.id.scanResult);
tv.setText(data.getStringExtra("SCAN_RESULT"));//this is the result
} else
if (resultCode == RESULT_CANCELED) {
// Handle cancel
} }
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}
答案 0 :(得分:1)
当我完成扫描仪活动时遇到了同样的问题,我找到的解决方案是让我的mainActivity扩展Zxing CaptureActivity,这样我就覆盖了handleDecode,我避免在不同的活动之间切换(你必须这样做)获取扫描仪结果。)
无论如何,在先前扫描后重新启动扫描过程我调用了方法
restartPreviewAfterDelay(0L)
(这是一种CaptureActivity的方法)在按钮的onClick函数中。
看看那种方法,我认为这就是你需要的。
答案 1 :(得分:0)
我终于找到了解决方案,我只想在onActivityResult()声明中添加此代码
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);
因此,扫描完成后,应用程序已准备好再次扫描,而不是返回主页活动