使用Zxing实现Android二维码扫描
2015-09-21 21:24
519 查看
现在市场上的很多Android应用具有二维码扫描的功能,本文将介绍如何将Zxing集成到自己的项目中实现二维码扫描的功能项目中使用的zxing版本是3.2.0应该是最新的,这里附上github上的zxing项目的链接(百度太让人无语了,搜zxing第一页搜索结果都没有这个)
https://github.com/zxing/zxing
Windows下载下来解压压缩包之后目录结构是这样的,如下图所示
关于各个目录功能的介绍都在README文档中,图中选中的三个目录是对我们有用的(实现二维码扫描而言),新建一个Android工程,将zxing android目录下的src中的源码拷到工程中,将android-core目录下的CameraConfigurationUtils.java类拷到工程中的camera目录下,将android目录下的res资源文件拷到或者合并到我们自己工程中相应的目录下(用于国际化的那些values文件不需要),在做完这些工作后工程目录应该是下面这样
libs下的core-3.2.0.jar就是zxing中的core部分打出的jar包,在android目录下的libs中,复制到项目中的libs下就行,不需要自己打包,然后按照android目录下的AndroidManifest.xml中的示例配置自己的工程,然后build自己的工程,这个时候会提示有很多错误,需要手动去改报错的类中的包引用(其他的错误可以根据problem中的error提示去修改)
做完以上的准备工作就可以在自己的MainActivity中调用二维码扫描的类了(布局文件比较简单,只有一个按钮和一个用于显示扫描结果的文本,这里不再给出代码),示例代码如下:
最后还需要在AndroidManifest.xml文件中声明权限以及涉及到的Activity,配置声明如下:
至此就成功的使用zxing集成到自己的项目中实现二维码扫描的功能了,这里没有对二维码扫描的界面进行修改,所以看到的就是zxing项目提供的最初的扫描样式,其实除了横屏以外,其它的都挺好
其实如果只是用到二维码扫描的功能,项目中的一些类、资源以及xml中的声明配置有很多是多余的,我在另一篇博客中具体介绍了如何通过修改Zxing实现二维码的竖屏扫描,并且简要介绍了一下如果精简项目代码,有兴趣的朋友可以参考一下:对Zxing修改实现二维码竖屏扫描
点击此处下载源码
有什么问题或者心得欢迎交流!
https://github.com/zxing/zxing
Windows下载下来解压压缩包之后目录结构是这样的,如下图所示
关于各个目录功能的介绍都在README文档中,图中选中的三个目录是对我们有用的(实现二维码扫描而言),新建一个Android工程,将zxing android目录下的src中的源码拷到工程中,将android-core目录下的CameraConfigurationUtils.java类拷到工程中的camera目录下,将android目录下的res资源文件拷到或者合并到我们自己工程中相应的目录下(用于国际化的那些values文件不需要),在做完这些工作后工程目录应该是下面这样
libs下的core-3.2.0.jar就是zxing中的core部分打出的jar包,在android目录下的libs中,复制到项目中的libs下就行,不需要自己打包,然后按照android目录下的AndroidManifest.xml中的示例配置自己的工程,然后build自己的工程,这个时候会提示有很多错误,需要手动去改报错的类中的包引用(其他的错误可以根据problem中的error提示去修改)
做完以上的准备工作就可以在自己的MainActivity中调用二维码扫描的类了(布局文件比较简单,只有一个按钮和一个用于显示扫描结果的文本,这里不再给出代码),示例代码如下:
public class MainActivity extends Activity { public static final int SCAN_CODE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.scan_button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, CaptureActivity.class); startActivityForResult(intent, SCAN_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case SCAN_CODE: TextView scanResult = (TextView) findViewById(R.id.scan_result); if (resultCode == RESULT_OK) { String result = data.getStringExtra("scan_result"); scanResult.setText(result); } else if (resultCode == RESULT_CANCELED) { scanResult.setText("没有扫描出结果"); } break; default: break; } } }代码中可以看出我们是使用的startActivityForResult方法开启的扫描类CaptureActivity,接下来我们需要修改CaptureActivity中处理扫描结果的方法(删掉了Zxing用于自己处理扫描结果的方法,这里只作为示例使用),示例代码如下:
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) { // playBeep(); inactivityTimer.onActivity(); lastResult = rawResult; if(rawResult!=null&&!rawResult.getText().equalsIgnoreCase("")){ Intent intent =new Intent(); intent.putExtra("scan_result", rawResult.getText()); setResult(RESULT_OK, intent); }else{ setResult(RESULT_CANCELED); } CaptureActivity.this.finish(); }
最后还需要在AndroidManifest.xml文件中声明权限以及涉及到的Activity,配置声明如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ricky.qrcodesanner" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.front" android:required="false" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> <uses-feature android:name="android.hardware.camera.flash" android:required="false" /> <uses-feature android:name="android.hardware.screen.landscape" /> <uses-feature android:name="android.hardware.wifi" android:required="false" /> <uses-feature android:name="android.hardware.touchscreen" /> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.CaptureActivity" android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden" android:screenOrientation="landscape" android:stateNotNeeded="true" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" > <intent-filter> <action android:name="com.google.zxing.client.android.SCAN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="zxing.appspot.com" android:path="/scan" android:scheme="http" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="www.google.com" android:path="/m/products/scan" android:scheme="http" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="www.google.co.uk" android:path="/m/products/scan" android:scheme="http" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="scan" android:path="/" android:scheme="zxing" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.PreferencesActivity" android:label="@string/preferences_name" android:stateNotNeeded="true" > </activity> <activity android:name="com.google.zxing.client.android.encode.EncodeActivity" android:stateNotNeeded="true" > <intent-filter> <action android:name="com.google.zxing.client.android.ENCODE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/x-vcard" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.book.SearchBookContentsActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/sbc_name" android:screenOrientation="landscape" android:stateNotNeeded="true" > <intent-filter> <action android:name="com.google.zxing.client.android.SEARCH_BOOK_CONTENTS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.share.ShareActivity" android:screenOrientation="user" android:stateNotNeeded="true" android:theme="@android:style/Theme.Light" > <intent-filter> <action android:name="com.google.zxing.client.android.SHARE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.history.HistoryActivity" android:label="@string/history_title" android:stateNotNeeded="true" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.share.BookmarkPickerActivity" android:label="@string/bookmark_picker_name" android:stateNotNeeded="true" > <intent-filter> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.share.AppPickerActivity" android:configChanges="orientation" android:label="@string/app_picker_name" android:stateNotNeeded="true" > <intent-filter> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.google.zxing.client.android.HelpActivity" android:screenOrientation="user" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
至此就成功的使用zxing集成到自己的项目中实现二维码扫描的功能了,这里没有对二维码扫描的界面进行修改,所以看到的就是zxing项目提供的最初的扫描样式,其实除了横屏以外,其它的都挺好
其实如果只是用到二维码扫描的功能,项目中的一些类、资源以及xml中的声明配置有很多是多余的,我在另一篇博客中具体介绍了如何通过修改Zxing实现二维码的竖屏扫描,并且简要介绍了一下如果精简项目代码,有兴趣的朋友可以参考一下:对Zxing修改实现二维码竖屏扫描
点击此处下载源码
有什么问题或者心得欢迎交流!
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories