Android开发之google Zxing实现二维码扫描的代码分析

本文主要介绍了Android开发中使用google的Zxing库进行二维码扫描的技术实现。首先,文章概览了Zxing库的基本功能和在二维码扫描中的应用;随后,详细剖析了如何集成和使用Zxing进行二维码的读取和解析过程。
摘要由CSDN通过智能技术生成

1.技术简介

   在Android中实现二维码的扫描主要是通过第三方框架来实现的,主要框架是google的Zxing。现在就用该框架来实现二维码及条形码的扫描及识别,同时对于手机中存储的图片也进行识别,并通过编写一个Demo来学习并熟悉该框架的使用,为以后的实际项目应用打下基础。
       google Zxing框架的源码及jar包下载地址: http://code.google.com/p/zxing/ (在国内,需要翻墙才可以进行下载)

2.技术实现

  现在准备开发一个demo,能够实现二维码、条形码的扫描及识别功能。在UI方面,将仿造小米手机自带的二维码扫描APP的界面。界面如下图所示:


      在android studio中,建立项目QRCodeApp并引入Zxing的jar文件及相关的源代码,项目结构如下图所示:

  

                      
    在项目中,com.mining.app.zxing包中的内容是Zxing框架中相关源代码,涉及扫描框、摄像机录制图片、识别图片中的二维码信息等类。QRCodeScanActivity类便是二维码扫描的界面。界面的布局文件如下:
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <SurfaceView
  6. android:id="@+id/preview_view"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:layout_gravity="center" />
  10. <com.mining.app.zxing.view.ViewfinderView
  11. android:id="@+id/viewfinder_view"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_gravity="center"/>
  15. <LinearLayout
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:orientation="horizontal"
  19. android:layout_gravity="bottom|center_horizontal"
  20. android:layout_marginBottom="16dp">
  21. <TextView
  22. android:id="@+id/tv_flash"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:textSize="12sp"
  26. android:text="照明"/>
  27. <TextView
  28. android:id="@+id/tv_picture"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:textSize="12sp"
  32. android:layout_marginLeft="16dp"
  33. android:text="图片"/>
  34. </LinearLayout>
  35. </FrameLayout>
    在布局文件中,包括SurfaceView控件用于Android摄像头Camera的预览界面,ViewfinderView是Zxing包中二维码扫描框的自定义控件,另外,包括两个用于控制闪光灯开光及选取图片进行二维码扫描的按钮。
    以下是ViewfinderView类的源代码:
  
  
  
  1. /*
  2. * Copyright (C) 2008 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mining.app.zxing.view;
  17. import java.util.Collection;
  18. import java.util.HashSet;
  19. import android.content.Context;
  20. import android.content.res.Resources;
  21. import android.graphics.Bitmap;
  22. import android.graphics.Canvas;
  23. import android.graphics.Color;
  24. import android.graphics.Paint;
  25. import android.graphics.Rect;
  26. import android.graphics.Typeface;
  27. import android.util.AttributeSet;
  28. import android.view.View;
  29. import com.google.zxing.ResultPoint;
  30. import com.htt.qrcodeapp.R;
  31. import com.mining.app.zxing.camera.CameraManager;
  32. /**
  33. * This view is overlaid on top of the camera preview. It adds the viewfinder
  34. * rectangle and partial transparency outside it, as well as the laser scanner
  35. * animation and result points.
  36. * 自定义控件,实现扫描框
  37. *
  38. */
  39. public final class ViewfinderView extends View {
  40. private static final String TAG = "log";
  41. /**
  42. * 刷新界面的时间
  43. */
  44. private static final long ANIMATION_DELAY = 10L;
  45. private static final int OPAQUE = 0xFF;
  46. /**
  47. * 四个绿色边角对应的长度
  48. */
  49. private int ScreenRate;
  50. /**
  51. * 四个绿色边角对应的宽度
  52. */
  53. private static final int CORNER_WIDTH = 10;
  54. /**
  55. * 扫描框中的中间线的宽度
  56. */
  57. private static final int MIDDLE_LINE_WIDTH = 6;
  58. /**
  59. * 扫描框中的中间线的与扫描框左右的间隙
  60. */
  61. private static final int MIDDLE_LINE_PADDING = 5;
  62. /**
  63. * 中间那条线每次刷新移动的距离
  64. */
  65. private static final int SPEEN_DISTANCE = 5;
  66. /**
  67. * 手机的屏幕密度
  68. */
  69. private static float density;
  70. /**
  71. * 字体大小
  72. */
  73. private static final int TEXT_SIZE = 16;
  74. /**
  75. * 字体距离扫描框下面的距离
  76. */
  77. private static final int TEXT_PADDING_TOP = 30;
  78. /**
  79. * 画笔对象的引用
  80. */
  81. private Paint paint;
  82. /**
  83. * 中间滑动线的最顶端位置
  84. */
  85. private int slideTop;
  86. /**
  87. * 中间滑动线的最底端位置
  88. */
  89. private int slideBottom;
  90. /**
  91. * 将扫描的二维码拍下来,这里没有这个功能,暂时不考虑
  92. */
  93. private Bitmap resultBitmap;
  94. private final int maskColor;
  95. private final int resultColor;
  96. private final int resultPointColor;
  97. private Collection<ResultPoint> possibleResultPoints;
  98. private Collection<ResultPoint> lastPossibleResultPoints;
  99. boolean isFirst;
  100. public ViewfinderView(Context context, AttributeSet attrs) {
  101. super(context, attrs);
  102. density = context.getResources().getDisplayMetrics().density;
  103. //将像素转换成dp
  104. ScreenRate = (int)(20 * density);
  105. paint = new Paint();
  106. Resources resources = getResources();
  107. maskColor = resources.getColor(R.color.viewfinder_mask);
  108. resultColor = resources.getColor(R.color.result_view);
  109. resultPointColor = resources.getColor(R.color.possible_result_points);
  110. possibleResultPoints = new HashSet<ResultPoint>(5);
  111. }
  112. @Override
  113. public void onDraw(Canvas canvas) {
  114. //中间的扫描框,你要修改扫描框的大小,去CameraManager里面修改
  115. Rect frame = CameraManager.get().getFramingRect();
  116. if (frame == null) {
  117. return;
  118. }
  119. //初始化中间线滑动的最上边和最下边
  120. if(!isFirst){
  121. isFirst = true;
  122. slideTop = frame.top;
  123. slideBottom = frame.bottom;
  124. }
  125. //获取屏幕的宽和高
  126. int width = canvas.getWidth();
  127. int height = canvas.getHeight();
  128. paint.setColor(resultBitmap != null ? resultColor : maskColor);
  129. //画出扫描框外面的阴影部分,共四个部分,扫描框的上面到屏幕上面,扫描框的下面到屏幕下面
  130. //扫描框的左边面到屏幕左边,扫描框的右边到屏幕右边
  131. canvas.drawRect(0, 0, width, frame.top, paint);
  132. canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
  133. canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1,
  134. paint);
  135. canvas.drawRect(0, frame.bottom + 1, width, height, paint);
  136. if (resultBitmap != null) {
  137. // Draw the opaque result bitmap over the scanning rectangle
  138. paint.setAlpha(OPAQUE);
  139. canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);
  140. } else {
  141. //画扫描框边上的角,总共8个部分
  142. paint.setColor(Color.GREEN);
  143. canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate,
  144. frame.top + CORNER_WIDTH, paint);
  145. canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top
  146. + ScreenRate, paint);
  147. canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right,
  148. frame.top + CORNER_WIDTH, paint);
  149. canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top
  150. + ScreenRate, paint);
  151. canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left
  152. + ScreenRate, frame.bottom, paint);
  153. canvas.drawRect(frame.left, frame.bottom - ScreenRate,
  154. frame.left + CORNER_WIDTH, frame.bottom, paint);
  155. canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH,
  156. frame.right, frame.bottom, paint);
  157. canvas.drawRect(frame
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值