新增支持自定义扫码框宽高

This commit is contained in:
jenly1314
2018-12-29 16:30:51 +08:00
parent 3f0ea02a9a
commit 73ba5a80d4
6 changed files with 102 additions and 57 deletions

Binary file not shown.

View File

@@ -28,7 +28,10 @@ ZXingLite for Android 是ZXing的精简版基于ZXing库优化扫码和生成
| labelTextColor | color |<font color=#C0C0C0>#FFC0C0C0</font>| 提示文本字体颜色 | | labelTextColor | color |<font color=#C0C0C0>#FFC0C0C0</font>| 提示文本字体颜色 |
| labelTextSize | dimension |14sp| 提示文本字体大小 | | labelTextSize | dimension |14sp| 提示文本字体大小 |
| labelTextPadding | dimension |24dp| 提示文本距离扫描区的间距 | | labelTextPadding | dimension |24dp| 提示文本距离扫描区的间距 |
| labelTextLocation | enum |top| 提示文本信息显示的位置 | | showResultPoint | boolean | false | 是否显示合适的扫码结果点 |
| frameWidth | dimension | | 扫码框宽度需与frameHeight同时使用才有效 |
| frameHeight | dimension | | 扫码框高度需与frameWidth同时使用才有效 |
## 引入 ## 引入
@@ -37,17 +40,17 @@ ZXingLite for Android 是ZXing的精简版基于ZXing库优化扫码和生成
<dependency> <dependency>
<groupId>com.king.zxing</groupId> <groupId>com.king.zxing</groupId>
<artifactId>zxing-lite</artifactId> <artifactId>zxing-lite</artifactId>
<version>1.0.4</version> <version>1.0.5</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
``` ```
### Gradle: ### Gradle:
```gradle ```gradle
implementation 'com.king.zxing:zxing-lite:1.0.4' implementation 'com.king.zxing:zxing-lite:1.0.5'
``` ```
### Lvy: ### Lvy:
```lvy ```lvy
<dependency org='com.king.zxing' name='zxing-lite' rev='1.0.4'> <dependency org='com.king.zxing' name='zxing-lite' rev='1.0.5'>
<artifact name='$AID' ext='pom'></artifact> <artifact name='$AID' ext='pom'></artifact>
</dependency> </dependency>
``` ```

View File

@@ -22,7 +22,6 @@ import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient; import android.graphics.LinearGradient;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.Rect;
@@ -35,6 +34,7 @@ import android.text.StaticLayout;
import android.text.TextPaint; import android.text.TextPaint;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.View; import android.view.View;
@@ -56,7 +56,7 @@ public final class ViewfinderView extends View {
private static final long ANIMATION_DELAY = 20L; private static final long ANIMATION_DELAY = 20L;
private static final int CURRENT_POINT_OPACITY = 0xA0; private static final int CURRENT_POINT_OPACITY = 0xA0;
private static final int MAX_RESULT_POINTS = 20; private static final int MAX_RESULT_POINTS = 20;
private static final int POINT_SIZE = 6; private static final int POINT_SIZE = 8;
private static final int CORNER_RECT_WIDTH = 8; //扫描区边角的宽 private static final int CORNER_RECT_WIDTH = 8; //扫描区边角的宽
private static final int CORNER_RECT_HEIGHT = 40; //扫描区边角的高 private static final int CORNER_RECT_HEIGHT = 40; //扫描区边角的高
@@ -76,7 +76,7 @@ public final class ViewfinderView extends View {
//四角颜色 //四角颜色
private final int cornerColor; private final int cornerColor;
private final int resultPointColor; private final int resultPointColor;
private int scannerAlpha; // private int scannerAlpha;
private final float labelTextPadding; private final float labelTextPadding;
private TextLocation labelTextLocation; private TextLocation labelTextLocation;
//扫描区域提示文本 //扫描区域提示文本
@@ -86,7 +86,15 @@ public final class ViewfinderView extends View {
private float labelTextSize; private float labelTextSize;
public int scannerStart = 0; public int scannerStart = 0;
public int scannerEnd = 0; public int scannerEnd = 0;
private boolean isShowResultPoint = false; private boolean isShowResultPoint;
private int screenWidth;
private int screenHeight;
//扫码框宽
private int frameWidth;
//扫码框宽
private int frameHeight;
private List<ResultPoint> possibleResultPoints; private List<ResultPoint> possibleResultPoints;
private List<ResultPoint> lastPossibleResultPoints; private List<ResultPoint> lastPossibleResultPoints;
@@ -132,13 +140,25 @@ public final class ViewfinderView extends View {
labelTextPadding = array.getDimension(R.styleable.ViewfinderView_labelTextPadding,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,24,getResources().getDisplayMetrics())); labelTextPadding = array.getDimension(R.styleable.ViewfinderView_labelTextPadding,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,24,getResources().getDisplayMetrics()));
labelTextLocation = TextLocation.getFromInt(array.getInt(R.styleable.ViewfinderView_labelTextLocation,0)); labelTextLocation = TextLocation.getFromInt(array.getInt(R.styleable.ViewfinderView_labelTextLocation,0));
isShowResultPoint = array.getBoolean(R.styleable.ViewfinderView_showResultPoint,false);
frameWidth = array.getDimensionPixelSize(R.styleable.ViewfinderView_frameWidth,0);
frameHeight = array.getDimensionPixelSize(R.styleable.ViewfinderView_frameHeight,0);
array.recycle(); array.recycle();
paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
scannerAlpha = 0; // scannerAlpha = 0;
possibleResultPoints = new ArrayList<>(5); possibleResultPoints = new ArrayList<>(5);
lastPossibleResultPoints = null; lastPossibleResultPoints = null;
screenWidth = getDisplayMetrics().widthPixels;
screenHeight = getDisplayMetrics().heightPixels;
}
private DisplayMetrics getDisplayMetrics(){
return getResources().getDisplayMetrics();
} }
public void setCameraManager(CameraManager cameraManager) { public void setCameraManager(CameraManager cameraManager) {
@@ -164,12 +184,22 @@ public final class ViewfinderView extends View {
@SuppressLint("DrawAllocation") @SuppressLint("DrawAllocation")
@Override @Override
public void onDraw(Canvas canvas) { public void onDraw(Canvas canvas) {
Rect frame;
if(frameWidth > 0 && frameWidth < screenWidth && frameHeight > 0 && frameHeight < screenHeight){
//扫码框默认居中,当自定义扫码框宽高时,支持利用内距偏移
int leftOffset = (screenWidth - frameWidth) / 2 + getPaddingLeft() - getPaddingRight();
int topOffset = (screenHeight - frameHeight) / 2 + getPaddingTop() - getPaddingBottom();
frame = new Rect(leftOffset, topOffset, leftOffset + frameWidth, topOffset + frameHeight);
}else{
if (cameraManager == null) { if (cameraManager == null) {
return; // not ready yet, early draw before done configuring return; // not ready yet, early draw before done configuring
} }
Rect frame = cameraManager.getFramingRect(); frame = cameraManager.getFramingRect();
Rect previewFrame = cameraManager.getFramingRectInPreview(); }
if (frame == null || previewFrame == null) {
if (frame == null) {
return; return;
} }
@@ -202,46 +232,8 @@ public final class ViewfinderView extends View {
drawLaserScanner(canvas,frame); drawLaserScanner(canvas,frame);
//绘制提示信息 //绘制提示信息
drawTextInfo(canvas, frame); drawTextInfo(canvas, frame);
//绘制扫码结果点
float scaleX = frame.width() / (float) previewFrame.width(); drawResultPoint(canvas,frame);
float scaleY = frame.height() / (float) previewFrame.height();
List<ResultPoint> currentPossible = possibleResultPoints;
List<ResultPoint> currentLast = lastPossibleResultPoints;
int frameLeft = frame.left;
int frameTop = frame.top;
if (currentPossible.isEmpty()) {
lastPossibleResultPoints = null;
} else {
possibleResultPoints = new ArrayList<>(5);
lastPossibleResultPoints = currentPossible;
paint.setAlpha(CURRENT_POINT_OPACITY);
paint.setColor(resultPointColor);
synchronized (currentPossible) {
for (ResultPoint point : currentPossible) {
if(point.getX()<frame.left || point.getX()>frame.right ||
point.getY()<frame.top || point.getY()>frame.bottom){
continue;
}
canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
frameTop + (int) (point.getY() * scaleY),
POINT_SIZE, paint);
}
}
}
if (currentLast != null) {
paint.setAlpha(CURRENT_POINT_OPACITY / 2);
paint.setColor(resultPointColor);
synchronized (currentLast) {
float radius = POINT_SIZE / 2.0f;
for (ResultPoint point : currentLast) {
canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
frameTop + (int) (point.getY() * scaleY),
radius, paint);
}
}
}
// Request another update at the animation interval, but only repaint the laser line, // Request another update at the animation interval, but only repaint the laser line,
// not the entire viewfinder mask. // not the entire viewfinder mask.
postInvalidateDelayed(ANIMATION_DELAY, postInvalidateDelayed(ANIMATION_DELAY,
@@ -335,6 +327,49 @@ public final class ViewfinderView extends View {
canvas.drawRect(0, frame.bottom + 1, width, height, paint); canvas.drawRect(0, frame.bottom + 1, width, height, paint);
} }
//绘制扫码结果点
private void drawResultPoint(Canvas canvas,Rect frame){
if(!isShowResultPoint){
return;
}
List<ResultPoint> currentPossible = possibleResultPoints;
List<ResultPoint> currentLast = lastPossibleResultPoints;
if (currentPossible.isEmpty()) {
lastPossibleResultPoints = null;
} else {
possibleResultPoints = new ArrayList<>(5);
lastPossibleResultPoints = currentPossible;
paint.setAlpha(CURRENT_POINT_OPACITY);
paint.setColor(resultPointColor);
synchronized (currentPossible) {
for (ResultPoint point : currentPossible) {
if(point.getX()<frame.left || point.getX()>frame.right ||
point.getY()<frame.top || point.getY()>frame.bottom){
continue;
}
canvas.drawCircle( point.getX(),point.getY(), POINT_SIZE, paint);
}
}
}
if (currentLast != null) {
paint.setAlpha(CURRENT_POINT_OPACITY / 2);
paint.setColor(resultPointColor);
synchronized (currentLast) {
float radius = POINT_SIZE / 2.0f;
for (ResultPoint point : currentLast) {
if(point.getX()<frame.left || point.getX()>frame.right ||
point.getY()<frame.top || point.getY()>frame.bottom){
continue;
}
canvas.drawCircle( point.getX(),point.getY(), radius, paint);
}
}
}
}
public void drawViewfinder() { public void drawViewfinder() {
Bitmap resultBitmap = this.resultBitmap; Bitmap resultBitmap = this.resultBitmap;
this.resultBitmap = null; this.resultBitmap = null;
@@ -348,6 +383,10 @@ public final class ViewfinderView extends View {
return isShowResultPoint; return isShowResultPoint;
} }
/**
* 设置显示结果点
* @param showResultPoint 是否显示结果点
*/
public void setShowResultPoint(boolean showResultPoint) { public void setShowResultPoint(boolean showResultPoint) {
isShowResultPoint = showResultPoint; isShowResultPoint = showResultPoint;
} }

View File

@@ -34,7 +34,7 @@ final class AutoFocusManager implements Camera.AutoFocusCallback {
private static final String TAG = AutoFocusManager.class.getSimpleName(); private static final String TAG = AutoFocusManager.class.getSimpleName();
private static final long AUTO_FOCUS_INTERVAL_MS = 1000L; private static final long AUTO_FOCUS_INTERVAL_MS = 1200L;
private static final Collection<String> FOCUS_MODES_CALLING_AF; private static final Collection<String> FOCUS_MODES_CALLING_AF;
static { static {
FOCUS_MODES_CALLING_AF = new ArrayList<>(2); FOCUS_MODES_CALLING_AF = new ArrayList<>(2);

View File

@@ -13,6 +13,9 @@
<enum name="top" value="0"/> <enum name="top" value="0"/>
<enum name="bottom" value="1"/> <enum name="bottom" value="1"/>
</attr> </attr>
<attr name="showResultPoint" format="boolean"/>
<attr name="frameWidth" format="dimension"/>
<attr name="frameHeight" format="dimension"/>
</declare-styleable> </declare-styleable>
</resources> </resources>

View File

@@ -1,7 +1,7 @@
//App //App
def app_version = [:] def app_version = [:]
app_version.versionCode = 5 app_version.versionCode = 6
app_version.versionName = "1.0.4" app_version.versionName = "1.0.5"
ext.app_version = app_version ext.app_version = app_version
//build version //build version