标签:android
本文使用 zxing-android-embedded 这个开源项目实现 二维码扫描/生成 功能;
开发工具:android studio
1、如何将zxing-android-embedded添加到我们的项目中
1.1 添加arr依赖包
将以下代码添加到build.gradle文件中。
repositories { mavenCentral() maven { url "https://dl.bintray.com/journeyapps/maven" } } dependencies { // Supports Android 4.0.3 and later (API level 15) compile ‘com.journeyapps:zxing-android-embedded:2.3.0@aar‘ // Supports Android 2.1 and later (API level 7), but not optimal for later Android versions. // If you only plan on supporting Android 4.0.3 and up, you don‘t need to include this. compile ‘com.journeyapps:zxing-android-legacy:2.3.0@aar‘ // Convenience library to launch the scanning Activities. // It automatically picks the best scanning library from the above two, depending on the // Android version and what is available. compile ‘com.journeyapps:zxing-android-integration:2.3.0@aar‘ // Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier. // This mostly affects encoding, but you should test if you plan to support these versions. // Older versions e.g. 2.2 may also work if you need support for older Android versions. compile ‘com.google.zxing:core:3.2.0‘ }
注意:是app目录下的build.gradle文件
1.2 通过gradle同步你的项目
点击 "sync project with gradle files",android studio 将联网下载必要的文件。
到这一步为止,我们的项目已经可以使用 zxing-android-embedded 的代码了!
2、扫描二维码
2.1 启动二维码扫描界面
在Activity中,使用默认选项启动,可使用以下代码:
new IntentIntegrator(this).initiateScan(); //‘this‘是当前的Activity
在Fragment中,使用默认选项启动,可使用以下代码:
IntentIntegrator.forSupportFragment(this).initiateScan(); //‘this‘是当前的Fragment
2.1.1 自定义选项,参考 IntentIntegrator
2.2 接收扫描结果
扫描完成,将会调用你的 onActivityResult 方法。
因此我们需要重写 onActivityResult 方法来获取扫描结果。
@Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (resultCode == Activity.RESULT_OK) { String contents = intent.getStringExtra("SCAN_RESULT"); //图像内容 String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); //图像格式 // Handle successful scan Log.v("qr-code", contents + "####" + format); tv_content.setText(contents); } else if (resultCode == Activity.RESULT_CANCELED) { // Handle cancel } }
到此步,完成了二维码扫描功能。
如果扫描这张以下二维码图片,你将在logcat中得到这条输出语句 “www.google.com####QR_CODE”
3 、生成二维码
如果你需要将一段文本转换成二维码Bitmap,可参考以下代码
private Bitmap generateQRCode(String qrCodeString){ Bitmap bmp = null; //二维码图片 QRCodeWriter writer = new QRCodeWriter(); try { BitMatrix bitMatrix = writer.encode(qrCodeString, BarcodeFormat.QR_CODE, 512, 512); //参数分别表示为: 条码文本内容,条码格式,宽,高 int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); //绘制每个像素 for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } } catch (WriterException e) { e.printStackTrace(); } return bmp; }
本文出自 “happy coding...” 博客,请务必保留此出处http://jiataozhou.blog.51cto.com/7355162/1651712
标签:android
原文地址:http://jiataozhou.blog.51cto.com/7355162/1651712