|
|
@@ -0,0 +1,161 @@
|
|
|
+package com.mgtech.base_library.activityUtils;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Vibrator;
|
|
|
+import android.widget.CompoundButton;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+
|
|
|
+import com.alibaba.android.arouter.facade.annotation.Route;
|
|
|
+import com.mgtech.base_library.R;
|
|
|
+import com.mgtech.base_library.R2;
|
|
|
+import com.mgtech.base_library.base.BaseActivity;
|
|
|
+import com.mgtech.base_library.router.RouterURLS;
|
|
|
+import com.mgtech.base_library.util.ToastUtils;
|
|
|
+
|
|
|
+import butterknife.BindView;
|
|
|
+import butterknife.OnCheckedChanged;
|
|
|
+import cn.bingoogolapple.qrcode.core.QRCodeView;
|
|
|
+import cn.bingoogolapple.qrcode.zxing.ZXingView;
|
|
|
+import pub.devrel.easypermissions.AfterPermissionGranted;
|
|
|
+import pub.devrel.easypermissions.EasyPermissions;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ProjectName: BaseLibrary
|
|
|
+ * @Package: com.tennor.base_library.base
|
|
|
+ * @ClassName: BarCodeScanActivity
|
|
|
+ * @Description: 条形码扫描
|
|
|
+ * @Author: 牛松涛
|
|
|
+ * @CreateDate: 2020/4/8 15:03
|
|
|
+ * @UpdateUser: 更新者:
|
|
|
+ * @UpdateDate: 2020/4/8 15:03
|
|
|
+ * @UpdateRemark: 更新说明:
|
|
|
+ * @Version: 1.0
|
|
|
+ */
|
|
|
+
|
|
|
+@Route(path = RouterURLS.BASE_BAR_CODE_SCAN_ACTIVITY_URL)
|
|
|
+public class BarCodeScanActivity extends BaseActivity implements QRCodeView.Delegate{
|
|
|
+ @BindView(R2.id.zxing_view)
|
|
|
+ ZXingView zxingView;
|
|
|
+
|
|
|
+ private Intent intent;
|
|
|
+
|
|
|
+ public static String BAR_CODE_INFO = "bar_code_info";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected int initLayoutId() {
|
|
|
+ return R.layout.activity_barcode_scan;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void initView() {
|
|
|
+ zxingView.setDelegate(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void initData() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onStart() {
|
|
|
+ super.onStart();
|
|
|
+ methodRequiresCameraPermission();
|
|
|
+// zxingView.startCamera(); // 打开后置摄像头开始预览,但是并未开始识别
|
|
|
+//// mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览,但是并未开始识别
|
|
|
+//
|
|
|
+// zxingView.startSpotAndShowRect(); // 显示扫描框,并开始识别
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onStop() {
|
|
|
+ zxingView.stopCamera(); // 关闭摄像头预览,并且隐藏扫描框
|
|
|
+ super.onStop();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ zxingView.onDestroy(); // 销毁二维码扫描控件
|
|
|
+ super.onDestroy();
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnCheckedChanged({R2.id.flash_btn})
|
|
|
+ public void OnCheckedChanged(CompoundButton button, boolean b){
|
|
|
+ int id = button.getId();
|
|
|
+ if (id == R.id.flash_btn){
|
|
|
+ if (b)
|
|
|
+ zxingView.openFlashlight();
|
|
|
+ else
|
|
|
+ zxingView.closeFlashlight();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void vibrate() {
|
|
|
+ Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
|
|
|
+ if (vibrator == null)
|
|
|
+ return;
|
|
|
+ vibrator.vibrate(200);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //扫码成功
|
|
|
+ @Override
|
|
|
+ public void onScanQRCodeSuccess(String result) {
|
|
|
+ vibrate();
|
|
|
+ if (intent == null)
|
|
|
+ intent = new Intent();
|
|
|
+ intent.putExtra(BAR_CODE_INFO,result);
|
|
|
+ setResult(RESULT_OK,intent);
|
|
|
+ finish();
|
|
|
+// ARouter.getInstance().build(TmaRouterUrls.HOME_ACTIVITY_URL).navigation();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCameraAmbientBrightnessChanged(boolean isDark) {
|
|
|
+ // 这里是通过修改提示文案来展示环境是否过暗的状态,接入方也可以根据 isDark 的值来实现其他交互效果
|
|
|
+ String tipText = zxingView.getScanBoxView().getTipText();
|
|
|
+ String ambientBrightnessTip = getString(R.string.camera_dark);
|
|
|
+ if (isDark) {
|
|
|
+ if (!tipText.contains(ambientBrightnessTip)) {
|
|
|
+ zxingView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (tipText.contains(ambientBrightnessTip)) {
|
|
|
+ tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
|
|
|
+ zxingView.getScanBoxView().setTipText(tipText);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onScanQRCodeOpenCameraError() {
|
|
|
+ ToastUtils.showShort(R.string.camera_open_error);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
+ EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterPermissionGranted(10)
|
|
|
+ private void methodRequiresCameraPermission() {
|
|
|
+ String[] perms = {Manifest.permission.CAMERA};
|
|
|
+ if (EasyPermissions.hasPermissions(this, perms)) {
|
|
|
+ // Already have permission, do the thing
|
|
|
+ zxingView.startCamera(); // 打开后置摄像头开始预览,但是并未开始识别
|
|
|
+// mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览,但是并未开始识别
|
|
|
+
|
|
|
+ zxingView.startSpotAndShowRect(); // 显示扫描框,并开始识别
|
|
|
+ } else {
|
|
|
+ // Do not have permissions, request them now
|
|
|
+ EasyPermissions.requestPermissions(this, getString(R.string.camera_permission),
|
|
|
+ 10, perms);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|