二维码自动扫描完整版
来源:互联网 发布:淘宝怎么修改分类 编辑:程序博客网 时间:2024/05/21 09:37
trebleZ的博客
比你努力一点点的人,其实已经甩你很远了。
android中Zxing实现二维码功能的快速集成以及扫描界面的定制
版权声明:本文为博主原创文章,未经博主允许不得转载。
Zxing二维码库是相当丰富。但是我们往往只需要里面的扫码识别以及生成二维码的功能就可以了,所以这次用到了已经抽离出核销代码的框架包
compile ‘com.journeyapps:zxing-android-embedded:3.3.0’,来快速集成开发。比较简单,后面还会有扫面界面的定制,仿微信二维码扫一扫功能。上几个效果图:
扫描中
扫描结果:
接下来我们来实现他。
一、集成二维码的简单扫一扫以及生成二维码的功能(录屏用的是genymotion,录的有点奇怪啊)
1、Zxing二维码的环境。我们只要compile ‘com.journeyapps:zxing-android-embedded:3.3.0’这个后面就不需要再集成了。
2、xml文件中
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开闪关灯" android:id="@+id/btn_switch" android:layout_alignTop="@+id/btn_hint1" /> <!-- 这个控件就是扫描的窗口了 --> <com.journeyapps.barcodescanner.DecoratedBarcodeView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dbv_custom" app:zxing_framing_rect_width="200dp" app:zxing_framing_rect_height="200dp" app:zxing_preview_scaling_strategy="fitXY" app:zxing_use_texture_view="true" > </com.journeyapps.barcodescanner.DecoratedBarcodeView></LinearLayout>
3、CustomScanAct.java
package com.coofond.zxingdemo;import android.app.Activity;import android.content.pm.PackageManager;import android.os.Bundle;import android.os.PersistableBundle;import android.view.KeyEvent;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.journeyapps.barcodescanner.CaptureManager;import com.journeyapps.barcodescanner.DecoratedBarcodeView;/** * Created by IBM on 2016/10/13. */public class CustomScanAct extends Activity implements DecoratedBarcodeView.TorchListener { // 实现相关接口 // 添加一个按钮用来控制闪光灯,同时添加两个按钮表示其他功能,先用Toast表示 Button swichLight; DecoratedBarcodeView mDBV; private CaptureManager captureManager; private boolean isLightOn = false; @Override protected void onPause() { super.onPause(); captureManager.onPause(); } @Override protected void onResume() { super.onResume(); captureManager.onResume(); } @Override protected void onDestroy() { super.onDestroy(); captureManager.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); captureManager.onSaveInstanceState(outState); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act_customscan); swichLight = (Button) findViewById(R.id.btn_switch); mDBV= (DecoratedBarcodeView) findViewById(R.id.dbv_custom); mDBV.setTorchListener(this); // 如果没有闪光灯功能,就去掉相关按钮 if (!hasFlash()) { swichLight.setVisibility(View.GONE); } //重要代码,初始化捕获 captureManager = new CaptureManager(this, mDBV); captureManager.initializeFromIntent(getIntent(), savedInstanceState); captureManager.decode(); //选择闪关灯 swichLight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isLightOn) { mDBV.setTorchOff(); } else { mDBV.setTorchOn(); } } }); } // torch 手电筒 @Override public void onTorchOn() { Toast.makeText(this, "torch on", Toast.LENGTH_LONG).show(); isLightOn = true; } @Override public void onTorchOff() { Toast.makeText(this, "torch off", Toast.LENGTH_LONG).show(); isLightOn = false; } // 判断是否有闪光灯功能 private boolean hasFlash() { return getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); }}
4、MainActivity.java
package com.coofond.zxingdemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import com.google.zxing.integration.android.IntentIntegrator;import com.google.zxing.integration.android.IntentResult;public class MainActivity extends Activity { private Button btnClick; private TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initEvent(); } private void initView() { btnClick = (Button) findViewById(R.id.btn_click); tvResult = (TextView) findViewById(R.id.tv_result); } private void initEvent() { btnClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //假如你要用的是fragment进行界面的跳转 //IntentIntegrator intentIntegrator = IntentIntegrator.forSupportFragment(ShopFragment.this).setCaptureActivity(CustomScanAct.class); IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this); intentIntegrator .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES) .setPrompt("将二维码/条码放入框内,即可自动扫描")//写那句提示的话 .setOrientationLocked(false)//扫描方向固定 .setCaptureActivity(CustomScanAct.class) // 设置自定义的activity是CustomActivity .initiateScan(); // 初始化扫描 } }); } //获取扫描的结果 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (intentResult != null) { if (intentResult.getContents() == null) { } else { // ScanResult 为获取到的字符串 String ScanResult = intentResult.getContents(); tvResult.setText(ScanResult); } } else { super.onActivityResult(requestCode, resultCode, data); } }}
这样就集成了二维码的基本功能了。显示的是一个闪动的激光条。。。
二、仿造微信扫一扫,重新定制扫面界面,看下要实现的效果图
1、主要是这一句 app:zxing_scanner_layout=”@layout/barcode_scanner”指向你自定义的界面
<!-- 这个控件就是扫描的窗口了 --> <com.journeyapps.barcodescanner.DecoratedBarcodeView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dbv_custom" app:zxing_framing_rect_width="200dp" app:zxing_framing_rect_height="200dp" app:zxing_preview_scaling_strategy="fitXY" app:zxing_use_texture_view="true" app:zxing_scanner_layout="@layout/barcode_scanner" > </com.journeyapps.barcodescanner.DecoratedBarcodeView>
2、那么我们看下如何定制这个窗口barcode_scanner.xml看一下
<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <com.journeyapps.barcodescanner.BarcodeView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/zxing_barcode_surface" app:zxing_framing_rect_width="250dp" app:zxing_framing_rect_height="250dp"> </com.journeyapps.barcodescanner.BarcodeView> <com.coofond.zxingdemo.CustomViewfinderView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/zxing_viewfinder_view" app:zxing_possible_result_points="@color/zxing_custom_possible_result_points" app:zxing_result_view="@color/zxing_custom_result_view" app:zxing_viewfinder_laser="#FFFFFF" app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask"/> <TextView android:id="@+id/zxing_status_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:background="@color/zxing_transparent" android:text="@string/zxing_msg_default_status" android:textColor="@color/zxing_status_text"/></merge>
3、主要是要自定义ViewfinderView,重新绘制你的扫描界面。看下CustomViewfinderView.java中的文件
package com.coofond.zxingdemo;import android.content.Context;import android.graphics.Canvas;import android.graphics.LinearGradient;import android.graphics.Rect;import android.graphics.Shader;import android.util.AttributeSet;import com.google.zxing.ResultPoint;import com.journeyapps.barcodescanner.ViewfinderView;import java.util.ArrayList;import java.util.List;/** * 自定义zxing二维码扫描界面 * Created by IBM on 2016/10/20. */public class CustomViewfinderView extends ViewfinderView { public int laserLinePosition=0; public float[] position=new float[]{0f,0.5f,1f}; public int[] colors=new int[]{0x00ffffff,0xffffffff,0x00ffffff}; public LinearGradient linearGradient ; public CustomViewfinderView(Context context, AttributeSet attrs) { super(context, attrs); } /** * 重写draw方法绘制自己的扫描框 * @param canvas */ @Override public void onDraw(Canvas canvas) { refreshSizes(); if (framingRect == null || previewFramingRect == null) { return; } Rect frame = framingRect; Rect previewFrame = previewFramingRect; int width = canvas.getWidth(); int height = canvas.getHeight(); //绘制4个角 paint.setColor(0xFFFFFFFF);//定义画笔的颜色 canvas.drawRect(frame.left, frame.top, frame.left+70, frame.top+10, paint); canvas.drawRect(frame.left, frame.top, frame.left + 10, frame.top + 70, paint); canvas.drawRect(frame.right-70, frame.top, frame.right, frame.top+10, paint); canvas.drawRect(frame.right-10, frame.top, frame.right, frame.top+70, paint); canvas.drawRect(frame.left, frame.bottom-10, frame.left+70, frame.bottom, paint); canvas.drawRect(frame.left, frame.bottom-70, frame.left+10, frame.bottom, paint); canvas.drawRect(frame.right-70, frame.bottom-10, frame.right, frame.bottom, paint); canvas.drawRect(frame.right-10, frame.bottom-70, frame.right, frame.bottom, paint); // Draw the exterior (i.e. outside the framing rect) darkened paint.setColor(resultBitmap != null ? resultColor : maskColor); canvas.drawRect(0, 0, width, frame.top, paint); canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); canvas.drawRect(0, frame.bottom + 1, width, height, paint); if (resultBitmap != null) { // Draw the opaque result bitmap over the scanning rectangle paint.setAlpha(CURRENT_POINT_OPACITY); canvas.drawBitmap(resultBitmap, null, frame, paint); } else { // paint.setAlpha(SCANNER_ALPHA[scannerAlpha]); // scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length; int middle = frame.height() / 2 + frame.top; laserLinePosition=laserLinePosition+5; if(laserLinePosition>frame.height()) { laserLinePosition=0; } linearGradient= new LinearGradient(frame.left + 1, frame.top+laserLinePosition , frame.right -1 , frame.top +10+laserLinePosition, colors, position, Shader.TileMode.CLAMP); // Draw a red "laser scanner" line through the middle to show decoding is active // paint.setColor(laserColor); paint.setShader(linearGradient); //绘制扫描线 canvas.drawRect(frame.left + 1, frame.top+laserLinePosition , frame.right -1 , frame.top +10+laserLinePosition, paint); paint.setShader(null); float scaleX = frame.width() / (float) previewFrame.width(); float scaleY = frame.height() / (float) previewFrame.height(); List<ResultPoint> currentPossible = possibleResultPoints; List<ResultPoint> currentLast = lastPossibleResultPoints; int frameLeft = frame.left; int frameTop = frame.top; if (currentPossible.isEmpty()) { lastPossibleResultPoints = null; } else { possibleResultPoints = new ArrayList<>(5); lastPossibleResultPoints = currentPossible; paint.setAlpha(CURRENT_POINT_OPACITY); paint.setColor(resultPointColor); for (ResultPoint point : currentPossible) { canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), frameTop + (int) (point.getY() * scaleY), POINT_SIZE, paint); } } if (currentLast != null) { paint.setAlpha(CURRENT_POINT_OPACITY / 2); paint.setColor(resultPointColor); float radius = POINT_SIZE / 2.0f; for (ResultPoint point : currentLast) { canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), frameTop + (int) (point.getY() * scaleY), radius, paint); } } postInvalidateDelayed(16, frame.left , frame.top , frame.right , frame.bottom); // postInvalidate(); } }}
那么就完全结束了。是懂非懂的感觉。只是停留在集成和使用的阶段。
最后该demo的下载地址http://download.csdn.net/detail/z_zt_t/9659802
- 顶
- 7
- 踩
- 0
- 上一篇将Android Studio中的项目上传到GitHub
- 下一篇自定义Spinner样式以及实现hint效果
- • Android studio集成Zxing实现扫一扫
- • 在Android Studio中集成Zxing
- • Android studio 使用Zxing二维码扫描,过程及问题汇总
- • Android实战——Zxing实现二维码扫描
- • 快速集成二维码扫描库-zxing的源码封装
- • Android Studio 使用zxing二维码处理
- • 【android】android studio下二维码类库zxing精简包的配置及第三方类库的导入
- • Android Studio 扫描二维码并解析跳转
- • Android studio使用zxing扫一扫
- • Android 基于google Zxing实现对手机中的二维码进行扫描
- 猜你在找
- C语言及程序设计(讲师:贺利坚)
- Python爬虫工程师培养课程全套(讲师:韦玮)
- Python全栈开发入门与实战课(讲师:李杰)
- 2017软考网络规划设计师视频套餐(讲师:任铄)
- 2017软考软件设计师视频套餐(讲师:任铄)
- 2017软考-信息系统项目管理师视频套餐(讲师:任铄)
- 软考(高级)项目经理实战营(讲师:张传波)
- 微信公众平台开发套餐(讲师:刘运强)
- 深度学习原理+实战+算法+主流框架套餐(讲师:唐宇迪)
- 2017系统集成项目管理工程师通关套餐(讲师:徐朋)
- 个人资料
- 访问:63338次
- 积分:1496
- 等级: 积分:1496
- 排名:千里之外
- 原创:75篇
- 转载:14篇
- 译文:0篇
- 评论:52条
z_zT_T
- 文章搜索
- 文章分类
- android路(49)
- 数据库分析(1)
- 算法与数据结构(2)
- Java基础(5)
- android基础(19)
- 文章存档
- 2017年07月(2)
- 2017年06月(1)
- 2017年05月(3)
- 2017年04月(8)
- 2017年03月(8)
- 2017年02月(3)
- 2017年01月(4)
- 2016年12月(10)
- 2016年11月(3)
- 2016年10月(5)
- 2016年09月(1)
- 2016年08月(6)
- 2016年07月(15)
- 2016年06月(3)
- 2016年05月(3)
- 2016年04月(1)
- 2016年03月(8)
- 2016年02月(6)
- 阅读排行
- android中开启子线程(6382)
- android中Zxing实现二维码功能的快速集成以及扫描界面的定制(5158)
- Android Studio使用总结(4498)
- 自定义Spinner样式以及实现hint效果(3779)
- com/android/dx/command/dexer/Main : Unsupported major.minor version 52.0(3167)
- 使用Android-PickerView实现地址选择器时间选择器(2104)
- 使用HorizontalScrollView简单实现柱状图(1965)
- 使用ViewPager实现问卷答题效果(1963)
- 使用ViewPager实现帖子列表(1925)
- JAVA多线程之Thread&&Runnable(1661)
- 评论排行
- android中Zxing实现二维码功能的快速集成以及扫描界面的定制(14)
- Android Studio使用总结(6)
- EditText设置DrawableRight,DrawableLeft,DrawableTop... 点击事件(4)
- 使用ViewPager实现帖子列表(4)
- 抓住2016年的最后时光,做个总结(3)
- com/android/dx/command/dexer/Main : Unsupported major.minor version 52.0(3)
- 自定义Spinner样式以及实现hint效果(2)
- 使用Android-PickerView修改item的字体大小(坑)(2)
- 使用ViewPager实现问卷答题效果(2)
- ExpandableListView实现侧页导航栏目(2)
- 推荐文章
- * CSDN日报20170714——《从创业到再就业,浅述对程序员职业生涯的看法》
- * Android 逆向 | 锁屏密码算法解析以及破解方案
- * 从单一WAR到多活,记述一个创业公司的架构演变
- * 我的AI转型之路与AI之我见(非985211的奋斗路程与视角)
- * AI大行其道,你准备好了吗?—谨送给徘徊于转行AI的程序员
- * AI转型中的思考和洞见
- 最新评论
- android中Zxing实现二维码功能的快速集成以及扫描界面的定制
weixin_37670883:Caused by: android.view.InflateException: Binary X...
- android中Zxing实现二维码功能的快速集成以及扫描界面的定制
chuabugi:thank you,搞出来了
- EditText设置DrawableRight,DrawableLeft,DrawableTop... 点击事件
z_zT_T:@aiynmimi:这个怪我了。我在genymotion中测试的,默认不弹出软键盘。。。。so,这是...
- android中Zxing实现二维码功能的快速集成以及扫描界面的定制
qq_33758131:怎么提高容错率啊
- EditText设置DrawableRight,DrawableLeft,DrawableTop... 点击事件
aiynmimi:您好,请问一下,点击之后,输入框获取焦点,弹出软键盘,之后软键盘消失,才会执行点击逻辑是怎么回事?
- 抓住2016年的最后时光,做个总结
qq_38631103:坚定心中目标。
- android post传递数组类型数据给后台php
z_zT_T:论后台交互使用json的重要性。发现此方法只是传递过去一个空串,还是需要拼接,简直无底洞。。。放弃了...
- android中Zxing实现二维码功能的快速集成以及扫描界面的定制
imagine186:请教,怎么调整扫描框的位置?看了下源码,好像默认就是在中间显示,只能调整大小,不知道应该怎么做。
- 自定义Spinner样式以及实现hint效果
qq_37094189:thank you!
- android中Zxing实现二维码功能的快速集成以及扫描界面的定制
z_zT_T:@zaj474439669:只是基于zxing的基本应用,如果发现问题可以一起讨论,共同踩坑
- 二维码自动扫描完整版
- 扫描二维码自动登录实现原理
- Android扫描wifi二维码自动连接wifi
- 微信扫描二维码,实现自动登录
- 微信扫描二维码实现自动跳转
- Android扫描wifi二维码自动连接wifi
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 二维码扫描
- 扫描二维码
- 二维码扫描
- 二维码扫描
- React component 生命周期
- 计算数字k在0到n中的出现的次数,k可能是0~9的一个值
- 产品汪的日常
- Postgresql UNION & UNION ALL 用法以及应用场景.md
- 编写代码时,每打一个字符就会弹出“Computing additional info”.Could not initialize class……
- 二维码自动扫描完整版
- Android中mpchartlib折线图的详细属性以及实现(双曲线)
- PHP7源码安装Memcached和Memcache拓展
- (译) 在AngularJS中使用的表单验证功能
- mac 更改文件权限
- 题目1112:拦截导弹
- 寻找一个字符串中第一个只出现两次的字符
- @Autowired的使用:推荐对构造函数进行注释
- 原生js获取鼠标坐标方法全面讲解:clientX/Y,pageX/Y,offsetX/Y,layerX/Y,screenX/Y