zxing生成二维码
2016-06-30 20:22
363 查看
zxing生成二维码
public class QrCodeUtil { public static Bitmap creaQrCode(String content, int qrWidth, int qrHeight) { return createQrCode(content, "utf-8", Color.BLACK, qrWidth, qrHeight, ErrorCorrectionLevel.H, null); } public static Bitmap createQrCode(String content, String characterSet, int qrColor, int qrWidth, int qrHeight , ErrorCorrectionLevel ecl, Bitmap logo) { if (content == null) { return null; } if (characterSet == null) { characterSet = "utf-8"; } if (ecl == null) { ecl = ErrorCorrectionLevel.H; } Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, characterSet); hints.put(EncodeHintType.ERROR_CORRECTION, ecl); hints.put(EncodeHintType.MARGIN, 0); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, hints); int[] piexs = new int[qrWidth * qrHeight]; for (int y = 0; y < qrHeight; y++) { for (int x = 0; x < qrWidth; x++) { if (bitMatrix.get(x, y)) { piexs[y * qrWidth + x] = qrColor; } else { piexs[y * qrWidth] = 0xffffffff; } } } Bitmap bitmap = Bitmap.createBitmap(qrWidth, qrHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(piexs, 0, qrWidth, 0, 0, qrWidth, qrHeight, true, null); if (logo != null) { int logoHeight = qrHeight / 5; int logoWidth = qrWidth / 5; canvas.drawBitmap(genThumbnails(logo, logoWidth, logoHeight), (qrWidth - logoWidth) / 2.0f, (qrHeight - logoHeight) / 2.0f, null); } return bitmap; } catch (WriterException e) { 4000 e.printStackTrace(); } return null; } private static Bitmap genThumbnails(Bitmap logo, int logoWidth, int logoHeight) { if (logo == null || logoHeight < 0 || logoWidth < 0) { throw new IllegalArgumentException(); } Bitmap bitmap = Bitmap.createBitmap(logoWidth, logoHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); canvas.drawBitmap(Bitmap.createScaledBitmap(logo, logoWidth - 10, logoHeight - 10, false), 5, 5, null); return bitmap; } }
相关文章推荐
- java复习(-)基本类型
- 编写一个封装用户信息的JavaBean对象,通过操作JavaBean的动作标识,输出用户的注册信息。
- Spark学习笔记总结-超级经典总结
- java集成开发环境eclipse/MyEclipse
- dubbo+zk实现demo
- mydumper安装笔记
- weka连接sqlserver2005
- linux --- TCPdump 工具
- beego任务定时执行,延迟执行
- Json 转 dynamic
- 框架组开发感想
- Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题
- 软件工程课设迭代开发第四天
- 音频管理器 AudioManager
- 将二叉搜索树转换成双向链表
- hdu_3549_Flow Problem(最大流)
- 开发restful风格的webservice
- nrf24L01接收端只触发一次中断解决办法
- hdu_3549_Flow Problem(最大流)
- 全文搜索原理简单解析