Android下利用zxing类库实现扫一扫
2015-07-27 21:01
591 查看
程序源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/zxingdemo.zip
zxing,一款无比强大的条码解析类库,下面讲解一下如何利用zxing类库实现扫一扫功能,先放上一张效果图:
主要代码如下:
1.在onPreviewFrame方法里获取预览图像,然后传递给DecodeHandler去解析
2. 在decode方法里调用zxing类库对图像进行解析
zxing,一款无比强大的条码解析类库,下面讲解一下如何利用zxing类库实现扫一扫功能,先放上一张效果图:
主要代码如下:
1.在onPreviewFrame方法里获取预览图像,然后传递给DecodeHandler去解析
public void onPreviewFrame(byte[] data, Camera camera) { Point cameraResolution = configManager.getCameraResolution(); if (!useOneShotPreviewCallback) { camera.setPreviewCallback(null); } if (previewHandler != null) { Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,cameraResolution.y, data); message.sendToTarget(); previewHandler = null; } else { Log.d(TAG, "Got preview callback, but no handler for it"); } }
2. 在decode方法里调用zxing类库对图像进行解析
private void decode(byte[] data, int width, int height) { long start = System.currentTimeMillis(); Result rawResult = null; byte[] rotatedData = new byte[data.length]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width]; } int tmp = width; // Here we are swapping, that's the difference to #11 width = height; height = tmp; PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap); } catch (ReaderException re) { // continue } finally { multiFormatReader.reset(); } if (rawResult != null) { long end = System.currentTimeMillis(); Log.d(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString()); Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult); Log.d(TAG, "Sending decode succeeded message..."); message.sendToTarget(); } else { Message message = Message.obtain(activity.getHandler(), R.id.decode_failed); message.sendToTarget(); } }
相关文章推荐
- android坐标
- Android studio项目结构目录详解
- android----通信录操作
- android 编程下 Touch 事件的分发和消费机制
- Android studio中使用asmack的jar包的前期准备工作
- 在Android中如何侦听Service内部的状态
- android 错误提示---解决办法
- Android Service Bind with Activity
- Android之——手机黑名单的实现
- Android中的5种数据存储方式之——文件存储数据
- 《深入了解Android:Wi-Fi、NFC和GPS音量》勘误表
- android的事物
- Android的数据存储方式
- 说一下android的sqlite数据库BaseSQLiteOpenHelper
- Android的top命令
- Android中通过GPS或NetWork获取当前位置的经纬度
- Android性能优化技巧
- Android 性能优化之使用MAT分析内存泄露问题
- 极简音乐播放器(一)
- 【攻克Android(26)】Async Task 异步任务