Android平台利用Zxing生成二维码与解析图片中的二维码
2015-07-28 11:26
691 查看
转载请注明http://blog.csdn.net/houkai6/article/details/47102733
1. 生成二维码
2. 解析图片中的二维码
1. 生成二维码
public final class EncodingHandler { private static final int BLACK = 0xff000000; public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
2. 解析图片中的二维码
public final class DecodeHandler { public static String decode(Bitmap bitmap) { if (bitmap == null) { return null; } int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()]; bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source)); Reader reader = new MultiFormatReader(); try { Result result = reader.decode(binaryBitmap); return result.getText(); } catch (NotFoundException e) { e.printStackTrace(); } catch (ChecksumException e) { e.printStackTrace(); } catch (FormatException e) { e.printStackTrace(); } return null; } }
相关文章推荐
- android使用ViewPager实现欢迎引导页
- android使用ViewPager实现欢迎引导页
- Android working with Volley Library
- android————AlertDialog,ProGressDialog
- android Notification
- android 应用获取系统权限
- Android:关闭软键盘自动弹出的解决方案
- android应用中去掉标题栏的方法
- android背景色改为透明色
- 查看Android应用包名package和入口activity名称
- android应用之——为自己的apk签名
- Android之——自定义复合控件的实现
- android布局参数详解
- android模拟器打不开问题总结原因大全
- Android基础_页面布局_碎片(Fragment)
- Android在加载大图片时,易出现内存溢出的情况 解决方案
- USB 默认功能修改
- Android相关资源下载
- android读取raw里数据
- android查看新闻简单案例