android zbar二维码扫描(中文不乱码)

感谢大神总结的zbar文章:android利用zbar二维码扫描-(解决中文乱码及扫描区域定义)

还有大神的工程:android_zbar二维码扫描


前段时间用zxing做的二维码扫描,中间遇到一个头疼的问题的时候,ios同事说我用的zbar,当时只是查了查android也有,今天有时间看了看zbar

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;
	/**
	 * 四个绿色边角对应的宽度
	 */
	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);
		zx_code_line = context.getResources().getDrawable(
				R.drawable.zx_code_line);
		lineHeight = 30;
		// 获取屏幕密度
		density = context.getResources().getDisplayMetrics().density;
		// 计算当前屏幕密度下边框的厚度
		ScreenRate = (int) (15 * density);
		// 获取边框颜色资源
		middleRectColor = context.getResources().getColor(
				R.color.viewfinder_frame);
		maskColor = context.getResources().getColor(R.color.viewfinder_mask);
	}

	// ////////////新增该方法//////////////////////
	/**
	 * 根据图片size求出矩形框在图片所在位置,tip:相机旋转90度以后,拍摄的图片是横着的,所有传递参数时,做了交换
	 * 
	 * @param w
	 * @param h
	 * @return
	 */
	public Rect getScanImageRect(int w, int h) {
		// 先求出实际矩形
		Rect rect = new Rect();
		rect.left = middleRect.left;
		rect.right = middleRect.right;
		float temp = h / (float) measureedHeight;
		rect.top = (int) (middleRect.top * temp);
		rect.bottom = (int) (middleRect.bottom * temp);
		return rect;
	}

	// //////////////////////////////////
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		measureedWidth = MeasureSpec.getSize(widthMeasureSpec);
		measureedHeight = MeasureSpec.getSize(heightMeasureSpec);
		int borderWidth = measureedWidth / 2 + 100;
		middleRect.set((measureedWidth - borderWidth) / 2,
				(measureedHeight - borderWidth) / 2,
				(measureedWidth - borderWidth) / 2 + borderWidth,
				(measureedHeight - borderWidth) / 2 + borderWidth);
		lineRect.set(middleRect);
		lineRect.bottom = lineRect.top + lineHeight;
		leftRect.set(0, middleRect.top, middleRect.left, middleRect.bottom);
		topRect.set(0, 0, measureedWidth, middleRect.top);
		rightRect.set(middleRect.right, middleRect.top, measureedWidth,
				middleRect.bottom);
		bottomRect.set(0, middleRect.bottom, measureedWidth, measureedHeight);
	}
}


大喊一声:开源万岁!

你可能感兴趣的:(android,zxing,二维码,扫描二维码,zbar)