我在项目中用到了二维码扫描的技术,用的是Google提供的ZXing开源项目,它提供二维码和条形码的扫描。扫描条形码就是直接读取条形码的内容,扫描二维码是按照自己指定的二维码格式进行编码和解码。

可以到http://code.google.com/p/zxing/下载ZXing项目的源码,然后按照官方文档进行开发,我这里使用的ZXing是经过简化版的,去除了一些一般使用不必要的文件,项目工程截图如下:

其中encoding包是我
在它的基础上自己加上去的,功能是根据传入的字符串来生成二维码图片,返回一个Bitmap,其余的包是ZXing项目自带的。另外对扫描界面的布局我也
进行了修改,官方的扫描界面是横向的,我改成了纵向的,并加入了顶部的Tab和取消按钮(camera.xml),另外还需要的一些文件是
colors.xml、ids.xml,这些都是原本ZXing项目中自带的,最后就是libs下面的jar包。

先来看看最后的效果:

首先是根据输入的字符串生成二维码图片(左图),然后扫描二维码图片可以在界面上显示扫描结果(右图):

              

点击Open Camera按钮代开扫描框(左图),扫描条形码结果如下(右图):

              

接下来看如何使用,首先是把ZXing项目中的一些文件拷贝到我们自己的项目中,然后在Mainifest文件中进行配置权限:

  1. <uses-permission android:name="android.permission.VIBRATE" />
  2. <uses-permission android:name="android.permission.CAMERA" />
  3. <uses-feature android:name="android.hardware.camera" />
  4. <uses-feature android:name="android.hardware.camera.autofocus" />

还有就是扫描界面Activity的配置:

  1. <activity
  2. android:configChanges="orientation|keyboardHidden"
  3. android:name="com.zxing.activity.CaptureActivity"
  4. android:screenOrientation="portrait"
  5. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  6. android:windowSoftInputMode="stateAlwaysHidden" >
  7. </activity>

接下来是我自己项目的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@android:color/white"
  6. android:orientation="vertical" >
  7. <Button
  8. android:id="@+id/btn_scan_barcode"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginTop="30dp"
  12. android:text="Open camera" />
  13. <LinearLayout
  14. android:orientation="horizontal"
  15. android:layout_marginTop="10dp"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content">
  18. <TextView
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:textColor="@android:color/black"
  22. android:textSize="18sp"
  23. android:text="Scan result:" />
  24. <TextView
  25. android:id="@+id/tv_scan_result"
  26. android:layout_width="fill_parent"
  27. android:textSize="18sp"
  28. android:textColor="@android:color/black"
  29. android:layout_height="wrap_content" />
  30. </LinearLayout>
  31. <EditText
  32. android:id="@+id/et_qr_string"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_marginTop="30dp"
  36. android:hint="Input the text"/>
  37. <Button
  38. android:id="@+id/btn_add_qrcode"
  39. android:layout_width="fill_parent"
  40. android:layout_height="wrap_content"
  41. android:text="Generate QRcode" />
  42. <ImageView
  43. android:id="@+id/iv_qr_image"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_marginTop="10dp"
  47. android:layout_gravity="center"/>
  48. </LinearLayout>

下面是主Activity的代码,主要功能是打开扫描框、显示扫描结果、根据输入的字符串生成二维码图片:

  1. public class BarCodeTestActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. private TextView resultTextView;
  4. private EditText qrStrEditText;
  5. private ImageView qrImgImageView;
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
  11. qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
  12. qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
  13. Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
  14. scanBarCodeButton.setOnClickListener(new OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. //打开扫描界面扫描条形码或二维码
  18. Intent openCameraIntent = new Intent(BarCodeTestActivity.this,CaptureActivity.class);
  19. startActivityForResult(openCameraIntent, 0);
  20. }
  21. });
  22. Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);
  23. generateQRCodeButton.setOnClickListener(new OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. try {
  27. String contentString = qrStrEditText.getText().toString();
  28. if (!contentString.equals("")) {
  29. //根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350)
  30. Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);
  31. qrImgImageView.setImageBitmap(qrCodeBitmap);
  32. }else {
  33. Toast.makeText(BarCodeTestActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
  34. }
  35. } catch (WriterException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39. }
  40. });
  41. }
  42. @Override
  43. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  44. super.onActivityResult(requestCode, resultCode, data);
  45. //处理扫描结果(在界面上显示)
  46. if (resultCode == RESULT_OK) {
  47. Bundle bundle = data.getExtras();
  48. String scanResult = bundle.getString("result");
  49. resultTextView.setText(scanResult);
  50. }
  51. }
  52. }

其中生成二维码图片的代码在EncodingHandler.java中:

  1. public final class EncodingHandler {
  2. private static final int BLACK = 0xff000000;
  3. public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
  4. Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
  5. hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  6. BitMatrix matrix = new MultiFormatWriter().encode(str,
  7. BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
  8. int width = matrix.getWidth();
  9. int height = matrix.getHeight();
  10. int[] pixels = new int[width * height];
  11. for (int y = 0; y < height; y++) {
  12. for (int x = 0; x < width; x++) {
  13. if (matrix.get(x, y)) {
  14. pixels[y * width + x] = BLACK;
  15. }
  16. }
  17. }
  18. Bitmap bitmap = Bitmap.createBitmap(width, height,
  19. Bitmap.Config.ARGB_8888);
  20. bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  21. return bitmap;
  22. }
  23. }

最后是在哪里对扫描结果进行解码,进入CaptureActivity.java找到下面这个方法便可以对自己对结果进行操作:

    1. /**
    2. * Handler scan result
    3. * @param result
    4. * @param barcode
    5. */
    6. public void handleDecode(Result result, Bitmap barcode) {
    7. inactivityTimer.onActivity();
    8. playBeepSoundAndVibrate();
    9. String resultString = result.getText();
    10. //FIXME
    11. if (resultString.equals("")) {
    12. Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();
    13. }else {
    14. //          System.out.println("Result:"+resultString);
    15. Intent resultIntent = new Intent();
    16. Bundle bundle = new Bundle();
    17. bundle.putString("result", resultString);
    18. resultIntent.putExtras(bundle);
    19. this.setResult(RESULT_OK, resultIntent);
    20. }
    21. CaptureActivity.this.finish();
    22. }
    23. 转自:http://blog.csdn.net/tangren03/article/details/7831826

二维码、条形码扫描——使用Google ZXing的更多相关文章

  1. java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例

    java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍   我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...

  2. iOS系统原生二维码条形码扫描

    本文讲述如何用系统自带的东东实现二维码扫描的功能:点击当前页面的某个按钮,创建扫描VIEW.细心的小伙伴可以发现 title被改变了,返回按钮被隐藏了.这个代码自己写就行了,与本文关系不大...绿色的 ...

  3. IOS原声二维码条形码扫描实现

    本文讲述如何用系统自带的东东实现二维码扫描的功能:点击当前页面的某个按钮,创建扫描VIEW.细心的小伙伴可以发现 title被改变了,返回按钮被隐藏了.这个代码自己写就行了,与本文关系不大...绿色的 ...

  4. zxing解析带logo二维码会报com.google.zxing.NotFoundException

    参考原文:https://blog.csdn.net/cyl1226541/article/details/70557232 //复杂模式,开启PURE_BARCODE模式(☆☆☆) HINTS.pu ...

  5. iOS雪花动画、音频图、新闻界面框架、2048游戏、二维码条形码扫码生成等源码

    iOS精选源码 粒子雪花与烟花的动画 iOS 2048游戏 JHSoundWaveView - 简单地声波图.音波图 一个可快速集成的新闻详情界面框架,类似今日头条,腾讯新闻 二维码/条形码扫描及扫描 ...

  6. 【转】 Android 基于google Zxing实现对手机中的二维码进行扫描--不错

    原文网址:http://blog.csdn.net/xiaanming/article/details/14450809 转载请注明出处:http://blog.csdn.net/xiaanming/ ...

  7. Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 现在的二维码可谓是烂大街了,到处都是二维码,什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

  8. Android 基于google Zxing实现对手机中的二维码进行扫描

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/14450809 有时候我们有这样子的需求,需要扫描手机中有二维码的的图片,所以今天实现的 ...

  9. Android高级控件(三)——&#160;使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 如今的二维码可谓是烂大街了.到处都是二维码.什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

  10. QRCode 扫描二维码、扫描条形码、相册获取图片后识别、生成带 Logo 二维码、支持微博微信 QQ 二维码扫描样式

    目录 功能介绍 常见问题 效果图与示例 apk Gradle 依赖 布局文件 自定义属性说明 接口说明 关于我 功能介绍 根据之前公司的产品需求,参考 barcodescanner 改的,希望能帮助到 ...

随机推荐

  1. 标准C程序设计七---34

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  2. asp传递参数的几种方式

    把下列代码分别加入a.asp和b.asp的<body></body>中,点提交,就可以将a.asp文本框的内容传给b.asp并显示出来 a.ASP <form actio ...

  3. 作为程序员,再也不想和PM干架了

    上周,又看见有程序和PM(产品经理)吵了起来,大致是因为晚上就要上线了,下午的时候PM来说要改点需求,但程序不愿意.兴许是天气热了,大家都很烦躁,于是一言不合就发飙了,最终还是程序老大介入才解决了问题 ...

  4. HDU 1011 Starship Troopers (树dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意: 题目大意是有n个房间组成一棵树,你有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个 ...

  5. android添加桌面悬浮窗

    1. 添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 2. ...

  6. free如何知道释放内存长度:vs与glibc分配内存时编译器内部处理

    鉴于网上这个资料实在太少,将以前整理过却未完全的一篇文章贴出来,希望大牛指正vs下内存管理方式.可联系gaoshiqiang1987@163.com vs分配内存 vs没有源码,编译器在分配内存时,分 ...

  7. maven打包自动配置数据库链接信息

    pom.xml加入下面代码 <profiles> <profile> <id>dev</id> <activation> <activ ...

  8. JSP/SERVLET新手教程--Servlet 使用入门

    如今的JSP书籍有的是直接讲述JSP的使用,然后再解说SERVERLET的使用;也有书籍是先讲述SERVERLET的使用,然后解说JSP使用.个人觉得另外一种相对好一些,至于原因大家能够在学习体会到! ...

  9. OpenGL超级宝典笔记——深度纹理和阴影 【转】

    目录[-] 光源视角 新型的纹理 深度纹理的大小 首先绘制阴影 然后是光照 投影阴影贴图 阴影比较 之前我们介绍过简单的把物体压平到投影平面来制造阴影.但这种阴影方式有其局限性(如投影平面须是平面). ...

  10. 转: Android开发的网络抓包

    方法就是在android机器上面安装tcpdump,然后通过-w参数把抓包记录到本地,再把抓到的.cap文件导到pc上面用wireshark来分析.这里步骤非常多,在和后台联调的时候,这个效率是非常低 ...