Android实战——Zxing实现二维码扫描

来源:互联网 发布:电脑软件打不开无响应 编辑:程序博客网 时间:2024/06/01 15:27

Zxing实现二维码扫描


前言

本篇文章从初学者的角度出发,从一个不知道对二维码扫描怎么下手的工作者,需要一个简单的扫描功能的话,可以阅读该篇文章。作为Google开源框架Zxing,里面的文件很大,这里主要讲的是精简ZXing项目后只保留扫描功能的代码,可以缩小项目的大小,对于只要扫描功能的项目已经够用了。扫描后的结果,只要通过WebView百度一下就出来了。简单的说,可以把Zxing这个二维码扫描功能当做一个第三方服务来使用,本篇文章分为两部分,Zxing的集成和Zxing的使用


事先说明

由于二维码需要相机权限,为了适配安卓6.0新权限系统,需要我们手动申请权限,可参考博客点击打开链接

欢迎关注个人CSDN博客:Hensen_的博客:http://blog.csdn.net/qq_30379689


第一部分:Zxing的集成


步骤一:下载我们所需要的Zxing精简版,点击这里下载


步骤二:复制到项目中,解压下载的包到ZXingProj/src/com/dtr目录下,复制这个zxing文件夹到我们的项目中,这个时候你会看到有几个红线错误:



接着我们一个个来修改这些红色错误,主要错误包括:导入的R包不是本项目的,存在R.raw和R.id和R.layout的资源找不到。首先我们把该放进去的资源先放进去,复制我们libs中的zxing.jar包到项目中,记得右键AddAsLibrary



复制下载的res的layout文件、res的values的ids文件、raw文件、res的drawable-xhdpi文件到我们项目的对应位置

          


接着就进入红线文件一个个导入,记得删除原先的R包,换成自己项目的R包



到这里我的项目所有红线已经没了,不知道你们呢?还有Result要导入的是带有zxing的包



第二部分:Zxing的使用


步骤一:在manifests中声明权限和Activity

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <uses-permission android:name="android.permission.CAMERA" />  
  2. <uses-permission android:name="android.permission.FLASHLIGHT" />  
  3.   
  4. <uses-feature android:name="android.hardware.camera" />  
  5. <uses-feature android:name="android.hardware.camera.autofocus" />  
  6.   
  7. <uses-permission android:name="android.permission.VIBRATE" />  
  8. <uses-permission android:name="android.permission.WAKE_LOCK" />  
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <activity  
  2.     android:name=".zxing.activity.CaptureActivity"  
  3.     android:screenOrientation="portrait"  
  4.     android:theme="@android:style/Theme.Black.NoTitleBar" />  
  5. <activity  
  6.     android:name=".zxing.activity.ResultActivity"  
  7.     android:screenOrientation="portrait"  
  8.     android:theme="@android:style/Theme.Black.NoTitleBar" />  


步骤二:在代码中启动我们的二维码扫描页面

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Button button = (Button) findViewById(R.id.button);  
  2. button.setOnClickListener(new View.OnClickListener() {  
  3.     @Override  
  4.     public void onClick(View v) {  
  5.         startActivity(new Intent(OpenZxingActivity.this, CaptureActivity.class));  
  6.     }  
  7. });  


步骤三:真机效果图

CaptureActivity:


ResultActivity:


步骤四:如果你想对Capture页面的界面进行修改可以制作一张图片替换drawable里面图片,这里我们只介绍对读取结果的介绍,我们打开ResultActivity文件:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class ResultActivity extends Activity {  
  2.   
  3.     private ImageView mResultImage;  
  4.     private TextView mResultText;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_result);  
  10.   
  11.         Bundle extras = getIntent().getExtras();  
  12.   
  13.         mResultImage = (ImageView) findViewById(R.id.result_image);  
  14.         mResultText = (TextView) findViewById(R.id.result_text);  
  15.   
  16.         if (null != extras) {  
  17.             int width = extras.getInt("width");  
  18.             int height = extras.getInt("height");  
  19.   
  20.             LayoutParams lps = new LayoutParams(width, height);  
  21.             lps.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources().getDisplayMetrics());  
  22.             lps.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());  
  23.             lps.rightMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());  
  24.               
  25.             mResultImage.setLayoutParams(lps);  
  26.   
  27.             String result = extras.getString("result");  
  28.             mResultText.setText(result);  
  29.   
  30.             Bitmap barcode = null;  
  31.             byte[] compressedBitmap = extras.getByteArray(DecodeThread.BARCODE_BITMAP);  
  32.             if (compressedBitmap != null) {  
  33.                 barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length, null);  
  34.                 // Mutable copy:  
  35.                 barcode = barcode.copy(Bitmap.Config.RGB_565, true);  
  36.             }  
  37.   
  38.             mResultImage.setImageBitmap(barcode);  
  39.         }  
  40.     }  
  41. }  


我们大致可以明白,从CaptureActivity中传来一个Bundle的参数,并解析Bundle的宽和高,将其交给ImageView的参数,做为宽高,同时增加Margin属性,同时解析一个Byte数组,其中就是图片的文件的字节码,其中解析的result,则是我们需要的二维码结果:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. String result = extras.getString("result");  
  2. mResultText.setText(result);  
0 0
原创粉丝点击