Skip to content

Commit

Permalink
新增扫描线颜色配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhiqiang1993 committed Oct 25, 2018
1 parent ea733dd commit 3c81ecd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 57 deletions.
7 changes: 4 additions & 3 deletions app/src/main/java/com/yzq/zxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ public void onAction(List<String> permissions) {
config.setPlayBeep(true);//是否播放扫描声音 默认为true
config.setShake(true);//是否震动 默认为true
config.setDecodeBarCode(true);//是否扫描条形码 默认为true
config.setReactColor(R.color.white);//设置扫描框四个角的颜色 默认为淡蓝色
config.setFrameLineColor(R.color.white);//设置扫描框边框颜色 默认无色
config.setFullScreenScan(true);//是否全屏扫描 默认为true 设为false则只会在扫描框中扫描
config.setReactColor(R.color.colorAccent);//设置扫描框四个角的颜色 默认为白色
config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
config.setScanLineColor(R.color.colorAccent);//设置扫描线的颜色 默认白色
config.setFullScreenScan(false);//是否全屏扫描 默认为true 设为false则只会在扫描框中扫描
intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
startActivityForResult(intent, REQUEST_CODE_SCAN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ public class ZxingConfig implements Serializable {
private int frameLineColor = -1;


// /*扫描线颜色*/
// @ColorRes
// private int scanLineColor = R.color.scanLineColor;
//
// /*遮罩颜色*/
// @ColorRes
// private int maskViewColor = R.color.viewfinder_mask;

/*扫描线颜色*/
@ColorRes
private int scanLineColor = R.color.scanLineColor;

public int getFrameLineColor() {
return frameLineColor;
Expand All @@ -56,21 +51,13 @@ public void setFrameLineColor(@ColorRes int frameLineColor) {
this.frameLineColor = frameLineColor;
}

// public int getScanLineColor() {
// return scanLineColor;
// }
//
// public void setScanLineColor(@ColorRes int scanLineColor) {
// this.scanLineColor = scanLineColor;
// }
//
// public int getMaskViewColor() {
// return maskViewColor;
// }
//
// public void setMaskViewColor(@ColorRes int maskViewColor) {
// this.maskViewColor = maskViewColor;
// }
public int getScanLineColor() {
return scanLineColor;
}

public void setScanLineColor(@ColorRes int scanLineColor) {
this.scanLineColor = scanLineColor;
}

public int getReactColor() {
return reactColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Vector;
import java.util.concurrent.CountDownLatch;

import javax.crypto.spec.DESedeKeySpec;

/**
* This thread does all the heavy lifting of decoding the images.
*
Expand Down Expand Up @@ -55,6 +57,7 @@ public DecodeThread(CaptureActivity activity, ResultPointCallback resultPointCal
/*是否解析有条形码(一维码)*/
if (activity.config.isDecodeBarCode()) {
decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);

}

decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
Expand All @@ -34,13 +33,14 @@ public final class ViewfinderView extends View {
private static final int POINT_SIZE = 6;

private CameraManager cameraManager;
private Paint paint, scanLinePaint;
private Paint paint, scanLinePaint, reactPaint, frameLinePaint;
private Bitmap resultBitmap;
private int maskColor; // 取景框外的背景颜色
private int resultColor;// result Bitmap的颜色
private int resultPointColor; // 特征点的颜色
private int statusColor; // 提示文字颜色
private int reactColor;//四个角的颜色
private int scanLineColor;//扫描线的颜色
private int frameLineColor = -1;//边框线的颜色


private List<ResultPoint> possibleResultPoints;
Expand Down Expand Up @@ -68,42 +68,61 @@ public ViewfinderView(Context context, AttributeSet attrs) {
public void setZxingConfig(ZxingConfig config) {
this.config = config;
reactColor = ContextCompat.getColor(getContext(), config.getReactColor());

if (config.getFrameLineColor() != -1) {
frameLineColor = ContextCompat.getColor(getContext(), config.getFrameLineColor());
}

scanLineColor = ContextCompat.getColor(getContext(), config.getScanLineColor());
initPaint();

}


public ViewfinderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);


maskColor = ContextCompat.getColor(getContext(), R.color.viewfinder_mask);
resultColor = ContextCompat.getColor(getContext(), R.color.result_view);
resultPointColor = ContextCompat.getColor(getContext(), R.color.possible_result_points);
statusColor = ContextCompat.getColor(getContext(), R.color.status_text);

possibleResultPoints = new ArrayList<ResultPoint>(10);
lastPossibleResultPoints = null;


initPaint();


}

private void initPaint() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);

scanLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
/*四个角的画笔*/
reactPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
reactPaint.setColor(reactColor);
reactPaint.setStyle(Paint.Style.FILL);
reactPaint.setStrokeWidth(dp2px(1));

/*边框线画笔*/

if (frameLineColor != -1) {
frameLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
frameLinePaint.setColor(ContextCompat.getColor(getContext(), config.getFrameLineColor()));
frameLinePaint.setStrokeWidth(dp2px(1));
frameLinePaint.setStyle(Paint.Style.STROKE);
}



/*扫描线画笔*/
scanLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
scanLinePaint.setStrokeWidth(dp2px(2));
scanLinePaint.setStyle(Paint.Style.FILL);
scanLinePaint.setDither(true);
scanLinePaint.setColor(Color.WHITE);
scanLinePaint.setColor(scanLineColor);

}

private void initAnimator() {


if (valueAnimator == null) {
valueAnimator = ValueAnimator.ofInt(frame.top, frame.bottom);
valueAnimator.setDuration(3000);
Expand Down Expand Up @@ -135,8 +154,9 @@ public void setCameraManager(CameraManager cameraManager) {
@SuppressLint("DrawAllocation")
@Override
public void onDraw(Canvas canvas) {

if (cameraManager == null) {
return; // not ready yet, early draw before done configuring
return;
}

// frame为取景框
Expand All @@ -156,7 +176,6 @@ public void onDraw(Canvas canvas) {
/*绘制取景框边框*/
drawFrameBounds(canvas, frame);


if (resultBitmap != null) {
// Draw the opaque result bitmap over the scanning rectangle
// 如果有二维码结果的Bitmap,在扫取景框内绘制不透明的result Bitmap
Expand All @@ -168,7 +187,7 @@ public void onDraw(Canvas canvas) {
drawScanLight(canvas, frame);

/*绘制闪动的点*/
// drawPoint(canvas, frame, previewFrame);
// drawPoint(canvas, frame, previewFrame);
}
}

Expand Down Expand Up @@ -243,17 +262,10 @@ private void drawMaskView(Canvas canvas, Rect frame, int width, int height) {
private void drawFrameBounds(Canvas canvas, Rect frame) {

/*扫描框的边框线*/
if (config.getFrameLineColor() != -1) {
paint.setColor(ContextCompat.getColor(getContext(), config.getFrameLineColor()));
paint.setStrokeWidth(dp2px(1));
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(frame, paint);
if (frameLineColor != -1) {
canvas.drawRect(frame, frameLinePaint);
}

/*扫描框的四个角*/
paint.setColor(reactColor);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(dp2px(1));

/*四个角的长度和宽度*/
int width = frame.width();
Expand All @@ -266,24 +278,24 @@ private void drawFrameBounds(Canvas canvas, Rect frame) {
/*角在线外*/
// 左上角
canvas.drawRect(frame.left - corWidth, frame.top, frame.left, frame.top
+ corLength, paint);
+ corLength, reactPaint);
canvas.drawRect(frame.left - corWidth, frame.top - corWidth, frame.left
+ corLength, frame.top, paint);
+ corLength, frame.top, reactPaint);
// 右上角
canvas.drawRect(frame.right, frame.top, frame.right + corWidth,
frame.top + corLength, paint);
frame.top + corLength, reactPaint);
canvas.drawRect(frame.right - corLength, frame.top - corWidth,
frame.right + corWidth, frame.top, paint);
frame.right + corWidth, frame.top, reactPaint);
// 左下角
canvas.drawRect(frame.left - corWidth, frame.bottom - corLength,
frame.left, frame.bottom, paint);
frame.left, frame.bottom, reactPaint);
canvas.drawRect(frame.left - corWidth, frame.bottom, frame.left
+ corLength, frame.bottom + corWidth, paint);
+ corLength, frame.bottom + corWidth, reactPaint);
// 右下角
canvas.drawRect(frame.right, frame.bottom - corLength, frame.right
+ corWidth, frame.bottom, paint);
+ corWidth, frame.bottom, reactPaint);
canvas.drawRect(frame.right - corLength, frame.bottom, frame.right
+ corWidth, frame.bottom + corWidth, paint);
+ corWidth, frame.bottom + corWidth, reactPaint);
}


Expand Down
1 change: 1 addition & 0 deletions zxinglibrary/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<color name="viewfinder_mask">#50000000</color>
<color name="react">#ffffff</color>
<color name="scanLineColor">#ffffff</color>
<color name="defaultColor">#ffffff</color>


</resources>

0 comments on commit 3c81ecd

Please sign in to comment.