zbar跟zxing的比较:
1、扫描速度来说zbar貌似略胜一筹(没有进行过多的测试,下结论有点早)
2、集成时间来讲zbar也更快一些,除了jar,几个so库,java文件只有一个
3、zbar没有生成二维码功能
如果只做二维码扫描,zbar更简单省事儿一些
开头提到了笔者是通过上面大神的文章才开始了解zbar
zbar只有一个java文件,Viewfinder.java
看了代码之后感觉边框通过图片来加载有点死板,.9图片也可以 对于边框和四个角笔者更倾向于直接画出来,想zxing那样
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
/**
-
查找框
-
@author niexiaoqiang
*/
public class FinderView extends View {
private static final long ANIMATION_DELAY = 100L;
private Paint finderMaskPaint;
private int measureedWidth;
private int measureedHeight;
/**
- 手机的屏幕密度
*/
private static float density;
/**
- 四个绿色边角对应的长度
*/
private int ScreenRate;
/**
- 四个绿色边角对应的宽
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
度
*/
private static final int CORNER_WIDTH = 5;
/**
- 边框颜色
*/
private int middleRectColor;
private int maskColor;
public FinderView(Context context) {
super(context);
init(context);
}
public FinderView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawRect(leftRect, finderMaskPaint);
// canvas.drawRect(topRect, finderMaskPaint);
// canvas.drawRect(rightRect, finderMaskPaint);
// canvas.drawRect(bottomRect, finderMaskPaint);
// 画框(此方法是通过图片画出 看起来比较死板,在此参照zxing画边方法)
// zx_code_kuang.setBounds(middleRect);
// zx_code_kuang.draw(canvas);
// 获取屏幕的宽和高
int width = canvas.getWidth();
int height = canvas.getHeight();
// 画出扫描框外面的阴影部分,共四个部分,扫描框的上面到屏幕上面,扫描框的下面到屏幕下面
// 扫描框的左边面到屏幕左边,扫描框的右边到屏幕右边
// Draw the exterior (i.e. outside the framing rect) darkened
finderMaskPaint.setColor(maskColor);
canvas.drawRect(0, 0, width, middleRect.top, finderMaskPaint);
canvas.drawRect(0, middleRect.top, middleRect.left, middleRect.bottom + 1, finderMaskPaint);
canvas.drawRect(middleRect.right + 1, middleRect.top, width, middleRect.bottom + 1,
finderMaskPaint);
canvas.drawRect(0, middleRect.bottom + 1, width, height, finderMaskPaint);
/*
- zxing 画扫描框边上的角,总共8个部分
*/
finderMaskPaint.setColor(Color.GREEN);
canvas.drawRect(middleRect.left, middleRect.top, middleRect.left
- ScreenRate, middleRect.top + CORNER_WIDTH, finderMaskPaint);
canvas.drawRect(middleRect.left, middleRect.top, middleRect.left
- CORNER_WIDTH, middleRect.top + ScreenRate, finderMaskPaint);
canvas.drawRect(middleRect.right - ScreenRate, middleRect.top,
middleRect.right, middleRect.top + CORNER_WIDTH,
finderMaskPaint);
canvas.drawRect(middleRect.right - CORNER_WIDTH, middleRect.top,
middleRect.right, middleRect.top + ScreenRate, finderMaskPaint);
canvas.drawRect(middleRect.left, middleRect.bottom - CORNER_WIDTH,
middleRect.left + ScreenRate, middleRect.bottom,
finderMaskPaint);
canvas.drawRect(middleRect.left, middleRect.bottom - ScreenRate,
middleRect.left + CORNER_WIDTH, middleRect.bottom,
finderMaskPaint);
canvas.drawRect(middleRect.right - ScreenRate, middleRect.bottom
- CORNER_WIDTH, middleRect.right, middleRect.bottom,
finderMaskPaint);
canvas.drawRect(middleRect.right - CORNER_WIDTH, middleRect.bottom
- ScreenRate, middleRect.right, middleRect.bottom,
finderMaskPaint);
if (lineRect.bottom < middleRect.bottom) {
zx_code_line.setBounds(lineRect);
lineRect.top = lineRect.top + lineHeight / 2;
lineRect.bottom = lineRect.bottom + lineHeight / 2;
} else {
lineRect.set(middleRect);
lineRect.bottom = lineRect.top + lineHeight;
zx_code_line.setBounds(lineRect);
}
zx_code_line.draw(canvas);
postInvalidateDelayed(ANIMATION_DELAY, middleRect.left, middleRect.top,
middleRect.right, middleRect.bottom);
}
private Rect topRect = new Rect();
private Rect bottomRect = new Rect();
private Rect rightRect = new Rect();
private Rect leftRect = new Rect();
private Rect middleRect = new Rect();
private Rect lineRect = new Rect();
private Drawable zx_code_kuang;
private Drawable zx_code_line;
private int lineHeight;
private void init(Context context) {
int finder_mask = context.getResources().getColor(R.color.finder_mask);
finderMaskPaint = new Paint();
finderMaskPaint.setColor(finder_mask);
zx_code_kuang = context.getResources().getDrawable(
R.drawable.zx_code_kuang);