Android 二维码扫描
有个比较易用的Android 二维码扫描的开源库:
https://github.com/bingoogolapple/BGAQRCode-Android
它也是基于谷歌的zxing开源库的:
https://github.com/zxing/zxing
在grandle中添加依赖库:
compile 'pub.devrel:easypermissions:0.1.7'//用于请求相机权限
compile 'com.google.zxing:core:3.2.1' //谷歌的zxing库
compile 'cn.bingoogolapple:bga-qrcodecore:1.1.7@aar'//bingoogolapple的库
compile 'cn.bingoogolapple:bga-zxing:1.1.7@aar'
1 public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks,QRCodeView.Delegate{ 2 private static final String TAG = "ActionsQRScanActivity"; 3 private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1; 4 private long clickTime = 0; //记录第一次点击的时间 5 6 private QRCodeView mQRCodeView; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 mQRCodeView = (ZXingView) findViewById(R.id.zxingview); 13 mQRCodeView.setDelegate(this); 14 15 } 16 17 @Override 18 protected void onStart() { 19 super.onStart(); 20 requestCodeQRCodePermissions(); 21 mQRCodeView.startCamera(); 22 mQRCodeView.startSpot(); 23 } 24 25 @Override 26 protected void onResume(){ 27 mQRCodeView.showScanRect(); 28 super.onResume(); 29 } 30 31 @Override 32 protected void onStop() { 33 mQRCodeView.stopCamera();
35 super.onStop(); 36 } 37 38 @Override 39 public void onScanQRCodeSuccess(String result) { 40 Log.i(TAG, "result:" + result); 41 42 } 43 44 @Override 45 public void onScanQRCodeOpenCameraError() { 46 Log.e(TAG, "open camera error!"); 47 } 48 49 @Override 50 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 51 EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); 52 } 53 54 @Override 55 public void onPermissionsGranted(int requestCode, List<String> perms) { 56 } 57 58 @Override 59 public void onPermissionsDenied(int requestCode, List<String> perms) { 60 } 61 62 @AfterPermissionGranted(REQUEST_CODE_QRCODE_PERMISSIONS) 63 private void requestCodeQRCodePermissions() { 64 String[] perms = {Manifest.permission.CAMERA}; 65 if (!EasyPermissions.hasPermissions(this, perms)) { 66 EasyPermissions.requestPermissions(this, getResources().getString(R.string.qrcode_permission), REQUEST_CODE_QRCODE_PERMISSIONS, perms); 67 } 68 } 69 70 @Override 71 public boolean onKeyDown(int keyCode, KeyEvent event) { 72 if (keyCode == KeyEvent.KEYCODE_BACK) { 73 exit(); 74 return true; 75 } 76 return super.onKeyDown(keyCode, event); 77 } 78 79 private void exit() { 80 if ((System.currentTimeMillis() - clickTime) > 2000) { 81 Toast.makeText(getApplicationContext(), getResources().getString(R.string.exit_message), 82 Toast.LENGTH_SHORT).show(); 83 clickTime = System.currentTimeMillis(); 84 } else { 85 Log.e(TAG, "exit application"); 86 this.finish(); 87 } 88 } 89 90 91 92 93 }
Activity主要实现的接口如下:
public interface PermissionCallbacks extends ActivityCompat.OnRequestPermissionsResultCallback {//授权成功失败回调 void onPermissionsGranted(int requestCode, List<String> perms); void onPermissionsDenied(int requestCode, List<String> perms); }
public interface Delegate {//扫码成功返回的结果是result,打开相机失败的回调 /** * 处理扫描结果 * * @param result */ void onScanQRCodeSuccess(String result); /** * 处理打开相机出错 */ void onScanQRCodeOpenCameraError(); }
xml文件中的ZxingView
1 <?xml version="1.0" encoding="utf-8"?> 2 <cn.bingoogolapple.qrcode.zxing.ZXingView 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:id="@+id/zxingview" 6 style="@style/MatchWrap" 7 app:qrcv_animTime="1000" 8 app:qrcv_barCodeTipText="将条码放入框内,即可自动扫描" 9 app:qrcv_barcodeRectHeight="140dp" 10 app:qrcv_borderColor="@android:color/white" 11 app:qrcv_borderSize="1dp" 12 app:qrcv_cornerColor="@color/colorPrimaryDark" 13 app:qrcv_cornerLength="20dp" 14 app:qrcv_cornerSize="3dp" 15 app:qrcv_customScanLineDrawable="@drawable/scan_icon_scanline" 16 app:qrcv_isBarcode="false" 17 app:qrcv_isCenterVertical="false" 18 app:qrcv_isOnlyDecodeScanBoxArea="false" 19 app:qrcv_isScanLineReverse="true" 20 app:qrcv_isShowDefaultGridScanLineDrawable="false" 21 app:qrcv_isShowDefaultScanLineDrawable="true" 22 app:qrcv_isShowTipBackground="true" 23 app:qrcv_isShowTipTextAsSingleLine="false" 24 app:qrcv_isTipTextBelowRect="false" 25 app:qrcv_maskColor="#33FFFFFF" 26 app:qrcv_qrCodeTipText="@string/scan_tips" 27 app:qrcv_rectWidth="200dp" 28 app:qrcv_scanLineColor="@color/colorPrimaryDark" 29 app:qrcv_scanLineMargin="0dp" 30 app:qrcv_scanLineSize="0.5dp" 31 app:qrcv_tipTextColor="@android:color/white" 32 app:qrcv_tipTextSize="12sp" 33 app:qrcv_toolbarHeight="56dp" 34 app:qrcv_topOffset="60dp"/>
自定义属性代码的含义在这里可以找到:https://github.com/bingoogolapple/BGAQRCode-Android
感觉还是很简单的