安卓扫描框架之BGAQRCode
一、 github索引地址
https://github.com/bingoogolapple/BGAQRCode-Android
二、 本文使用方向
重点是针对扫码进行处理
三、引入依赖
compile'com.google.zxing:core:3.3.0
compile'cn.bingoogolapple:bga-qrcodecore:1.1.9@aar
compile'cn.bingoogolapple:bga-zxing:1.1.9@aar
四、AndroidManifest中配置权限
uses-permission android:name="android.permission.CAMERA"
uses-permission android:name="android.permission.VIBRATE"
uses-permission android:name="android.permission.FLASHLIGHT"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
五、布局xml核心文件
<cn.bingoogolapple.qrcode.zxing.ZXingView
android:id="@+id/zx_scan"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:qrcv_animTime="2000"
app:qrcv_barCodeTipText="将条码放入框内,即可自动扫描"
app:qrcv_borderColor="@android:color/white"
app:qrcv_borderSize="1dp"
app:qrcv_cornerColor="#f57257"
app:qrcv_cornerLength="20dp"
app:qrcv_cornerSize="3dp"
app:qrcv_isBarcode="false"
app:qrcv_isOnlyDecodeScanBoxArea="true"
app:qrcv_isScanLineReverse="true"
app:qrcv_isShowDefaultGridScanLineDrawable="true"
app:qrcv_isShowDefaultScanLineDrawable="false"
app:qrcv_isShowTipBackground="true"
app:qrcv_isShowTipTextAsSingleLine="false"
app:qrcv_isTipTextBelowRect="false"
app:qrcv_maskColor="#33FFFFFF"
app:qrcv_qrCodeTipText="将二维码放入框内,即可自动扫描"
app:qrcv_rectWidth="200dp"
app:qrcv_scanLineColor="#f57257"
app:qrcv_scanLineMargin="0dp"
app:qrcv_scanLineSize="1dp"
app:qrcv_tipTextColor="@android:color/white"
app:qrcv_tipTextSize="12sp"
app:qrcv_toolbarHeight="0dp"
app:qrcv_topOffset="180dp"/>
六、核心使用代码
public class ScanCodeActivity extends Activity implements QRCodeView.Delegate {
private static final int REQUEST_CODE_CAMERA = 999;
private static final String TAG = ScanCodeActivity.class.getSimpleName();
private QRCodeView mQRCodeView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_code);
mQRCodeView = (ZXingView) findViewById(R.id.zx_scan);
mQRCodeView.setDelegate(this);
}
@Override
protected void onStart() {
super.onStart();
mQRCodeView.startCamera();
mQRCodeView.startSpot();
}
@Override
public void onResume() {
super.onResume();
mQRCodeView.showScanRect();
}
@Override
protected void onStop() {
mQRCodeView.stopCamera();
super.onStop();
}
@Override
protected void onDestroy() {
mQRCodeView.onDestroy();
super.onDestroy();
}
//震动器
private void vibrate() {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(100);
}
@Override
public void onScanQRCodeSuccess(String result) {
Log.e(TAG, "result:" + result);
vibrate();//震动
if (!TextUtils.isEmpty(result)) {
mQRCodeView.stopCamera();
mQRCodeView.onDestroy();
Intent intent = new Intent(ScanCodeActivity.this, MessageActivity.class);
intent.putExtra("url", result);
startActivity(intent);
} else {
Toast.makeText(this, "链接无效,请重新扫描", Toast.LENGTH_SHORT).show();
mQRCodeView.startSpot();
}
mQRCodeView.startSpot();
}
@Override
public void onScanQRCodeOpenCameraError() {
Log.e(TAG, "无相机权限,打开相机出错");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_CODE_CAMERA) {
mQRCodeView.startCamera();
mQRCodeView.startSpot();
}
}
}