Zxing3.x的简单使用(二)
来源:互联网 发布:药品查询真伪软件 编辑:程序博客网 时间:2024/05/22 00:23
Zxing3.x的使用
上一篇简单讲解了生成二维码,现在接着讲扫描二维码,扫描稍微复杂一些,需要自己用到SurfaceView,还好源码上来就用这个了,不用太封装,直接拿过来用即可,解析成功后用SurfaceHolder传递信息,源码实现比较复杂,调用起来不是很复杂。
cameraManager = new CameraManager(getApplication());
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
viewfinderView.setCameraManager(cameraManager);
resultView = findViewById(R.id.result_view); handler = null; lastResult = null; handler = null; lastResult = null; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); resetStatusView(); beepManager.updatePrefs(); ambientLightManager.start(cameraManager); inactivityTimer.onResume(); Intent intent = getIntent(); copyToClipboard = prefs.getBoolean(PreferencesActivity.KEY_COPY_TO_CLIPBOARD, true) && (intent == null || intent.getBooleanExtra(Intents.Scan.SAVE_HISTORY, true)); source = IntentSource.NONE; sourceUrl = null; scanFromWebPageManager = null; decodeFormats = null; characterSet = null; if (intent != null) { String action = intent.getAction(); String dataString = intent.getDataString(); if (Intents.Scan.ACTION.equals(action)) { // Scan the formats the intent requested, and return the result to the calling activity. source = IntentSource.NATIVE_APP_INTENT; decodeFormats = DecodeFormatManager.parseDecodeFormats(intent); decodeHints = DecodeHintManager.parseDecodeHints(intent); if (intent.hasExtra(Intents.Scan.WIDTH) && intent.hasExtra(Intents.Scan.HEIGHT)) { int width = intent.getIntExtra(Intents.Scan.WIDTH, 0); int height = intent.getIntExtra(Intents.Scan.HEIGHT, 0); if (width > 0 && height > 0) { cameraManager.setManualFramingRect(width, height); } } if (intent.hasExtra(Intents.Scan.CAMERA_ID)) { int cameraId = intent.getIntExtra(Intents.Scan.CAMERA_ID, -1); if (cameraId >= 0) { cameraManager.setManualCameraId(cameraId); } } String customPromptMessage = intent.getStringExtra(Intents.Scan.PROMPT_MESSAGE); if (customPromptMessage != null) { } } else if (dataString != null && dataString.contains("http://www.google") && dataString.contains("/m/products/scan")) { // Scan only products and send the result to mobile Product Search. source = IntentSource.PRODUCT_SEARCH_LINK; sourceUrl = dataString; decodeFormats = DecodeFormatManager.PRODUCT_FORMATS; } else if (isZXingURL(dataString)) { // Scan formats requested in query string (all formats if none specified). // If a return URL is specified, send the results there. Otherwise, handle it ourselves. source = IntentSource.ZXING_LINK; sourceUrl = dataString; Uri inputUri = Uri.parse(dataString); scanFromWebPageManager = new ScanFromWebPageManager(inputUri); decodeFormats = DecodeFormatManager.parseDecodeFormats(inputUri); // Allow a sub-set of the hints to be specified by the caller. decodeHints = DecodeHintManager.parseDecodeHints(inputUri); } characterSet = intent.getStringExtra(Intents.Scan.CHARACTER_SET); } SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view); SurfaceHolder surfaceHolder = surfaceView.getHolder(); if (hasSurface) { // The activity was paused but not stopped, so the surface still exists. Therefore // surfaceCreated() won't be called, so init the camera here. initCamera(surfaceHolder); } else { // Install the callback and wait for surfaceCreated() to init the camera. surfaceHolder.addCallback(this); }
核心代码就是这些,其他的也一起粘过来即可,官方写的有点乱,需要注意的是默认它会调用相机的调焦方法,让相机看东西更清楚,所以如果你正确的使用了该方法,仍然需要花费一点时间才能解码,那是正常的,只能是手机硬件的摄像头有待性能不行,跟代码的无关,android平台的手机,什么品质的都有,尤其是一些“垃圾”手机,摄像头做的烂的一X,扫码扫不出来正常。
Zxing3.x版本修复了一些2.x版本的bug,比如横竖屏的问题,使用3.x版本直接正常使用即可,只是demo不是太友好而已。
0 0
- Zxing3.x的简单使用(二)
- Zxing3.x的简单使用(一)
- vitamio5.x的简单使用(二)
- Quartz的简单使用,基于1.X(二)【类的详细解释】
- quartz的简单使用-基于2.X(二)【与框架的整合】
- vitamio5.x的简单使用
- vitamio5.x的简单使用
- Sikuli-X简单的使用
- vitamio5.x的简单使用
- VideoView的简单使用(二)
- ViewPager 的简单使用(二)
- Gson的简单使用(二)
- greenDAO的简单使用(二)
- Android MediaPlayer的简单使用(二)
- Quartz(二)简单的使用
- iOS cocos2d-X之CCUserDefault的使用(二)
- Vert.x-Web的讲解和使用(二)
- ubuntu 的使用(二)—— 关闭 X server
- 串口调试助手(SecureCRT)-使用小技巧
- iOS 开发证书的讲解
- 剑指offer:数组中只出现一次的数字
- iOS 自定义过场动画
- Android Studio使用shape出现的问题
- Zxing3.x的简单使用(二)
- @Autowired 与@Resource的区别 及@Autowired 在成员变量和set方法的区别
- 那些年,追过的开源软件和技术
- Redis实战-Jedis使用指南
- WordCount 的 Java 和 Scala 实现
- new、delete、malloc、free关系
- jquery对象访问 介绍及如何使用
- 福斯数据服务平台产品白皮书
- andriod no changes to deploy