// 使用Zxing的示例代码 compile 'com.google.zxing:core:3.3.2'
// 在build.gradle中添加调用的类 compile 'me.dm7.barcodescanner:zxing:1.9.8'
// In the layout XML file
// In the activity or fragment public class MyActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler { private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerView = new ZXingScannerView(this); // Initiate the scanner library object
setContentView(mScannerView); // Set the scanner view as the content view
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
@Override
public void handleResult(Result result) {
// Do something with the result here
Toast.makeText(this, result.getText(), Toast.LENGTH_LONG).show();
// Resume scanning
mScannerView.resumeCameraPreview(this);
}
}