您的位置:首页 > 移动开发 > Android开发

野人学Android第二弹——利用Zxing实现二维码效果的步骤讲解

2015-11-18 16:00 411 查看
如今,不管是微信还是淘宝,越来越多的app会加入二维码扫描的功能,那么二维码的生成和解析怎么实现呢?我首先想到的是,这么流行的功能,一定有第三方类,果不其然,在百度之后,发现google已经将二维码的功能封装成zxing类,并且已经相当成熟。自然而然,只要将这个类添加到我的app项目中,就ok了。
思路很简单,但是现实很骨感。刚开始接触zxing的时候,无从下手,百度了很多博客,有些细节没有讲全,有些讲的含糊,我花了好几天才将zxing运行成功。现在回过头来,将这些知识细细咀嚼后,整理成通俗易懂的步骤,让刚学Android的童鞋,能快速地享受zxing带来的效果。
第一步,下载zxing代码包

本来想在官方上下载的,但是下载出现了错误,下载不了,那么我就将我这里的zxing核心包上传吧。下载地址
第二步,导入core.jar核心包



将zxing.jar核心包复制到libs文件夹下,系统会自动将zxing.jar导入到Android Private Libraries中,就这么简单。在刚开始接触zxing的时候,各种博客的说法不一致,导致我花了很多的时间来研究这个。将核心包导入到这里,是为了下一步埋下伏笔。
第三步,导入关键class文件



网络上很多的教程中,都将zxing的class类分门别类,但是在实际的开发中,二维码作为app的一个功能,未免有点多余。综合考虑,我还是将关键的代码放在一个包内,这样给app的其他功能腾出了空间。这些关键的代码主要实现的是二维码的扫描,至于二维码的生成则不包括在内。PS:关键代码可以点击这个链接
第四步,排错



导入上面的核心包和关键代码文件之后,你会发现有很多的错误。最主要的错误就是包名引用错误的问题。除了一个一个修改,别无法他法。将这些错误修改后,还存在一个问题,就是找不到相应的文件。正如上面的图片所示,这些文件需要添加到res文件夹下。
beep.ogg是一个音频文件,是当你扫描成功之后的音频效果,这个你可以自己定义。
ids.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2008 ZXing authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>

<item name="decode" type="id"/>
<item name="decode_failed" type="id"/>
<item name="decode_succeeded" type="id"/>
<item name="quit" type="id"/>
<item name="restart_preview" type="id"/>
<item name="return_scan_result" type="id"/>

</resources>


除此之外,在layout文件夹下还需要添加activity_capture.xml。
activity_capture.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical" >

<SurfaceView
android:id="@+id/capture_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<RelativeLayout
android:id="@+id/capture_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/capture_mask_top"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:background="@drawable/shadow" />

<RelativeLayout
android:id="@+id/capture_crop_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/capture_mask_top"
android:layout_centerHorizontal="true"
android:background="@drawable/qr_code_bg" >

<ImageView
android:id="@+id/capture_scan_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/scan_line" />
</RelativeLayout>

<ImageView
android:id="@+id/capture_mask_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/capture_crop_view"
android:background="@drawable/shadow" />

<ImageView
android:id="@+id/capture_mask_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@id/capture_mask_bottom"
android:layout_alignParentLeft="true"
android:layout_below="@id/capture_mask_top"
android:layout_toLeftOf="@id/capture_crop_view"
android:background="@drawable/shadow" />

<ImageView
android:id="@+id/capture_mask_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@id/capture_mask_bottom"
android:layout_alignParentRight="true"
android:layout_below="@id/capture_mask_top"
android:layout_toRightOf="@id/capture_crop_view"
android:background="@drawable/shadow" />
</RelativeLayout>

</RelativeLayout>


第五步,权限配置

二维码扫描这个功能的实现,涉及到了很多的权限。我本来想直接告诉答案的,但是想想还是算了。如果你不知道该配置哪些权限,那就先去测试下程序,看看日志中会报什么错误,然后一步步添加就可以了。
第六步,Activity中的使用

先讲一下使用的基本思路:在MainActivity中使用Intent(),将信息传递到CaptureActivity,然后用onActivityResult()接收调回的信息。

private Button bt;
private TextView tv;
private int REQUEST_CODE=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre">	</span>super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_CODE){
if(resultCode==RESULT_CANCELED)
setTitle("cancle");
else if (resultCode==RESULT_OK) {
String temp=null;
Bundle bundle=data.getExtras();
if(bundle!=null){
temp=bundle.getString("result");
tv.setText(temp);
}
}
}
}


写到这里,Zxing应该能正常运行了。如果报错的话,那么耐心地调试下就可以了。如果你觉得不够个性,想自己自定义下,那么网上有很多教程,请自行百度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: