diff --git a/README.md b/README.md index 945ee14..5dd0f14 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,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 | 扫码框与屏幕占比 | ## 引入 @@ -48,21 +55,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 - + ``` @@ -140,6 +147,10 @@ api 'com.google.zxing:core:3.3.3' ## 版本记录 +#### v1.1.3:2019-9-24 +* 支持真实识别区域比例和识别区域偏移量可配置 +* 对外暴露更多可配置参数 + #### v1.1.2:2019-6-27 * 优化部分细节,为迁移至AndroidX做准备 * 支持AndroidX对应版本 @@ -196,6 +207,8 @@ api 'com.google.zxing:core:3.3.3' CSDN: jenly121 + 博客园: jenly + Github: jenly1314 加入QQ群: 20867961 diff --git a/app/release/app-release.apk b/app/release/app-release.apk index e693aa5..4dcda69 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 e86aa59..a1a4bfe 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":11,"versionName":"1.1.2","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":13,"versionName":"1.1.3","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 4e68d3f..20eedd2 100644 --- a/app/src/main/java/com/king/zxing/app/CustomCaptureActivity.java +++ b/app/src/main/java/com/king/zxing/app/CustomCaptureActivity.java @@ -61,8 +61,11 @@ public void onCreate(Bundle icicle) { //获取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);//是否连扫 } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 52b9a40..070a5c4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -14,7 +14,9 @@ android:background="@color/colorPrimary" android:layout_height="wrap_content" app:contentInsetLeft="0dp" - app:contentInsetStart="0dp"> + app:contentInsetStart="0dp" + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintTop_toTopOf="parent"> { + inactivityTimer.onActivity(); + beepManager.playBeepSoundAndVibrate(); + onResult(result); }; //设置是否播放音效和震动 beepManager.setPlayBeep(isPlayBeep); @@ -309,6 +320,7 @@ private void handleZoom(boolean isZoomIn, Camera camera) { * @param event * @param camera */ + @Deprecated private void focusOnTouch(MotionEvent event,Camera camera) { Camera.Parameters params = camera.getParameters(); @@ -332,13 +344,10 @@ private void focusOnTouch(MotionEvent event,Camera camera) { 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); }); } @@ -421,18 +430,15 @@ public void onResult(Result result){ } 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; } @@ -599,6 +605,46 @@ public CaptureHelper fullScreenScan(boolean fullScreenScan) { 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; + } + /** * 设置扫码回调 diff --git a/lib/src/main/java/com/king/zxing/ViewfinderView.java b/lib/src/main/java/com/king/zxing/ViewfinderView.java index c8693bf..9fe8a79 100644 --- a/lib/src/main/java/com/king/zxing/ViewfinderView.java +++ b/lib/src/main/java/com/king/zxing/ViewfinderView.java @@ -17,17 +17,11 @@ */ -import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; -import android.graphics.Bitmap; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.LinearGradient; -import android.graphics.Matrix; import android.graphics.Paint; -import android.graphics.Path; -import android.graphics.PointF; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Shader; @@ -41,7 +35,6 @@ import android.text.TextUtils; import android.util.AttributeSet; import android.util.DisplayMetrics; -import android.util.Log; import android.util.TypedValue; import android.view.View; @@ -58,52 +51,141 @@ */ 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; @@ -188,6 +270,13 @@ private void init(Context context, AttributeSet attrs) { 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); @@ -199,7 +288,7 @@ private void init(Context context, AttributeSet attrs) { 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; @@ -240,7 +329,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { frame = new Rect(leftOffset, topOffset, leftOffset + frameWidth, topOffset + frameHeight); } - @SuppressLint("DrawAllocation") @Override public void onDraw(Canvas canvas) { @@ -250,7 +338,7 @@ public void onDraw(Canvas canvas) { if(scannerStart == 0 || scannerEnd == 0) { scannerStart = frame.top; - scannerEnd = frame.bottom - SCANNER_LINE_HEIGHT; + scannerEnd = frame.bottom - scannerLineHeight; } int width = canvas.getWidth(); @@ -270,7 +358,7 @@ public void onDraw(Canvas canvas) { 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, @@ -307,17 +395,17 @@ private void drawTextInfo(Canvas canvas, Rect frame) { 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); } /** @@ -349,7 +437,7 @@ private void drawLineScanner(Canvas canvas,Rect frame){ //线性渐变 LinearGradient linearGradient = new LinearGradient( frame.left, scannerStart, - frame.left, scannerStart + SCANNER_LINE_HEIGHT, + frame.left, scannerStart + scannerLineHeight, shadeColor(laserColor), laserColor, Shader.TileMode.MIRROR); @@ -357,9 +445,9 @@ private void drawLineScanner(Canvas canvas,Rect frame){ 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; } @@ -395,7 +483,7 @@ private void drawGridScanner(Canvas canvas,Rect frame){ } if(scannerStart + + + + + + + + \ No newline at end of file diff --git a/versions.gradle b/versions.gradle index 260e136..45676f2 100644 --- a/versions.gradle +++ b/versions.gradle @@ -1,7 +1,7 @@ //App def app_version = [:] -app_version.versionCode = 11 //androidx 12 -app_version.versionName = "1.1.2" +app_version.versionCode = 13 //androidx 14 +app_version.versionName = "1.1.3" ext.app_version = app_version //build version