From 3c81ecd35bfc2d53534eccdf8c5f3257c2eaf5ae Mon Sep 17 00:00:00 2001 From: yzq <1149635502@qq.com> Date: Thu, 25 Oct 2018 16:47:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=89=AB=E6=8F=8F=E7=BA=BF?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/yzq/zxing/MainActivity.java | 7 +- .../yzq/zxinglibrary/bean/ZxingConfig.java | 33 +++------ .../yzq/zxinglibrary/decode/DecodeThread.java | 3 + .../yzq/zxinglibrary/view/ViewfinderView.java | 74 +++++++++++-------- zxinglibrary/src/main/res/values/colors.xml | 1 + 5 files changed, 61 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/com/yzq/zxing/MainActivity.java b/app/src/main/java/com/yzq/zxing/MainActivity.java index 3989092..89db25a 100644 --- a/app/src/main/java/com/yzq/zxing/MainActivity.java +++ b/app/src/main/java/com/yzq/zxing/MainActivity.java @@ -114,9 +114,10 @@ public void onAction(List 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); } diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/bean/ZxingConfig.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/bean/ZxingConfig.java index 01bfb67..b1fc604 100644 --- a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/bean/ZxingConfig.java +++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/bean/ZxingConfig.java @@ -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; @@ -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; diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/decode/DecodeThread.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/decode/DecodeThread.java index 0fd561b..5418ee3 100644 --- a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/decode/DecodeThread.java +++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/decode/DecodeThread.java @@ -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. * @@ -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); diff --git a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/view/ViewfinderView.java b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/view/ViewfinderView.java index 2b94fd3..f793dc8 100644 --- a/zxinglibrary/src/main/java/com/yzq/zxinglibrary/view/ViewfinderView.java +++ b/zxinglibrary/src/main/java/com/yzq/zxinglibrary/view/ViewfinderView.java @@ -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; @@ -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 possibleResultPoints; @@ -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(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); @@ -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为取景框 @@ -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 @@ -168,7 +187,7 @@ public void onDraw(Canvas canvas) { drawScanLight(canvas, frame); /*绘制闪动的点*/ - // drawPoint(canvas, frame, previewFrame); + // drawPoint(canvas, frame, previewFrame); } } @@ -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(); @@ -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); } diff --git a/zxinglibrary/src/main/res/values/colors.xml b/zxinglibrary/src/main/res/values/colors.xml index ca6df3b..c8d5930 100644 --- a/zxinglibrary/src/main/res/values/colors.xml +++ b/zxinglibrary/src/main/res/values/colors.xml @@ -13,6 +13,7 @@ #50000000 #ffffff #ffffff + #ffffff