1、支持真实识别区域比例和识别区域偏移量可配置

2、对外暴露更多可配置参数
This commit is contained in:
jenly1314
2019-09-24 11:04:36 +08:00
parent 3465b76e96
commit 39f13c3c3f
10 changed files with 266 additions and 93 deletions

View File

@@ -17,10 +17,10 @@ package com.king.zxing;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.RectF;
import android.hardware.Camera;
import android.support.annotation.FloatRange;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.MotionEvent;
@@ -114,6 +114,19 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
*/
private boolean isFullScreenScan;
/**
* 识别区域比例范围建议在0.625 ~ 1.0之间默认0.9
*/
private float framingRectRatio = 0.9f;
/**
* 识别区域垂直方向偏移量
*/
private int framingRectVerticalOffset;
/**
* 识别区域水平方向偏移量
*/
private int framingRectHorizontalOffset;
private OnCaptureCallback onCaptureCallback;
@@ -139,6 +152,9 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
cameraManager = new CameraManager(activity);
cameraManager.setFullScreenScan(isFullScreenScan);
cameraManager.setFramingRectRatio(framingRectRatio);
cameraManager.setFramingRectVerticalOffset(framingRectVerticalOffset);
cameraManager.setFramingRectHorizontalOffset(framingRectHorizontalOffset);
callback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
@@ -162,15 +178,10 @@ public class CaptureHelper implements CaptureLifecycle,CaptureTouchEvent,Capture
}
};
onCaptureListener = new OnCaptureListener() {
@Override
public void onHandleDecode(Result result, Bitmap barcode, float scaleFactor) {
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
onResult(result);
}
onCaptureListener = (result, barcode, scaleFactor) -> {
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
onResult(result);
};
//设置是否播放音效和震动
beepManager.setPlayBeep(isPlayBeep);
@@ -309,6 +320,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();
@@ -332,13 +344,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);
});
}
@@ -421,18 +430,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;
}
@@ -599,6 +605,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;
}
/**
* 设置扫码回调

View File

@@ -17,17 +17,11 @@ package com.king.zxing;
*/
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.TextPaint;
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 @@ import java.util.List;
*/
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<ResultPoint> possibleResultPoints;
private List<ResultPoint> lastPossibleResultPoints;
@@ -188,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);
@@ -199,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;
@@ -240,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) {
@@ -250,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();
@@ -270,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,
@@ -307,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);
}
/**
@@ -349,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);
@@ -357,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;
}
@@ -395,7 +483,7 @@ public final class ViewfinderView extends View {
}
if(scannerStart<scannerEnd){
scannerStart += SCANNER_LINE_MOVE_DISTANCE;
scannerStart += scannerLineMoveDistance;
} else {
scannerStart = frame.top;
}
@@ -420,10 +508,10 @@ public final class ViewfinderView extends View {
*/
private void drawFrame(Canvas canvas, Rect frame) {
paint.setColor(frameColor);
canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2, paint);
canvas.drawRect(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1, paint);
canvas.drawRect(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1, paint);
canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1, paint);
canvas.drawRect(frame.left, frame.top, frame.right, frame.top + frameLineWidth, paint);
canvas.drawRect(frame.left, frame.top, frame.left + frameLineWidth, frame.bottom, paint);
canvas.drawRect(frame.right - frameLineWidth, frame.top, frame.right, frame.bottom, paint);
canvas.drawRect(frame.left, frame.bottom - frameLineWidth, frame.right, frame.bottom, paint);
}
/**
@@ -436,9 +524,9 @@ public final class ViewfinderView extends View {
private void drawExterior(Canvas canvas, Rect frame, int width, int height) {
paint.setColor(maskColor);
canvas.drawRect(0, 0, width, frame.top, paint);
canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
canvas.drawRect(0, frame.bottom + 1, width, height, paint);
canvas.drawRect(0, frame.top, frame.left, frame.bottom, paint);
canvas.drawRect(frame.right, frame.top, width, frame.bottom, paint);
canvas.drawRect(0, frame.bottom, width, height, paint);
}
/**

View File

@@ -21,10 +21,11 @@ import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Camera;
import android.media.FaceDetector;
import android.os.Handler;
import android.support.annotation.FloatRange;
import android.util.Log;
import android.view.SurfaceHolder;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.king.zxing.camera.open.OpenCamera;
import com.king.zxing.camera.open.OpenCameraInterface;
@@ -61,6 +62,10 @@ public final class CameraManager {
private int requestedFramingRectHeight;
private boolean isFullScreenScan;
private float framingRectRatio;
private int framingRectVerticalOffset;
private int framingRectHorizontalOffset;
/**
* Preview frames are delivered here, which we pass on to the registered handler. Make sure to
* clear the handler so it will only receive one message.
@@ -235,10 +240,10 @@ public final class CameraManager {
if(isFullScreenScan){
framingRect = new Rect(0,0,width,height);
}else{
int size = Math.min(width,height);
int size = (int)(Math.min(width,height) * framingRectRatio);
int leftOffset = (width - size) / 2;
int topOffset = (height - size) / 2;
int leftOffset = (width - size) / 2 + framingRectHorizontalOffset;
int topOffset = (height - size) / 2 + framingRectVerticalOffset;
framingRect = new Rect(leftOffset, topOffset, leftOffset + size, topOffset + size);
}
@@ -284,14 +289,22 @@ public final class CameraManager {
return framingRectInPreview;
}
public boolean isFullScreenScan() {
return isFullScreenScan;
}
public void setFullScreenScan(boolean fullScreenScan) {
isFullScreenScan = fullScreenScan;
}
public void setFramingRectRatio(@FloatRange(from = 0.0f ,to = 1.0f) float framingRectRatio) {
this.framingRectRatio = framingRectRatio;
}
public void setFramingRectVerticalOffset(int framingRectVerticalOffset) {
this.framingRectVerticalOffset = framingRectVerticalOffset;
}
public void setFramingRectHorizontalOffset(int framingRectHorizontalOffset) {
this.framingRectHorizontalOffset = framingRectHorizontalOffset;
}
public Point getCameraResolution() {
return configManager.getCameraResolution();
}
@@ -355,9 +368,9 @@ public final class CameraManager {
if(isFullScreenScan){
return new PlanarYUVLuminanceSource(data,width,height,0,0,width,height,false);
}
int size = Math.min(width,height);
int left = (width-size)/2;
int top = (height-size)/2;
int size = (int)(Math.min(width,height) * framingRectRatio);
int left = (width-size)/2 + framingRectHorizontalOffset;
int top = (height-size)/2 + framingRectVerticalOffset;
// Go ahead and assume it's YUV rather than die.
return new PlanarYUVLuminanceSource(data, width, height, left, top,
size, size, false);