写在最前(这是对上一篇博文的问题做的更新【android利用zbar二维码扫描】)
前天早上登陆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);}@Overrideprotected 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;}@Overrideprotected 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;@Overrideprotected 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();}@Overridepublic 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 = "";@Overrideprotected 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;}@Overrideprotected 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);}};@Overridepublic void surfaceCreated(SurfaceHolder holder) {try {mCamera = Camera.open();} catch (Exception e) {mCamera = null;}}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if (mCamera != null) {mCamera.setPreviewCallback(null);mCamera.release();mCamera = null;}}

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

关于乱码的解决

我重新使用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");
/*********************************************/

NDK编译这块儿有麻烦的朋友,估计就要好好学习下了。 东西太多,不好些博文

附代码下载: http://download.csdn.net/download/nie312122330/7747533

文章来原: http://www.itnose.net/detail/6083222.html

[转载]android利用zbar二维码扫描-(解决中文乱码及扫描区域定义)相关推荐

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

    写在最前(这是对上一篇博文的问题做的更新[android利用zbar二维码扫描]) project下载   zbarLib编译project  project下载0积分 bug 在2.3的系统中Hol ...

  2. qrcode生成二维码图片命名中文乱码解决

    转换字符编码为GBK function charsetToGBK($mixed) {if (is_array($mixed)) {foreach ($mixed as $k => $v) {if ...

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

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

  4. 在没有个人/公司网站的情况下,如何利用同一个二维码自动识别手机系统(Android/IOS)跳转不同下载页面

    一.使用场景 开发了一款App,包括iOS及Android版,到了推广阶段,准备生成二维码让用户扫码下载,那这个二维码该怎么生成?iOS及Andorid各自生成一个二维码让用户区分下载?当然这种方式是 ...

  5. Android 应用之二维码扫描登录

    下面介绍二维码扫描登录原理, 首先需要web服务端,和app客户端. web服务端主要工作是生成二维码,检测客户端提交信息正确性,更新网页界面. app客户端主要工作是扫描二维码,提交账户信息(此不是 ...

  6. 基于ZXing Android实现生成二维码图片和相机扫描二维码图片即时解码的功能

    NextQRCode ZXing开源库的精简版 **基于ZXing Android实现生成二维码图片和相机扫描二维码图片即时解码的功能 原文博客 附源码下载地址** 与原ZXingMini项目对比 N ...

  7. iOS和Android使用同一个二维码自动跳转不同下载页面链接(附生成二维码地址方法)

    一.使用场景 开发了一款App,包括iOS及Android版,到了推广阶段,准备生成二维码让用户扫码下载,那这个二维码该怎么生成?iOS及Andorid各自生成一个二维码让用户区分下载?当然这种方式是 ...

  8. Android平台下二维码漏洞攻击杂谈

    路人甲 · 2015/12/02 12:42 0x00 前言 现在Android App几乎都有二维码扫描功能,如果没有考虑到二维码可能存在的安全问题,将会导致扫描二维码就会受到漏洞攻击,严重的可能导 ...

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

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

最新文章

  1. 干货|重磅发布:人工智能行业应用价值报告(PDF报告下载)
  2. 【C++】 为什么C++空类占一个字节
  3. UOJ #576. 积的第K小数
  4. python json dumps utf8_Python2操作JSON出现乱码的解决方案
  5. python在线搭建教程_理解python web开发,轻松搭建web app!
  6. html中选择日期怎么实现,JavaScript+HTML5实现的日期比较功能示例
  7. goldendb基于mysql_中兴通讯GoldenDB在中信银行信用卡核心应用实践
  8. 在浏览器中运行java applet
  9. Spring学习(三)
  10. 为nopcommerce自定义用户积分功能(1)
  11. Mac的触控板坏了怎么办?将键盘变成鼠标的方法
  12. (超详细)夜间灯光总值TNLI如何得到
  13. python基础篇--从零开始(中)PyCharm、Vscode安装。
  14. 机顶盒直播点播源码方案开发
  15. iphone配置实用工具iPhone Configuration Utility
  16. IF:4+ 铁代谢和免疫相关基因标记预测三阴性乳腺癌的临床结局和分子特征
  17. VScode中使用platformIO开发,编译时找不到自己的源文件(报错信息:undefined reference to )
  18. AEC 声学回声消除
  19. Problem - 1077E - E. Thematic Contests(暴力+二分)
  20. java aes iv_java AES加密解密

热门文章

  1. keepalived原理 mysql_高可用实现KeepAlived原理简介
  2. 5位数的数字黑洞是多少_目前对于6174数字黑洞现象是否有合理的解释或证明?...
  3. 惠普n54l gen7 安装linux,HP GEN7改造NAS
  4. 有哪些句子是真正写到你的心里去了?
  5. 示波器中符号的意思是什么
  6. 软件测试工程师——你不仅仅应该会点点点
  7. 主线程和子线程的关系(讨论主线程结束,子线程是否要回收)
  8. linux中who命令显示的pts/0和(:0)(:0.0)是什么意思?
  9. 操作系统启动篇--01
  10. 如何选择一款优质的商城系统?