diff --git a/README.md b/README.md index 9360776..16fe42d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ZXingCode -## 快速集成二维码扫描,使用最新版本的zxing代码提取(2017.11.10--ZXing 3.3.1) +## 快速集成二维码扫描,生成二维码,可配置相册,闪光灯,自定义扫描线颜色(ZXing 3.3.3) [![](https://jitpack.io/v/maning0303/MNZXingCode.svg)](https://jitpack.io/#maning0303/MNZXingCode) ## 功能: @@ -13,8 +13,6 @@ ![image](https://github.com/maning0303/ZXingCodeDemo/blob/master/screenshots/mn_zxing_screenshot_001.png) ![image](https://github.com/maning0303/ZXingCodeDemo/blob/master/screenshots/mn_zxing_screenshot_002.jpg) ![image](https://github.com/maning0303/ZXingCodeDemo/blob/master/screenshots/mn_zxing_screenshot_003.jpg) -## 最新版本zxing(2017.11.10--ZXing 3.3.1): -![image](https://github.com/maning0303/ZXingCodeDemo/blob/master/screenshots/mn_zxing_screenshot_004.png) ## 如何添加 ### Gradle添加: @@ -32,57 +30,71 @@ #### 2.在Module目录下的build.gradle中添加依赖 ``` gradle dependencies { - compile 'com.github.maning0303:MNZXingCode:V1.1.3' + compile 'com.github.maning0303:MNZXingCode:V1.1.5' } ``` ### 源码添加: -#### 直接拷贝整个module:libraryzxing 和 libraryzxingcore,然后关联到你的项目就可以使用 +#### 直接拷贝整个module:libraryzxing,然后关联到你的项目就可以使用 ## 使用方法: ### 进入需要提前申请相机权限;进入需要提前申请相机权限;进入需要提前申请相机权限; ``` java - - 1.跳转: - Intent intent = new Intent(this, CaptureActivity.class); - //是否显示相册按钮 - intent.putExtra(CaptureActivity.INTENT_KEY_PHOTO_FLAG, true); - //识别声音 - intent.putExtra(CaptureActivity.INTENT_KEY_BEEP_FLAG, true); - //识别震动 - intent.putExtra(CaptureActivity.INTENT_KEY_VIBRATE_FLAG, true); - //扫码框的颜色 - intent.putExtra(CaptureActivity.INTENT_KEY_SCSNCOLOR, "#FFFF00"); - //扫码框上面的提示文案 - intent.putExtra(CaptureActivity.INTENT_KEY_HINTTEXT, "请将二维码放入框中...."); - startActivityForResult(intent, 1000); - - 2.获取结果: - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == 1000) { - if (data == null) { - return; + 1:开始扫描: + //默认扫描 + MNScanManager.startScan(this, new MNScanCallback() { + @Override + public void onActivityResult(int resultCode, Intent data) { + //TODO: + switch (resultCode) { + case MNScanManager.RESULT_SUCCESS: + String resultSuccess = data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_SUCCESS); + break; + case MNScanManager.RESULT_FAIL: + String resultError = data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_ERROR); + break; + case MNScanManager.RESULT_CANCLE: + showToast("取消扫码"); + break; } + } + }); + + + //自定义扫描 + MNScanConfig scanConfig = new MNScanConfig.Builder() + //设置完成震动 + .isShowVibrate(false) + //扫描完成声音 + .isShowBeep(true) + //显示相册功能 + .isShowPhotoAlbum(true) + //打开扫描页面的动画 + .setActivityOpenAnime(R.anim.activity_anmie_in) + //自定义文案 + .setScanHintText("我是自定义文字") + //扫描线的颜色 + .setScanColor("#FFFF00") + .builder(); + MNScanManager.startScan(this, scanConfig, new MNScanCallback() { + @Override + public void onActivityResult(int resultCode, Intent data) { + //TODO: switch (resultCode) { - case CaptureActivity.RESULT_SUCCESS: - String resultSuccess = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_SUCCESS); - showToast(resultSuccess); - textView.setText(resultSuccess); + case MNScanManager.RESULT_SUCCESS: + String resultSuccess = data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_SUCCESS); break; - case CaptureActivity.RESULT_FAIL: - String resultError = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_ERROR); - showToast(resultError); + case MNScanManager.RESULT_FAIL: + String resultError = data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_ERROR); break; - case CaptureActivity.RESULT_CANCLE: + case MNScanManager.RESULT_CANCLE: showToast("取消扫码"); break; } } - } + }); 3:生成二维码: Bitmap qrImage = ZXingUtils.createQRImage("xxxxxx"); diff --git a/app/src/main/java/com/maning/zxingcodedemo/MainActivity.java b/app/src/main/java/com/maning/zxingcodedemo/MainActivity.java index 5deca33..c3bc74e 100644 --- a/app/src/main/java/com/maning/zxingcodedemo/MainActivity.java +++ b/app/src/main/java/com/maning/zxingcodedemo/MainActivity.java @@ -106,16 +106,16 @@ private void handlerResult(int resultCode, Intent data) { return; } switch (resultCode) { - case CaptureActivity.RESULT_SUCCESS: - String resultSuccess = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_SUCCESS); + case MNScanManager.RESULT_SUCCESS: + String resultSuccess = data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_SUCCESS); showToast(resultSuccess); textView.setText(resultSuccess); break; - case CaptureActivity.RESULT_FAIL: - String resultError = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_ERROR); + case MNScanManager.RESULT_FAIL: + String resultError = data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_ERROR); showToast(resultError); break; - case CaptureActivity.RESULT_CANCLE: + case MNScanManager.RESULT_CANCLE: showToast("取消扫码"); break; } diff --git a/libraryzxing/build.gradle b/libraryzxing/build.gradle index bf499c0..1e57223 100644 --- a/libraryzxing/build.gradle +++ b/libraryzxing/build.gradle @@ -21,5 +21,5 @@ android { dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') - compile 'com.google.zxing:core:3.3.1' + compile 'com.google.zxing:core:3.3.3' } diff --git a/libraryzxing/src/main/java/com/google/zxing/client/android/CaptureActivity.java b/libraryzxing/src/main/java/com/google/zxing/client/android/CaptureActivity.java index 98a06d5..0513922 100755 --- a/libraryzxing/src/main/java/com/google/zxing/client/android/CaptureActivity.java +++ b/libraryzxing/src/main/java/com/google/zxing/client/android/CaptureActivity.java @@ -69,17 +69,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Callback { private static final String TAG = CaptureActivity.class.getSimpleName(); - //常量 - public static final int RESULT_SUCCESS = 0; - public static final int RESULT_FAIL = 1; - public static final int RESULT_CANCLE = 2; - public static final String INTENT_KEY_RESULT_SUCCESS = "intent_key_result_success"; - public static final String INTENT_KEY_RESULT_ERROR = "intent_key_result_error"; - public static final String INTENT_KEY_HINTTEXT = "intent_key_hinttext"; - public static final String INTENT_KEY_SCSNCOLOR = "intent_key_scsncolor"; - public static final String INTENT_KEY_PHOTO_FLAG = "intent_key_photo_flag"; - public static final String INTENT_KEY_BEEP_FLAG = "intent_key_beep_flag"; - public static final String INTENT_KEY_VIBRATE_FLAG = "intent_key_vibrate_flag"; private CameraManager cameraManager; private CaptureActivityHandler handler; @@ -181,11 +170,11 @@ public void onClick(View v) { private void initIntent() { Intent intent = getIntent(); - String hintText = intent.getStringExtra(INTENT_KEY_HINTTEXT); - String scanColor = intent.getStringExtra(INTENT_KEY_SCSNCOLOR); - boolean photoFlag = intent.getBooleanExtra(INTENT_KEY_PHOTO_FLAG, true); - beepFlag = intent.getBooleanExtra(INTENT_KEY_BEEP_FLAG, true); - vibrateFlag = intent.getBooleanExtra(INTENT_KEY_VIBRATE_FLAG, true); + String hintText = intent.getStringExtra(MNScanManager.INTENT_KEY_HINTTEXT); + String scanColor = intent.getStringExtra(MNScanManager.INTENT_KEY_SCSNCOLOR); + boolean photoFlag = intent.getBooleanExtra(MNScanManager.INTENT_KEY_PHOTO_FLAG, true); + beepFlag = intent.getBooleanExtra(MNScanManager.INTENT_KEY_BEEP_FLAG, true); + vibrateFlag = intent.getBooleanExtra(MNScanManager.INTENT_KEY_VIBRATE_FLAG, true); if (!TextUtils.isEmpty(hintText)) { viewfinderView.setHintText(hintText); } @@ -336,15 +325,15 @@ public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) { private void finishFailed(String errorMsg) { Intent intent = new Intent(); - intent.putExtra(INTENT_KEY_RESULT_ERROR, errorMsg); - this.setResult(RESULT_FAIL, intent); + intent.putExtra(MNScanManager.INTENT_KEY_RESULT_ERROR, errorMsg); + this.setResult(MNScanManager.RESULT_FAIL, intent); this.finish(); //关闭窗体动画显示 this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out, 0); } private void finishCancle() { - this.setResult(RESULT_CANCLE, null); + this.setResult(MNScanManager.RESULT_CANCLE, null); this.finish(); //关闭窗体动画显示 this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out, 0); @@ -352,8 +341,8 @@ private void finishCancle() { private void finishSuccess(String result) { Intent intent = new Intent(); - intent.putExtra(INTENT_KEY_RESULT_SUCCESS, result); - this.setResult(RESULT_SUCCESS, intent); + intent.putExtra(MNScanManager.INTENT_KEY_RESULT_SUCCESS, result); + this.setResult(MNScanManager.RESULT_SUCCESS, intent); this.finish(); //关闭窗体动画显示 this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out, 0); diff --git a/libraryzxing/src/main/java/com/google/zxing/client/android/MNScanManager.java b/libraryzxing/src/main/java/com/google/zxing/client/android/MNScanManager.java index 7c28716..711c659 100644 --- a/libraryzxing/src/main/java/com/google/zxing/client/android/MNScanManager.java +++ b/libraryzxing/src/main/java/com/google/zxing/client/android/MNScanManager.java @@ -14,6 +14,21 @@ public class MNScanManager { + //常量 + public static final int RESULT_SUCCESS = 0; + public static final int RESULT_FAIL = 1; + public static final int RESULT_CANCLE = 2; + public static final String INTENT_KEY_RESULT_SUCCESS = "intent_key_result_success"; + public static final String INTENT_KEY_RESULT_ERROR = "intent_key_result_error"; + + + //跳转传入的数据 + public static final String INTENT_KEY_HINTTEXT = "intent_key_hinttext"; + public static final String INTENT_KEY_SCSNCOLOR = "intent_key_scsncolor"; + public static final String INTENT_KEY_PHOTO_FLAG = "intent_key_photo_flag"; + public static final String INTENT_KEY_BEEP_FLAG = "intent_key_beep_flag"; + public static final String INTENT_KEY_VIBRATE_FLAG = "intent_key_vibrate_flag"; + public static void startScan(Activity activity, MNScanCallback scanCallback) { Intent intent = new Intent(activity.getApplicationContext(), CaptureActivity.class); @@ -28,15 +43,15 @@ public static void startScan(Activity activity, MNScanConfig mnScanConfig, MNSca } Intent intent = new Intent(activity.getApplicationContext(), CaptureActivity.class); //是否显示相册按钮 - intent.putExtra(CaptureActivity.INTENT_KEY_PHOTO_FLAG, mnScanConfig.isShowPhotoAlbum()); + intent.putExtra(MNScanManager.INTENT_KEY_PHOTO_FLAG, mnScanConfig.isShowPhotoAlbum()); //识别声音 - intent.putExtra(CaptureActivity.INTENT_KEY_BEEP_FLAG, mnScanConfig.isShowBeep()); + intent.putExtra(MNScanManager.INTENT_KEY_BEEP_FLAG, mnScanConfig.isShowBeep()); //识别震动 - intent.putExtra(CaptureActivity.INTENT_KEY_VIBRATE_FLAG, mnScanConfig.isShowVibrate()); + intent.putExtra(MNScanManager.INTENT_KEY_VIBRATE_FLAG, mnScanConfig.isShowVibrate()); //扫码框的颜色 - intent.putExtra(CaptureActivity.INTENT_KEY_SCSNCOLOR, mnScanConfig.getScanColor()); + intent.putExtra(MNScanManager.INTENT_KEY_SCSNCOLOR, mnScanConfig.getScanColor()); //扫码框上面的提示文案 - intent.putExtra(CaptureActivity.INTENT_KEY_HINTTEXT, mnScanConfig.getScanHintText()); + intent.putExtra(MNScanManager.INTENT_KEY_HINTTEXT, mnScanConfig.getScanHintText()); ActResultRequest actResultRequest = new ActResultRequest(activity); actResultRequest.startForResult(intent, scanCallback); diff --git a/screenshots/mn_zxing_screenshot_004.png b/screenshots/mn_zxing_screenshot_004.png deleted file mode 100644 index 9874d98..0000000 Binary files a/screenshots/mn_zxing_screenshot_004.png and /dev/null differ