写在最前(这是对上一篇博文的问题做的更新【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.example.qu.MainActivity中有个静态共享库没有载入进来。在低版本号的ANDROID中会报错

改动方法:

在com.example.qu.MainActivity的第33行新增代码:

static {

System.loadLibrary("iconv");

}

前天早上登陆CSDN时候一条消息:一网友提出了两点疑惑

1.扫描框眼下仅仅是做的假象。是全屏的图片进行解析

2.中文乱码现象

各种查找,今天得以修复:及时共享给各位网友

补张图(有图有真相)

1.扫描框定义

/**
* 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;
} //////////////新增该方法//////////////////////
/**
* 依据图片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);
}
}

2.扫描界面

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.graphics.Rect;
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;
import android.widget.TextView; public class MainActivity extends Activity implements SurfaceHolder.Callback {
private static String TAG = "xiaoqiang";
private Camera mCamera;
private SurfaceHolder mHolder;
private SurfaceView surface_view;
private ImageScanner scanner;
private Handler autoFocusHandler;
private AsyncDecode asyncDecode; private FinderView finder_view;
private TextView textview; @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);
finder_view = (FinderView) findViewById(R.id.finder_view);
textview = (TextView) findViewById(R.id.textview);
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);
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
//图片是被旋转了90度的
Image source = new Image(size.width, size.height, "Y800");
Rect scanImageRect = finder_view.getScanImageRect(size.height, size.width);
//图片旋转了90度,将扫描框的TOP作为left裁剪
source.setCrop(scanImageRect.top, scanImageRect.left, scanImageRect.bottom, scanImageRect.right);
source.setData(data);
asyncDecode = new AsyncDecode();
asyncDecode.execute(source); }
}
}; private class AsyncDecode extends AsyncTask<Image, Void, Void> {
private boolean stoped = true;
private String str = ""; @Override
protected Void doInBackground(Image... params) {
stoped = false;
StringBuilder sb = new StringBuilder();
Image barcode = params[0];
int result = scanner.scanImage(barcode);
if (result != 0) {
// mCamera.setPreviewCallback(null);
// mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
switch (sym.getType()) {
case Symbol.CODABAR:
Log.d(TAG, "条形码 " + sym.getData());
//条形码
sb.append(sym.getData() + "\n");
break;
case Symbol.CODE128:
//128编码格式二维码
Log.d(TAG, "128编码格式二维码: " + sym.getData());
sb.append(sym.getData() + "\n");
break;
case Symbol.QRCODE:
//QR码二维码
Log.d(TAG, "QR码二维码 :" + sym.getData());
sb.append(sym.getData() + "\n");
break;
case Symbol.ISBN10:
//ISBN10图书查询
Log.d(TAG, "ISBN10图书查询 : " + sym.getData());
sb.append(sym.getData() + "\n");
break;
case Symbol.ISBN13:
//ISBN13图书查询
Log.d(TAG, "ISBN13图书查询 : " + sym.getData());
sb.append(sym.getData() + "\n");
break;
case Symbol.NONE:
Log.d(TAG, "未知 : " + sym.getData());
sb.append(sym.getData() + "\n");
break;
default:
Log.d(TAG, "其它: " + sym.getData());
sb.append(sym.getData() + "\n");
break;
}
}
}
str = sb.toString();
return null;
} @Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
stoped = true;
if (null == str || str.equals("")) {
} else {
textview.setText(str);
}
} 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;
}
}<pre name="code" class="html">

3.布局定义

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <com.example.qu.FinderView
android:id="@+id/finder_view"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center_vertical"
android:textColor="@color/rea" /> </FrameLayout>

一些资源文件请下载源代码,假设没有积分可QQ联系312122330

//关于乱码的解决    zbarLib编译project

1.我又一次使用NDK编译了zbar的库。并对zbarlib\jni\zbar\qrcode\qrdectxt.c做了例如以下改动

/*This is the encoding the standard says is the default.*/

/*********************************************/

/*********   改动此处以支持中文                                      **********/

/*********************************************/

//latin1_cd=iconv_open("UTF-8","ISO8859-1");

latin1_cd = iconv_open("UTF-8", "GB18030");





/*But this one is often used, as well.*/

//sjis_cd = iconv_open("UTF-8", "SJIS");

sjis_cd = iconv_open("UTF-8", "GB2312");

/*********************************************/

2.NDK编译这块儿有麻烦的朋友。预计就要好好学习下了。

东西太多,不好些博文

android利用zbar二维码扫描-(解决中文乱码及扫描区域定义)的更多相关文章

  1. Android快速实现二维码扫描--Zbar

    Android中二维码扫描的最常用库是zxing和zbar,上一篇<Android快速实现二维码扫描–Zxing>介绍了Zxing.这次说Zbar,Zbar速度极快,我就比较常用,项目地址 ...

  2. Android二维码的生成,解析以及扫描功能

    <1> 布局只有2个按钮,实现生成二维码和解析二维码 <Button android:layout_width="wrap_content" android:la ...

  3. Android开发——Android中的二维码生成与扫描

    0. 前言 今天这篇文章主要描述二维码的生成与扫描,使用目前流行的Zxing,为什么要讲二维码,因为二维码太普遍了,随便一个Android APP都会有二维码扫描.本篇旨在帮助有需求的同学快速完成二维 ...

  4. Opencv+Zbar二维码识别(二维码校正)

    二维码和车牌识别基本都会涉及到图像的校正,主要是形变和倾斜角度的校正,一种二维码的畸变如下图: 这个码用微信扫了一下,识别不出来,但是用Zbar还是可以准确识别的~~. 这里介绍一种二维码校正方法,通 ...

  5. Android开发 QRCode二维码开发第三方框架

    前言 Android开发里二维码开发经常用到,这里简单的介绍下Android开发里的二维码. 最广泛使用的二维码库zxing zxing是最广泛的二维码库各个平台都可以适用它,但是Android平台使 ...

  6. Android Wear和二维码

    这是一篇发布在Android官方开发者社区博客,15年年初的时候就看到了这篇文章,直到现在才有时间把它翻译下来. 这是一篇如何在Android Wear上面如何正确地展示二维码的文章,里面有许多的经验 ...

  7. tornado zbar 二维码识别 ,配合nginx 反向代理,supervisord 监控

    tornado zbar 二维码识别 ,配合nginx 反向代理,supervisord 监控 1.zbar识别二维码程序,python2.6.6 #!/usr/bin/env python # co ...

  8. jquery.qrcode.js 生成二维码并支持中文的方法

    GitHub地址: https://github.com/jeromeetienne/jquery-qrcode <div class="QR"></div> ...

  9. Android快速实现二维码扫描--Zxing

    Android中二维码扫描的最常用库是zxing和zbar,zxing项目地址为https://github.com/zxing/zxing,目前还有多个人在维护.zbar主要用C来写的,对速度有要求 ...

随机推荐

  1. C#调用Geocoding API进行地理编码与逆编码

    使用C#调用Geocoding API来将地址转为经纬度,或者将经纬度转变为具体的地址. Geocoding API的详细介绍参见:http://developer.baidu.com/map/web ...

  2. BZOJ3232: 圈地游戏

    题解: 神题一道... 题解戳这里:http://hi.baidu.com/strongoier/item/0425f0e5814e010265db0095 分数规划可以看这里:http://blog ...

  3. Oracle 10g DataGuard手记之基础配置

    DataGuard为企业数据的高可用性,数据安全以及灾难恢复提供支持,一般由一个primary db与几个物理或逻辑standby db组成一个DataGuard配置. 系统环境 操作系统为windo ...

  4. 【转】angular通过$http与服务器通信

    http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View angular是一个前端框架,实现了可交互式的页面,但是对于一个web应用,页面 ...

  5. 【Android】Android ListViewAnimations分析

    使用:https://github.com/android-cn/android-open-project-demo/tree/master/listview-animations-demo APK例 ...

  6. 单点登录系统(SSO)的开发思路

    单点登录并不是一个新鲜的玩意儿,比较官方的解释是企业业务整合的解决方案之一,通俗来讲SSO就是一个通用的用户中心,国内比较流行的UCenter就是一套单点登录解决方案.而近期以CSDN明文存储用户密码 ...

  7. C# Multilanguage messagebox z

    Either way, can't you just call MessageBox.Show(rm.GetString("messageboxData", ci)) class ...

  8. BinaryReader和BinaryWriter的leaveOpen参数 z

    在.NET 4.5后,微软为BinaryWriter和BinaryReader类型的构造函数中加入了leaveOpen参数,当该参数为true后,BinaryReader或者BinaryWriter关 ...

  9. HDU 1561-The more, The Better(树状背包)

    题意: n个城堡,每个有一定的财富,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡,求应攻克哪m个城堡,获得最大财富. 分析:dp[i][j],以i为根的子树,攻克j个城堡,获得的 ...

  10. Zabbix探索:网络设备监控3

    这两天主要是建立Cisco和H3C的网络设备监控模板.在建立的过程中就能明显感受到两家企业的深层次的东西. Cisco设备的CPU等信息都很好找,而且有官方的SNMP Object Navigator ...