Skip to content

Commit

Permalink
修复扫描结果点显示问题,默认显示扫描点
Browse files Browse the repository at this point in the history
  • Loading branch information
maning0303 committed Oct 22, 2020
1 parent 1812d8b commit 5759cf7
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 56 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ dependencies {
//颜色选择器
implementation 'com.github.duanhong169:colorpicker:1.1.6'

implementation 'com.github.maning0303:MNZXingCode:V2.1.4'
// implementation project(':libraryzxing')
// implementation 'com.github.maning0303:MNZXingCode:V2.1.4'
implementation project(':libraryzxing')
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public class CustomActivity extends AppCompatActivity implements View.OnClickLis
private String colorLine = "#FFFFFF00";
private String colorBackground = "#22FF0000";
private String colorStatusBar = "#00000000";
private String colorResultPoint = "#CCFFFF00";
private String colorResultPointStroke = "#FFFFFFFF";
/**
* 左边
*/
Expand Down Expand Up @@ -236,7 +238,7 @@ public void onColorPicked(int color) {
private String getHexString(int color) {
String format = String.format("#%X", color);
Log.e("=====", "format:" + format);
if("#0".equals(format)){
if ("#0".equals(format)) {
format = "#00000000";
Log.e("=====", "format:" + format);
}
Expand Down Expand Up @@ -284,7 +286,8 @@ public void scanCode(View view) {
.setFullScreenScan(mCbFullscreenScan.isChecked())
//二维码标记点
.isShowResultPoint(mCbScanPoint.isChecked())
.setResultPointConfigs(60, 30, 10, "#FFFFFFFF", "#7000A81F")
//单位dp
.setResultPointConfigs(36, 12, 3, colorResultPointStroke, colorResultPoint)
//状态栏设置
.setStatusBarConfigs(colorStatusBar, mCbStatusDark.isChecked())
//自定义遮罩
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void onScanSuccess(final String resultTxt, Bitmap barcode) {
public void run() {
finishSuccess(resultTxt);
}
}, 100);
}, 200);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public enum LaserStyle {
private boolean isFullScreenScan = false;
//是否显示扫描中心点
private boolean isShowResultPoint = false;
//扫描二维码中心点显示半径
private int resultPointRadiusCircle = 0;
//扫描二维码中心点宽高
private int resultPointWithdHeight = 0;
//扫描二维码中心点显示圆角
private int resultPointCorners = 0;
//扫描二维码中心点显示描边
Expand Down Expand Up @@ -115,7 +115,7 @@ private MNScanConfig(Builder builder) {
isFullScreenScan = builder.isFullScreenScan;
isSupportZoom = builder.isSupportZoom;
isShowResultPoint = builder.isShowResultPoint;
resultPointRadiusCircle = builder.resultPointRadiusCircle;
resultPointWithdHeight = builder.resultPointWithdHeight;
resultPointCorners = builder.resultPointCorners;
resultPointStrokeWidth = builder.resultPointStrokeWidth;
resultPointStrokeColor = builder.resultPointStrokeColor;
Expand All @@ -137,8 +137,8 @@ public boolean isShowResultPoint() {
return isShowResultPoint;
}

public int getResultPointRadiusCircle() {
return resultPointRadiusCircle;
public int getResultPointWithdHeight() {
return resultPointWithdHeight;
}

public int getResultPointCorners() {
Expand Down Expand Up @@ -271,9 +271,9 @@ public static class Builder {
//缩放器位置
private ZoomControllerLocation zoomControllerLocation = ZoomControllerLocation.Right;
//是否显示扫描中心点
private boolean isShowResultPoint = false;
private boolean isShowResultPoint = true;
//扫描二维码中心点显示半径
private int resultPointRadiusCircle = 0;
private int resultPointWithdHeight = 0;
//扫描二维码中心点显示圆角
private int resultPointCorners = 0;
//扫描二维码中心点显示描边
Expand Down Expand Up @@ -303,12 +303,21 @@ public Builder isShowResultPoint(boolean isShowResultPoint) {
return this;
}

public Builder setResultPointConfigs(int resultPointRadiusCircle,
/**
* 设置扫描点大小圆角等(单位dp)
* @param resultPointWithdHeight
* @param resultPointCorners
* @param resultPointStrokeWidth
* @param resultPointStrokeColor
* @param resultPointColor
* @return
*/
public Builder setResultPointConfigs(int resultPointWithdHeight,
int resultPointCorners,
int resultPointStrokeWidth,
String resultPointStrokeColor,
String resultPointColor) {
this.resultPointRadiusCircle = resultPointRadiusCircle;
this.resultPointWithdHeight = resultPointWithdHeight;
this.resultPointCorners = resultPointCorners;
this.resultPointStrokeWidth = resultPointStrokeWidth;
this.resultPointStrokeColor = resultPointStrokeColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
Expand Down Expand Up @@ -60,11 +65,11 @@ public final class ViewfinderView extends View {
private Result resultPoint;
private float scaleFactor;
private boolean showResultPoint;
private int pointColor;
private int pointBorderColor;
private float resultPointRadiusCircle = 60;
private float resultPointRadiusCorners = 60;
private float resultPointStrokeWidth = 10;
private int resultPointColor;
private int resultPointStrokeColor;
private int resultPointWithdHeight;
private int resultPointRadiusCorners;
private int resultPointStrokeWidth;

private Rect frame;
private String hintMsg;
Expand Down Expand Up @@ -98,8 +103,8 @@ public ViewfinderView(Context context, AttributeSet attrs) {
Resources resources = getResources();
maskColor = resources.getColor(R.color.mn_scan_viewfinder_mask);
laserColor = resources.getColor(R.color.mn_scan_viewfinder_laser);
pointColor = resources.getColor(R.color.mn_scan_viewfinder_laser_result_point);
pointBorderColor = resources.getColor(R.color.mn_scan_viewfinder_laser_result_point_border);
resultPointColor = resources.getColor(R.color.mn_scan_viewfinder_laser_result_point);
resultPointStrokeColor = resources.getColor(R.color.mn_scan_viewfinder_laser_result_point_border);
hintMsg = "将二维码放入框内,即可自动扫描";
//文字
paintText.setColor(Color.WHITE);
Expand Down Expand Up @@ -127,6 +132,10 @@ private void initSize() {
//网格扫描线先关配置
gridColumn = 24;
gridHeight = 0;
//扫描点大小
resultPointWithdHeight = CommonUtils.dip2px(context, 36);
resultPointRadiusCorners = CommonUtils.dip2px(context, 36);
resultPointStrokeWidth = CommonUtils.dip2px(context, 3);
}

/**
Expand Down Expand Up @@ -202,16 +211,31 @@ public void setScanConfig(MNScanConfig scanConfig) {

private void initResultPointConfigs() {
showResultPoint = mnScanConfig.isShowResultPoint();
resultPointRadiusCorners = mnScanConfig.getResultPointCorners();
resultPointRadiusCircle = mnScanConfig.getResultPointRadiusCircle();
resultPointStrokeWidth = mnScanConfig.getResultPointStrokeWidth();
String resultPointColor = mnScanConfig.getResultPointColor();
String resultPointStrokeColor = mnScanConfig.getResultPointStrokeColor();
if (!TextUtils.isEmpty(resultPointColor)) {
pointColor = Color.parseColor(resultPointColor);
}
if (!TextUtils.isEmpty(resultPointStrokeColor)) {
pointBorderColor = Color.parseColor(resultPointStrokeColor);
if (showResultPoint) {
resultPointRadiusCorners = CommonUtils.dip2px(context, mnScanConfig.getResultPointCorners());
resultPointWithdHeight = CommonUtils.dip2px(context, mnScanConfig.getResultPointWithdHeight());
resultPointStrokeWidth = CommonUtils.dip2px(context, mnScanConfig.getResultPointStrokeWidth());
String resultPointColorStr = mnScanConfig.getResultPointColor();
String resultPointStrokeColorStr = mnScanConfig.getResultPointStrokeColor();
if (resultPointWithdHeight == 0) {
resultPointWithdHeight = CommonUtils.dip2px(context, 36);
}
if (resultPointRadiusCorners == 0) {
resultPointRadiusCorners = CommonUtils.dip2px(context, 36);
}
if (resultPointStrokeWidth == 0) {
resultPointStrokeWidth = CommonUtils.dip2px(context, 3);
}
if (!TextUtils.isEmpty(resultPointColorStr)) {
resultPointColor = Color.parseColor(resultPointColorStr);
} else {
resultPointColor = context.getResources().getColor(R.color.mn_scan_viewfinder_laser_result_point);
}
if (!TextUtils.isEmpty(resultPointStrokeColorStr)) {
resultPointStrokeColor = Color.parseColor(resultPointStrokeColorStr);
} else {
resultPointStrokeColor = context.getResources().getColor(R.color.mn_scan_viewfinder_laser_result_point_border);
}
}
}

Expand Down Expand Up @@ -328,10 +352,10 @@ public void onDraw(Canvas canvas) {
} else if (laserStyle == MNScanConfig.LaserStyle.Grid) {
drawGridScanner(canvas, frame);
}
//结果点
drawableResultPoint(canvas);
//动画刷新
startAnimation();
//结果点
drawableResultPoint(canvas);
}

/**
Expand Down Expand Up @@ -436,6 +460,11 @@ public void setResultPoint(Result result, float scaleFactor) {
postInvalidate();
}

/**
* TODO:显示有问题,内圈和外圈有间距
*
* @param canvas
*/
public void drawableResultPoint(Canvas canvas) {
if (!showResultPoint) {
return;
Expand Down Expand Up @@ -464,35 +493,43 @@ public void drawableResultPoint(Canvas canvas) {
pointBottom = point;
}
}
float centerX = pointRight.getX() - (pointRight.getX() - pointBottom.getX()) / 2 + resultPointRadiusCircle;
float centerY = pointBottom.getY() - (pointBottom.getY() - pointRight.getY()) / 2 + resultPointRadiusCircle;
int centerX = (int) (pointRight.getX() - (pointRight.getX() - pointBottom.getX()) / 2);
int centerY = (int) (pointBottom.getY() - (pointBottom.getY() - pointRight.getY()) / 2);
//判断是不是全屏模式
if (!(mnScanConfig != null && mnScanConfig.isFullScreenScan())) {
centerX += frame.left;
centerY += frame.top;
}
paintResultPoint.setStyle(Paint.Style.STROKE);
paintResultPoint.setColor(pointBorderColor);
paintResultPoint.setStrokeWidth(resultPointStrokeWidth);
RectF rect0 = new RectF(
centerX - resultPointRadiusCircle,
centerY - resultPointRadiusCircle,
centerX + resultPointRadiusCircle,
centerY + resultPointRadiusCircle);
rect0.inset(resultPointStrokeWidth / 2, resultPointStrokeWidth / 2);
canvas.drawRoundRect(rect0, resultPointRadiusCorners, resultPointRadiusCorners, paintResultPoint);
paintResultPoint.setStyle(Paint.Style.FILL);
paintResultPoint.setColor(pointColor);
paintResultPoint.setStrokeWidth(0);
RectF rect = new RectF(
centerX - resultPointRadiusCircle,
centerY - resultPointRadiusCircle,
centerX + resultPointRadiusCircle,
centerY + resultPointRadiusCircle);
rect.inset(resultPointStrokeWidth, resultPointStrokeWidth);
canvas.drawRoundRect(rect, resultPointRadiusCorners, resultPointRadiusCorners, paintResultPoint);

GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setCornerRadius(resultPointRadiusCorners);
gradientDrawable.setStroke(resultPointStrokeWidth, resultPointStrokeColor);
gradientDrawable.setColor(resultPointColor);
gradientDrawable.setSize(resultPointWithdHeight, resultPointWithdHeight);
Bitmap bitmap = drawableToBitmap(gradientDrawable);
if (bitmap != null) {
canvas.drawBitmap(bitmap, centerX, centerY, paintResultPoint);
}
}
}

public Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();

// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}

}
2 changes: 1 addition & 1 deletion libraryzxing/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
<resources>
<color name="mn_scan_viewfinder_laser">#FFff3535</color>
<color name="mn_scan_viewfinder_mask">#80000000</color>
<color name="mn_scan_viewfinder_laser_result_point">#72FF3535</color>
<color name="mn_scan_viewfinder_laser_result_point">#CCFF3535</color>
<color name="mn_scan_viewfinder_laser_result_point_border">#FFFFFFFF</color>
</resources>

0 comments on commit 5759cf7

Please sign in to comment.