Skip to content

Commit

Permalink
新增扫描框颜色配置
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhiqiang1993 committed May 30, 2018
1 parent 4ee0b2d commit 99e9e53
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 31 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
6 changes: 4 additions & 2 deletions app/src/main/java/com/yzq/zxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ public void onClick(View v) {
public void onAction(List<String> permissions) {
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);

/*ZxingConfig是配置类 可以设置是否显示底部布局,闪光灯,相册,是否播放提示音 震动等动能
/*ZxingConfig是配置类 可以设置是否显示底部布局,闪光灯,相册,是否播放提示音 震动 扫描框颜色等动能
* 也可以不传这个参数
* 不传的话 默认都为默认不震动 其他都为true
*
* */

ZxingConfig config = new ZxingConfig();
config.setPlayBeep(true);
config.setShake(true);
config.setDecodeBarCode(false);
config.setReactColor(R.color.colorAccent);
//config.setFrameLineColor(R.color.colorAccent);
config.setFullScreenScan(true);
intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ private void initView() {
previewView.setOnClickListener(this);

viewfinderView = findViewById(R.id.viewfinder_view);
viewfinderView.setOnClickListener(this);
viewfinderView.setZxingConfig(config);


backIv = findViewById(R.id.backIv);
backIv.setOnClickListener(this);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.yzq.zxinglibrary.bean;

import android.support.annotation.ColorRes;

import com.yzq.zxinglibrary.R;

import java.io.Serializable;

/**
Expand All @@ -23,10 +27,57 @@ public class ZxingConfig implements Serializable {
/*是否显示相册按钮*/
private boolean isShowAlbum = true;
/*是否解析条形码*/
private boolean isDecodeBarCode=true;
private boolean isDecodeBarCode = true;
/*是否全屏扫描*/
private boolean isFullScreenScan=true;
private boolean isFullScreenScan = true;

/*四个角的颜色*/
@ColorRes
private int reactColor = R.color.react;
/*扫描框颜色*/
@ColorRes
private int frameLineColor = -1;

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


public int getFrameLineColor() {
return frameLineColor;
}

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 getReactColor() {
return reactColor;
}

public void setReactColor(@ColorRes int reactColor) {
this.reactColor = reactColor;
}

public boolean isFullScreenScan() {
return isFullScreenScan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

Expand All @@ -34,10 +34,13 @@ public final class ViewfinderView extends View {
private CameraManager cameraManager;
private final Paint paint;
private Bitmap resultBitmap;
private final int maskColor; // 取景框外的背景颜色
private final int resultColor;// result Bitmap的颜色
private final int resultPointColor; // 特征点的颜色
private final int statusColor; // 提示文字颜色
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 All @@ -50,10 +53,12 @@ public final class ViewfinderView extends View {
private int scanLightHeight = 20;
// 扫描线
Bitmap scanLight;
private ZxingConfig config;


public ViewfinderView(Context context) {
this(context, null);

}

public ViewfinderView(Context context, AttributeSet attrs) {
Expand All @@ -63,21 +68,37 @@ public ViewfinderView(Context context, AttributeSet attrs) {
}



public void setZxingConfig(ZxingConfig config) {

this.config = config;

// maskColor = ContextCompat.getColor(getContext(), config.getMaskViewColor());
reactColor = ContextCompat.getColor(getContext(), config.getReactColor());
// scanLineColor = ContextCompat.getColor(getContext(), config.getScanLineColor());

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


}


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


paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Resources resources = getResources();

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


possibleResultPoints = new ArrayList<ResultPoint>(10);
Expand Down Expand Up @@ -134,7 +155,7 @@ public void onDraw(Canvas canvas) {
// drawStatusText(canvas, frame, width);

/*绘制扫描线*/
drawScanLight(canvas, frame);
drawScanLight(canvas, frame);

float scaleX = frame.width() / (float) previewFrame.width();
float scaleY = frame.height() / (float) previewFrame.height();
Expand Down Expand Up @@ -194,27 +215,30 @@ private void drawFrameBounds(Canvas canvas, Rect frame) {


/*扫描框的边框线*/
paint.setColor(Color.WHITE);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);

canvas.drawRect(frame, paint);

if (frameLineColor != -1) {
paint.setColor(frameLineColor);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(frame, paint);


}



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

/*四个角的长度和宽度*/
int width = frame.width();
int corLength = (int) (width * 0.1);
int corLength = (int) (width * 0.07);
int corWidth = (int) (corLength * 0.2);


// if (corWidth > 15) {
// corWidth = 15;
// }

corWidth = corWidth > 15 ? 15 : corWidth;


Expand Down Expand Up @@ -300,6 +324,7 @@ private void drawScanLight(Canvas canvas, Rect frame) {
SCAN_VELOCITY = (int) (SCAN_VELOCITY > 10 ? Math.ceil(SCAN_VELOCITY) : 10);
scanLineTop += SCAN_VELOCITY;
}

Rect scanRect = new Rect(frame.left, scanLineTop, frame.right,
scanLineTop + scanLightHeight);
canvas.drawBitmap(scanLight, null, scanRect, paint);
Expand Down Expand Up @@ -339,4 +364,5 @@ public void addPossibleResultPoint(ResultPoint point) {
}
}


}
2 changes: 1 addition & 1 deletion zxinglibrary/src/main/res/layout/activity_capture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
android:gravity="center"
android:text="相册"

android:textColor="#ffffff"/>
android:textColor="#ffffff" />
</android.support.v7.widget.LinearLayoutCompat>


Expand Down
9 changes: 5 additions & 4 deletions zxinglibrary/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<!--zxing-->
<color name="contents_text">#000000</color>
<color name="encode_view">#ffffff</color>
<color name="possible_result_points">#ffbd21</color> <!-- Android standard ICS color -->
<color name="possible_result_points">#ffbd21</color>
<color name="result_minor_text">#c0c0c0</color>
<color name="result_points">#99cc00</color> <!-- Android standard ICS color -->
<color name="result_points">#99cc00</color>
<color name="result_text">#ffffff</color>
<color name="result_view">#000000</color>
<color name="status_text">#ffffff</color>
<color name="transparent">#00000000</color>
<color name="viewfinder_laser">#cc0000</color> <!-- Android standard ICS color -->
<color name="viewfinder_mask">#50000000</color>
<color name="react">#63B8FF</color>
<color name="scanLineColor">#63B8FF</color>


</resources>

0 comments on commit 99e9e53

Please sign in to comment.