android二维码扫描 Zxing 3.X 使用与开启闪关灯
在Android studio中我们使用新版的二维码扫描要方便了很多只需要以下几个步骤:
1. 在app的build.gradle中添加
compile 'com.journeyapps:zxing-android-embedded:3.4.0'
2. 在需要调用的二维码界面的地方添加以下代码就可以调用扫描界面了。
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
//integrator.setCameraId(0); // Use a specific camera of the device
integrator.setBeepEnabled(true);
integrator.setOrientationLocked(false);
integrator.setBarcodeImageEnabled(true);
integrator.setPrompt("");
integrator.setCaptureActivity(ScanActivity.class);
integrator.initiateScan();
- integrator.setCaptureActivity(ScanActivity.class);为设置自定义界面。
- integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);为设置支持的格式,比如二维码一维码的格式等,这里设置的是全格式支持。
public class ScanActivity extends Activity {
private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
private boolean flag = true;
private String photo_path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
barcodeScannerView = (DecoratedBarcodeView) findViewById(R.id.viewfinder_view);
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
setClick();
}
@Override
protected void onResume() {
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {