android利用zbar二维码扫描

源码下载

之前用zxing做开发,各种奇葩问题,横屏修等等,而且性能也不搞。被测试批了,没办法后来换了zbar。性能好多了。

直接上图,看看效果

2.界面上的查找框

/**
 *  2014-7-15   上午11:14:21
 *  Created By niexiaoqiang
 */

package com.example.qu;

import android.content.Context;
import android.graphics.Canvas;
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 = 30;
	private Paint finderMaskPaint;
	private int measureedWidth;
	private int measureedHeight;

	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);
		//画框
		zx_code_kuang.setBounds(middleRect);
		zx_code_kuang.draw(canvas);
		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(Paint.ANTI_ALIAS_FLAG);
		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;
	}

	@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);
	}
}

2.扫描的activity

package com.example.qu;

import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity implements SurfaceHolder.Callback {
	private Camera mCamera;
	private SurfaceHolder mHolder;
	private SurfaceView surface_view;
	private ImageScanner scanner;
	private Handler autoFocusHandler;
	private AsyncDecode asyncDecode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_zbar_finder);
		init();
	}

	private void init() {
		surface_view = (SurfaceView) findViewById(R.id.surface_view);
		mHolder = surface_view.getHolder();
		mHolder.addCallback(this);
		scanner = new ImageScanner();
		scanner.setConfig(0, Config.X_DENSITY, 3);
		scanner.setConfig(0, Config.Y_DENSITY, 3);
		autoFocusHandler = new Handler();
		asyncDecode = new AsyncDecode();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		if (mHolder.getSurface() == null) {
			return;
		}
		try {
			mCamera.stopPreview();
		} catch (Exception e) {
		}
		try {
			mCamera.setDisplayOrientation(90);
			mCamera.setPreviewDisplay(mHolder);
			mCamera.setPreviewCallback(previewCallback);
			mCamera.startPreview();
			mCamera.autoFocus(autoFocusCallback);
		} catch (Exception e) {
			Log.d("DBG", "Error starting camera preview: " + e.getMessage());
		}
	}

	/**
	 * 预览数据
	 */
	PreviewCallback previewCallback = new PreviewCallback() {
		public void onPreviewFrame(byte[] data, Camera camera) {
			if (asyncDecode.isStoped()) {
				Camera.Parameters parameters = camera.getParameters();
				Size size = parameters.getPreviewSize();
				Image barcode = new Image(size.width, size.height, "Y800");
				barcode.setData(data);
				asyncDecode = new AsyncDecode();
				asyncDecode.execute(barcode);
			}
		}
	};

	private class AsyncDecode extends AsyncTask<Image, Void, Void> {
		private boolean stoped = true;

		@Override
		protected Void doInBackground(Image... params) {
			stoped = false;
			Image barcode = params[0];
			int result = scanner.scanImage(barcode);
			if (result != 0) {
				//				mCamera.setPreviewCallback(null);
				//				mCamera.stopPreview();
				SymbolSet syms = scanner.getResults();
				System.out.println(syms);
				for (Symbol sym : syms) {
					Log.d("xiaoqiang", sym.getData());
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			stoped = true;
		}

		public boolean isStoped() {
			return stoped;
		}
	}

	/**
	 * 自动对焦回调
	 */
	AutoFocusCallback autoFocusCallback = new AutoFocusCallback() {
		public void onAutoFocus(boolean success, Camera camera) {
			autoFocusHandler.postDelayed(doAutoFocus, 1000);
		}
	};

	//自动对焦
	private Runnable doAutoFocus = new Runnable() {
		public void run() {
			if (null == mCamera || null == autoFocusCallback) {
				return;
			}
			mCamera.autoFocus(autoFocusCallback);
		}
	};

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		try {
			mCamera = Camera.open();
		} catch (Exception e) {
			mCamera = null;
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (mCamera != null) {
			mCamera.setPreviewCallback(null);
			mCamera.release();
			mCamera = null;
		}
	}
}

android利用zbar二维码扫描

时间: 2024-10-20 02:26:08

android利用zbar二维码扫描的相关文章

android利用zbar二维码扫描-(解决中文乱码及扫描区域定义)

写在最前(这是对上一篇博文的问题做的更新[android利用zbar二维码扫描]) project下载   zbarLib编译project  project下载0积分 bug 在2.3的系统中Holder须要设置type,否则会黑屏(感谢网友[(α ⒎待sんа)294439435]) com.example.qu.MainActivity的第50行mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 今天发现 在com.exampl

android中Zing二维码扫描,二维码生成

Android中二维码扫描的最常用库是zxing和zbar,zxing项目地址为https://github.com/zxing/zxing,目前还有多个人在维护.zbar主要用C来写的,对速度有要求的可使用zbar,但目前没有在维护,项目地址:https://github.com/ZBar/ZBar. 1.添加依赖 在根build.gradle中添加maven { url 'https://jitpack.io' } allprojects { repositories { google()

Android &amp;quot;QR二维码扫描&amp;quot;

支持灯 扫描结果 支持 抄.分享.浏览打开(超链接) 自己主动保存扫描记录 划删除 和源代码 git: http://git.oschina.net/892642257/QRCode csdn(0分): http://download.csdn.net/detail/onlyonecoder/7713589      版权声明:本文博主原创文章.博客,未经同意不得转载.

【转】Android手机客户端关于二维码扫描的源码--不错

原文网址:https://github.com/SkillCollege/QrCodeScan QrCodeScan 这是Android手机客户端关于二维码扫描的源码,使用了高效的ZBar解码库,并修复了中文乱码. 融合了ZXing代码(使用其中的相机管理功能). 一. 使用开源ZXing扫描的缺点 1.原始代码是横屏模式,尽管可以改成竖屏,但是扫描界面的自定义和多屏幕适配不好做 2.有效扫描区域不好控制,可能是我自己技术不成熟,没找到好方法 3.ZXing是Java写的,对二维码的解析效率没有

【转】Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果--不错

原文网址:http://blog.csdn.net/xiaanming/article/details/10163203 转载请注明出处:http://blog.csdn.net/xiaanming/article/details/10163203 了解二维码这个东西还是从微信中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候,老板说要加上二维码扫描功能,然后自己的屁颠屁颠的去百度,google啥的,发现

XAMARIN ANDROID 二维码扫描示例

现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile  做一个简单的 Android 条码扫描示例程序. 1.新建一个 Xamarin.Android 应用: 阅读全文

Android二维码扫描登陆网页

1        二维码扫描登陆 1,web端生成二维码,传递uuid,并存入数据库 2,web端轮训查询信息,是否有数据库扫描二维码信息 3,手机端扫描二维码,获取UUID,传递用户名.密码web端 4,web查询数据库中用户信息(UserLogin),将登陆信息(用户名,密码)存入数据库. 5,web轮训查到用户登陆信息,信息完整,则登陆成功. 1.1    web端 1.1.1  导入依赖包(二维码生成QRCode.jar) jar包下载:QRCode.jar 1.1.2  用户登陆信息实

android 二维码 扫描与生成(内置)

本文使用 zxing-android-embedded 这个开源项目实现 二维码扫描/生成 功能: 开发工具:android studio 1.如何将zxing-android-embedded添加到我们的项目中 1.1  添加arr依赖包 将以下代码添加到build.gradle文件中. repositories {     mavenCentral()     maven {         url "https://dl.bintray.com/journeyapps/maven"

条码扫描二维码扫描—ZXing android 改进版本

看了Vurtexゞ. 文章<[Android实例] 条码扫描二维码扫描——ZXing android 源码简化 (附:支持中文) >的基础上对代码进行了修改 1.增加了将代码嵌套入自己工程后传值的办法(初学,如果有更好的方法也希望告诉我,先谢谢了) 2.扫码界面进行了处理(初步实现了现有某些软件的样子,至于长的像谁就不说了) //画四个角的代码<br>paint.setColor(frameColor); canvas.drawRect(15 + frame.left, 15 +