首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

QR二维码Java开发札记

2012-12-18 
QR二维码Java开发笔记?? 版本7???/** * @param sms_info信息内容 * @param filePath输出路径 * @param wid

QR二维码Java开发笔记

?? 版本7QR二维码Java开发札记

?

?

?

/** * @param sms_info信息内容 * @param filePath 输出路径 * @param width宽度 * @param height高度 * @throws Exception */public static void createImage(String sms_info,String filePath,int width,int height) throws Exception {try {Qrcode testQrcode = new Qrcode();testQrcode.setQrcodeErrorCorrect('M');testQrcode.setQrcodeEncodeMode('B');testQrcode.setQrcodeVersion(7);String testString = sms_info;byte[] d = testString.getBytes("UTF-8");System.out.println(d.length);// 限制最大字节数为120if (d.length > 0 && d.length < 120) {boolean[][] s = testQrcode.calQrcode(d);BufferedImage bi = new BufferedImage(s.length, s[0].length,BufferedImage.TYPE_BYTE_BINARY);Graphics2D g = bi.createGraphics();g.setBackground(Color.WHITE);g.clearRect(0, 0, s.length, s[0].length);g.setColor(Color.BLACK);int mulriple=1;for (int i = 0; i < s.length; i++) {for (int j = 0; j < s.length; j++) {if (s[j][i]) {g.fillRect(j * mulriple, i * mulriple, mulriple, mulriple);}}}g.dispose();bi.flush();File f = new File(filePath);if (!f.exists()) {f.createNewFile();}bi=resize(bi,width,height);// 创建图片ImageIO.write(bi, "png", f);}}catch (Exception e) {e.printStackTrace();}} /** * 图像缩放 * @param source * @param targetW * @param targetH * @return */ private static BufferedImage resize(BufferedImage source, int targetW, int targetH) { int type = source.getType(); BufferedImage target = null; double sx = (double) targetW / source.getWidth(); double sy = (double) targetH / source.getHeight(); target = new BufferedImage(targetW, targetH, type); Graphics2D g = target.createGraphics(); g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); g.dispose(); return target; }

?

可以通过以下方法设置容错、编码模式、版本

setQrcodeErrorCorrect?????? L M Q H

?

setQrcodeEncodeMode????? N数字 A英文 其他为Binary

?

setQrcodeVersion????????????? 不设置会自动选择适当的版本

?

这个库只是通过calQrcode方法返回表示二维码的数组,图像需要自己处理

boolean[][] s = testQrcode.calQrcode(d);

?

热点排行