package cn.qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.decoder.Mode; import com.google.zxing.qrcode.decoder.Version; import com.google.zxing.qrcode.encoder.QRCode; public class QRTest2 { public static void main(String[] args) { try { createImage("hello world!", "src/a.png", 200, 200); } catch (Exception e) { e.printStackTrace(); } } /** * @param contents 字符串内容 * @param width 宽度 * @param height 高度 * @param imgPath 图片输出路径 */ public void encode(String contents, int width, int height, String imgPath) { Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath)); } catch (Exception e) { e.printStackTrace(); } } /** * @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 qrCode = new QRCode(); qrCode.setECLevel(ErrorCorrectionLevel.M); qrCode.setMode(Mode.BYTE); qrCode.setVersion(Version.getVersionForNumber(7)); String testString = sms_info; byte[] d = testString.getBytes("UTF-8"); System.out.println(d.length); // // 限制最大字节数为120 // if (d.length > 0 && d.length < 120) { // boolean[][] s = qrCode.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; } }

lwt123456 LV1
2021年12月1日
newbag LV9
2021年8月9日
wyx065747 LV67
2021年7月9日
17587079094 LV6
2021年4月27日
robinyu1022 LV1
2021年1月22日
yccycc2002 LV1
2020年9月2日
739520942 LV8
2020年8月6日
huasir2018 LV14
2020年5月9日
没文化 LV1
2020年4月23日
雨泽ta姓郝 LV2
2020年4月15日