diff --git a/README.md b/README.md index 414f628..31fbb83 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,13 @@ [![CircleCI](https://circleci.com/gh/jenly1314/ZXingLite.svg?style=svg)](https://circleci.com/gh/jenly1314/ZXingLite) [![API](https://img.shields.io/badge/API-16%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=16) [![License](https://img.shields.io/badge/license-Apche%202.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0) -[![Blog](https://img.shields.io/badge/blog-Jenly-9933CC.svg)](https://jenly1314.github.io) +[![Blog](https://img.shields.io/badge/blog-Jenly-9933CC.svg)](https://jenly1314.github.io/) [![QQGroup](https://img.shields.io/badge/QQGroup-20867961-blue.svg)](http://shang.qq.com/wpa/qunwpa?idkey=8fcc6a2f88552ea44b1411582c94fd124f7bb3ec227e2a400dbbfaad3dc2f5ad) ZXingLite for Android 是ZXing的精简版,基于ZXing库优化扫码和生成二维码/条形码功能,扫码界面完全支持自定义,也可一行代码使用默认实现的扫码功能。总之你想要的都在这里。 >简单如斯,你不试试? Come on~ + ## Gif 展示 ![Image](GIF.gif) @@ -37,6 +38,13 @@ ZXingLite for Android 是ZXing的精简版,基于ZXing库优化扫码和生成 | laserStyle | enum | line | 扫描激光的样式 | | gridColumn | integer | 20 | 网格扫描激光列数 | | gridHeight | integer | 40dp | 网格扫描激光高度,为0dp时,表示动态铺满 | +| cornerRectWidth | dimension | 4dp | 扫描区边角的宽 | +| cornerRectHeight | dimension | 16dp | 扫描区边角的高 | +| scannerLineMoveDistance | dimension | 2dp | 扫描线每次移动距离 | +| scannerLineHeight | dimension | 5dp | 扫描线高度 | +| frameLineWidth | dimension | 1dp | 边框线宽度 | +| scannerAnimationDelay | integer | 15 | 扫描动画延迟间隔时间,单位:毫秒 | +| frameRatio | float | 0.625f | 扫码框与屏幕占比 | ## 引入 @@ -46,20 +54,21 @@ ZXingLite for Android 是ZXing的精简版,基于ZXing库优化扫码和生成 com.king.zxing zxing-lite - 1.1.2 + 1.1.3 pom ``` ### Gradle: ```gradle //AndroidX 版本 -implementation 'com.king.zxing:zxing-lite:1.1.2-androidx' +implementation 'com.king.zxing:zxing-lite:1.1.3-androidx' + //Android 版本 -implementation 'com.king.zxing:zxing-lite:1.1.2' +implementation 'com.king.zxing:zxing-lite:1.1.3' ``` ### Lvy: ```lvy - + ``` @@ -137,6 +146,10 @@ api 'com.google.zxing:core:3.3.3' ## 版本记录 +#### v1.1.3:2019-9-24 +* 支持真实识别区域比例和识别区域偏移量可配置 +* 对外暴露更多可配置参数 + #### v1.1.2:2019-6-27 * 优化部分细节,为迁移至AndroidX做准备 * 支持AndroidX对应版本 @@ -204,4 +217,3 @@ api 'com.google.zxing:core:3.3.3' - diff --git a/app/release/app-release.apk b/app/release/app-release.apk index ec0a2e2..fb016aa 100644 Binary files a/app/release/app-release.apk and b/app/release/app-release.apk differ diff --git a/app/release/output.json b/app/release/output.json index c23f15f..f696025 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":12,"versionName":"1.1.2-androidx","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":14,"versionName":"1.1.3-androidx","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file diff --git a/app/src/main/java/com/king/zxing/app/CustomCaptureActivity.java b/app/src/main/java/com/king/zxing/app/CustomCaptureActivity.java index 14ea674..bd84d5d 100644 --- a/app/src/main/java/com/king/zxing/app/CustomCaptureActivity.java +++ b/app/src/main/java/com/king/zxing/app/CustomCaptureActivity.java @@ -62,8 +62,11 @@ public class CustomCaptureActivity extends CaptureActivity { //获取CaptureHelper,里面有扫码相关的配置设置 getCaptureHelper().playBeep(false)//播放音效 .vibrate(true)//震动 -// .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度 .supportVerticalCode(true)//支持扫垂直条码,建议有此需求时才使用。 +// .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度 +// .framingRectRatio(0.9f)//设置识别区域比例,范围建议在0.625 ~ 1.0之间。非全屏识别时才有效 +// .framingRectVerticalOffset(0)//设置识别区域垂直方向偏移量,非全屏识别时才有效 +// .framingRectHorizontalOffset(0)//设置识别区域水平方向偏移量,非全屏识别时才有效 .continuousScan(isContinuousScan);//是否连扫 } @@ -87,7 +90,6 @@ public class CustomCaptureActivity extends CaptureActivity { return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); } - /** * 扫码结果回调 * @param result 扫码结果 @@ -119,4 +121,4 @@ public class CustomCaptureActivity extends CaptureActivity { break; } } -} +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6658478..a557909 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -13,7 +13,8 @@ android:layout_height="wrap_content" app:contentInsetLeft="0dp" app:contentInsetStart="0dp" - tools:ignore="MissingConstraints"> + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintTop_toTopOf="parent"> { + inactivityTimer.onActivity(); + beepManager.playBeepSoundAndVibrate(); + onResult(result); }; //设置是否播放音效和震动 beepManager.setPlayBeep(isPlayBeep); @@ -310,6 +321,7 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture * @param event * @param camera */ + @Deprecated private void focusOnTouch(MotionEvent event,Camera camera) { Camera.Parameters params = camera.getParameters(); @@ -333,13 +345,10 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO); camera.setParameters(params); - camera.autoFocus(new Camera.AutoFocusCallback() { - @Override - public void onAutoFocus(boolean success, Camera camera) { - Camera.Parameters params = camera.getParameters(); - params.setFocusMode(currentFocusMode); - camera.setParameters(params); - } + camera.autoFocus((success, camera1) -> { + Camera.Parameters params1 = camera1.getParameters(); + params1.setFocusMode(currentFocusMode); + camera1.setParameters(params1); }); } @@ -422,18 +431,15 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture } if(isPlayBeep){//如果播放音效,则稍微延迟一点,给予播放音效时间 - captureHandler.postDelayed(new Runnable() { - @Override - public void run() { - //如果设置了回调,并且onCallback返回为true,则表示拦截 - if(onCaptureCallback!=null && onCaptureCallback.onResultCallback(text)){ - return; - } - Intent intent = new Intent(); - intent.putExtra(Intents.Scan.RESULT,text); - activity.setResult(Activity.RESULT_OK,intent); - activity.finish(); + captureHandler.postDelayed(() -> { + //如果设置了回调,并且onCallback返回为true,则表示拦截 + if(onCaptureCallback!=null && onCaptureCallback.onResultCallback(text)){ + return; } + Intent intent = new Intent(); + intent.putExtra(Intents.Scan.RESULT,text); + activity.setResult(Activity.RESULT_OK,intent); + activity.finish(); },100); return; } @@ -600,6 +606,46 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture return this; } + /** + * 设置识别区域比例,范围建议在0.625 ~ 1.0之间。非全屏识别时才有效 + * 0.625 即与默认推荐显示区域一致,1.0表示与宽度一致 + * @param framingRectRatio 默认0.9 + * @return + */ + public CaptureHelper framingRectRatio(@FloatRange(from = 0.0f ,to = 1.0f) float framingRectRatio) { + this.framingRectRatio = framingRectRatio; + if(cameraManager!=null){ + cameraManager.setFramingRectRatio(framingRectRatio); + } + return this; + } + + /** + * 设置识别区域垂直方向偏移量,非全屏识别时才有效 + * @param framingRectVerticalOffset 默认0,表示不偏移 + * @return + */ + public CaptureHelper framingRectVerticalOffset(int framingRectVerticalOffset) { + this.framingRectVerticalOffset = framingRectVerticalOffset; + if(cameraManager!=null){ + cameraManager.setFramingRectVerticalOffset(framingRectVerticalOffset); + } + return this; + } + + /** + * 设置识别区域水平方向偏移量,非全屏识别时才有效 + * @param framingRectHorizontalOffset 默认0,表示不偏移 + * @return + */ + public CaptureHelper framingRectHorizontalOffset(int framingRectHorizontalOffset) { + this.framingRectHorizontalOffset = framingRectHorizontalOffset; + if(cameraManager!=null){ + cameraManager.setFramingRectHorizontalOffset(framingRectHorizontalOffset); + } + return this; + } + /** * 设置扫码回调 @@ -646,4 +692,4 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture public InactivityTimer getInactivityTimer() { return inactivityTimer; } -} +} \ No newline at end of file diff --git a/lib/src/main/java/com/king/zxing/ViewfinderView.java b/lib/src/main/java/com/king/zxing/ViewfinderView.java index a8214a1..483aa38 100644 --- a/lib/src/main/java/com/king/zxing/ViewfinderView.java +++ b/lib/src/main/java/com/king/zxing/ViewfinderView.java @@ -51,52 +51,141 @@ import androidx.core.content.ContextCompat; */ public final class ViewfinderView extends View { - private static final long ANIMATION_DELAY = 15L; private static final int CURRENT_POINT_OPACITY = 0xA0; private static final int MAX_RESULT_POINTS = 20; private static final int POINT_SIZE = 20; - private static final int CORNER_RECT_WIDTH = 8; //扫描区边角的宽 - private static final int CORNER_RECT_HEIGHT = 40; //扫描区边角的高 - private static final int SCANNER_LINE_MOVE_DISTANCE = 6; //扫描线移动距离 - private static final int SCANNER_LINE_HEIGHT = 10; //扫描线宽度 - + /** + * 画笔 + */ private Paint paint; + + /** + * 文本画笔 + */ private TextPaint textPaint; + /** + * 扫码框外面遮罩颜色 + */ private int maskColor; - //扫描区域边框颜色 + /** + * 扫描区域边框颜色 + */ private int frameColor; - //扫描线颜色 + /** + * 扫描线颜色 + */ private int laserColor; - //四角颜色 + /** + * 扫码框四角颜色 + */ private int cornerColor; + /** + * 结果点颜色 + */ private int resultPointColor; + /** + * 提示文本与扫码框的边距 + */ private float labelTextPadding; + /** + * 提示文本的位置 + */ private TextLocation labelTextLocation; - //扫描区域提示文本 + /** + * 扫描区域提示文本 + */ private String labelText; - //扫描区域提示文本颜色 + /** + * 扫描区域提示文本颜色 + */ private int labelTextColor; + /** + * 提示文本字体大小 + */ private float labelTextSize; + + /** + * 扫描线开始位置 + */ public int scannerStart = 0; + /** + * 扫描线结束位置 + */ public int scannerEnd = 0; + /** + * 是否显示结果点 + */ private boolean isShowResultPoint; + /** + * 屏幕宽 + */ private int screenWidth; + /** + * 屏幕高 + */ private int screenHeight; - //扫码框宽 + /** + * 扫码框宽 + */ private int frameWidth; - //扫码框宽 + /** + * 扫码框高 + */ private int frameHeight; - //扫描激光线风格 + /** + * 扫描激光线风格 + */ private LaserStyle laserStyle; + /** + * 网格列数 + */ private int gridColumn; + /** + * 网格高度 + */ private int gridHeight; + /** + * 扫码框 + */ private Rect frame; + /** + * 扫描区边角的宽 + */ + private int cornerRectWidth; + /** + * 扫描区边角的高 + */ + private int cornerRectHeight; + /** + * 扫描线每次移动距离 + */ + private int scannerLineMoveDistance; + /** + * 扫描线高度 + */ + private int scannerLineHeight; + + /** + * 边框线宽度 + */ + private int frameLineWidth; + + /** + * 扫描动画延迟间隔时间 默认15毫秒 + */ + private int scannerAnimationDelay; + + /** + * 扫码框占比 + */ + private float frameRatio; + private List possibleResultPoints; private List lastPossibleResultPoints; @@ -181,6 +270,13 @@ public final class ViewfinderView extends View { gridColumn = array.getInt(R.styleable.ViewfinderView_gridColumn,20); gridHeight = (int)array.getDimension(R.styleable.ViewfinderView_gridHeight,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,40,getResources().getDisplayMetrics())); + cornerRectWidth = (int)array.getDimension(R.styleable.ViewfinderView_cornerRectWidth,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4,getResources().getDisplayMetrics())); + cornerRectHeight = (int)array.getDimension(R.styleable.ViewfinderView_cornerRectHeight,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,16,getResources().getDisplayMetrics())); + scannerLineMoveDistance = (int)array.getDimension(R.styleable.ViewfinderView_scannerLineMoveDistance,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,2,getResources().getDisplayMetrics())); + scannerLineHeight = (int)array.getDimension(R.styleable.ViewfinderView_scannerLineHeight,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,5,getResources().getDisplayMetrics())); + frameLineWidth = (int)array.getDimension(R.styleable.ViewfinderView_frameLineWidth,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,1,getResources().getDisplayMetrics())); + scannerAnimationDelay = array.getInteger(R.styleable.ViewfinderView_scannerAnimationDelay,15); + frameRatio = array.getFloat(R.styleable.ViewfinderView_frameRatio,0.625f); array.recycle(); paint = new Paint(Paint.ANTI_ALIAS_FLAG); @@ -192,7 +288,7 @@ public final class ViewfinderView extends View { screenWidth = getDisplayMetrics().widthPixels; screenHeight = getDisplayMetrics().heightPixels; - int size = (int)(Math.min(screenWidth,screenHeight) * 0.625f); + int size = (int)(Math.min(screenWidth,screenHeight) * frameRatio); if(frameWidth<=0 || frameWidth > screenWidth){ frameWidth = size; @@ -233,7 +329,6 @@ public final class ViewfinderView extends View { frame = new Rect(leftOffset, topOffset, leftOffset + frameWidth, topOffset + frameHeight); } - @SuppressLint("DrawAllocation") @Override public void onDraw(Canvas canvas) { @@ -243,7 +338,7 @@ public final class ViewfinderView extends View { if(scannerStart == 0 || scannerEnd == 0) { scannerStart = frame.top; - scannerEnd = frame.bottom - SCANNER_LINE_HEIGHT; + scannerEnd = frame.bottom - scannerLineHeight; } int width = canvas.getWidth(); @@ -263,7 +358,7 @@ public final class ViewfinderView extends View { drawResultPoint(canvas,frame); // Request another update at the animation interval, but only repaint the laser line, // not the entire viewfinder mask. - postInvalidateDelayed(ANIMATION_DELAY, + postInvalidateDelayed(scannerAnimationDelay, frame.left - POINT_SIZE, frame.top - POINT_SIZE, frame.right + POINT_SIZE, @@ -300,17 +395,17 @@ public final class ViewfinderView extends View { private void drawCorner(Canvas canvas, Rect frame) { paint.setColor(cornerColor); //左上 - canvas.drawRect(frame.left, frame.top, frame.left + CORNER_RECT_WIDTH, frame.top + CORNER_RECT_HEIGHT, paint); - canvas.drawRect(frame.left, frame.top, frame.left + CORNER_RECT_HEIGHT, frame.top + CORNER_RECT_WIDTH, paint); + canvas.drawRect(frame.left, frame.top, frame.left + cornerRectWidth, frame.top + cornerRectHeight, paint); + canvas.drawRect(frame.left, frame.top, frame.left + cornerRectHeight, frame.top + cornerRectWidth, paint); //右上 - canvas.drawRect(frame.right - CORNER_RECT_WIDTH, frame.top, frame.right, frame.top + CORNER_RECT_HEIGHT, paint); - canvas.drawRect(frame.right - CORNER_RECT_HEIGHT, frame.top, frame.right, frame.top + CORNER_RECT_WIDTH, paint); + canvas.drawRect(frame.right - cornerRectWidth, frame.top, frame.right, frame.top + cornerRectHeight, paint); + canvas.drawRect(frame.right - cornerRectHeight, frame.top, frame.right, frame.top + cornerRectWidth, paint); //左下 - canvas.drawRect(frame.left, frame.bottom - CORNER_RECT_WIDTH, frame.left + CORNER_RECT_HEIGHT, frame.bottom, paint); - canvas.drawRect(frame.left, frame.bottom - CORNER_RECT_HEIGHT, frame.left + CORNER_RECT_WIDTH, frame.bottom, paint); + canvas.drawRect(frame.left, frame.bottom - cornerRectWidth, frame.left + cornerRectHeight, frame.bottom, paint); + canvas.drawRect(frame.left, frame.bottom - cornerRectHeight, frame.left + cornerRectWidth, frame.bottom, paint); //右下 - canvas.drawRect(frame.right - CORNER_RECT_WIDTH, frame.bottom - CORNER_RECT_HEIGHT, frame.right, frame.bottom, paint); - canvas.drawRect(frame.right - CORNER_RECT_HEIGHT, frame.bottom - CORNER_RECT_WIDTH, frame.right, frame.bottom, paint); + canvas.drawRect(frame.right - cornerRectWidth, frame.bottom - cornerRectHeight, frame.right, frame.bottom, paint); + canvas.drawRect(frame.right - cornerRectHeight, frame.bottom - cornerRectWidth, frame.right, frame.bottom, paint); } /** @@ -342,7 +437,7 @@ public final class ViewfinderView extends View { //线性渐变 LinearGradient linearGradient = new LinearGradient( frame.left, scannerStart, - frame.left, scannerStart + SCANNER_LINE_HEIGHT, + frame.left, scannerStart + scannerLineHeight, shadeColor(laserColor), laserColor, Shader.TileMode.MIRROR); @@ -350,9 +445,9 @@ public final class ViewfinderView extends View { paint.setShader(linearGradient); if(scannerStart <= scannerEnd) { //椭圆 - RectF rectF = new RectF(frame.left + 2 * SCANNER_LINE_HEIGHT, scannerStart, frame.right - 2 * SCANNER_LINE_HEIGHT, scannerStart + SCANNER_LINE_HEIGHT); + RectF rectF = new RectF(frame.left + 2 * scannerLineHeight, scannerStart, frame.right - 2 * scannerLineHeight, scannerStart + scannerLineHeight); canvas.drawOval(rectF, paint); - scannerStart += SCANNER_LINE_MOVE_DISTANCE; + scannerStart += scannerLineMoveDistance; } else { scannerStart = frame.top; } @@ -388,7 +483,7 @@ public final class ViewfinderView extends View { } if(scannerStart + + + + + + + + \ No newline at end of file diff --git a/versions.gradle b/versions.gradle index ad01cd3..01618b7 100644 --- a/versions.gradle +++ b/versions.gradle @@ -1,7 +1,7 @@ //App def app_version = [:] -app_version.versionCode = 12 -app_version.versionName = "1.1.2-androidx" +app_version.versionCode = 14 +app_version.versionName = "1.1.3-androidx" ext.app_version = app_version //build version