• java二维码生成代码


        
     QRCodeUtil.encode(text, "D:/004.jpg", "D:", true, "exp");// 这个方法的第一个参数是网址,第二个参数是内嵌图片,第三个参数:是否压缩logo,第四个参数:二维码名字;
    需要core-2.3.0.jar包,然后直接复制粘贴即可用。




    1
    package otherTest; 2 3 import java.awt.BasicStroke; 4 import java.awt.Graphics; 5 import java.awt.Graphics2D; 6 import java.awt.Image; 7 import java.awt.Shape; 8 import java.awt.geom.RoundRectangle2D; 9 import java.awt.image.BufferedImage; 10 import java.io.File; 11 import java.io.OutputStream; 12 import java.util.Hashtable; 13 14 import javax.imageio.ImageIO; 15 16 import com.google.zxing.BarcodeFormat; 17 import com.google.zxing.EncodeHintType; 18 import com.google.zxing.MultiFormatWriter; 19 import com.google.zxing.common.BitMatrix; 20 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 21 22 public class QRCodeUtil { 23 private static final String CHARSET = "utf-8"; 24 private static final String FORMAT_NAME = "JPG"; 25 // 二维码尺寸 26 private static final int QRCODE_SIZE = 300; 27 // LOGO宽度 28 private static final int WIDTH = 60; 29 // LOGO高度 30 private static final int HEIGHT = 60; 31 32 private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception { 33 Hashtable hints = new Hashtable(); 34 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 35 hints.put(EncodeHintType.CHARACTER_SET, CHARSET); 36 hints.put(EncodeHintType.MARGIN, 1); 37 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, 38 hints); 39 int width = bitMatrix.getWidth(); 40 int height = bitMatrix.getHeight(); 41 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 42 for (int x = 0; x < width; x++) { 43 for (int y = 0; y < height; y++) { 44 image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); 45 } 46 } 47 if (imgPath == null || "".equals(imgPath)) { 48 return image; 49 } 50 51 // 插入图片 52 QRCodeUtil.insertImage(image, imgPath, needCompress); 53 return image; 54 } 55 56 private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception { 57 File file = new File(imgPath); 58 if (!file.exists()) { 59 System.err.println("" + imgPath + " 该文件不存在!"); 60 return; 61 } 62 Image src = ImageIO.read(new File(imgPath)); 63 int width = src.getWidth(null); 64 int height = src.getHeight(null); 65 if (needCompress) { // 压缩LOGO 66 if (width > WIDTH) { 67 width = WIDTH; 68 } 69 if (height > HEIGHT) { 70 height = HEIGHT; 71 } 72 Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH); 73 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 74 Graphics g = tag.getGraphics(); 75 g.drawImage(image, 0, 0, null); // 绘制缩小后的图 76 g.dispose(); 77 src = image; 78 } 79 // 插入LOGO 80 Graphics2D graph = source.createGraphics(); 81 int x = (QRCODE_SIZE - width) / 2; 82 int y = (QRCODE_SIZE - height) / 2; 83 graph.drawImage(src, x, y, width, height, null); 84 Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6); 85 graph.setStroke(new BasicStroke(3f)); 86 graph.draw(shape); 87 graph.dispose(); 88 } 89 90 public static void encode(String content, String imgPath, String destPath, boolean needCompress, String fileName) 91 throws Exception { 92 BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress); 93 mkdirs(destPath); 94 String file = fileName + "." + FORMAT_NAME.toLowerCase(); 95 ImageIO.write(image, FORMAT_NAME, new File(destPath + File.separator + file)); 96 } 97 98 public static void mkdirs(String destPath) { 99 File file = new File(destPath); 100 // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) 101 if (!file.exists() && !file.isDirectory()) { 102 file.mkdirs(); 103 } 104 } 105 public static void main(String[] args) throws Exception { 106 String text = "http://www.baidu.com"; 107 QRCodeUtil.encode(text, "D:/004.jpg", "D:", true, "exp"); 108 } 109 }
    休闲玩家 佛系更博
  • 相关阅读:
    作为一名程序员应该具备哪些素质
    从100万个整数里找出100个最大的数
    数据库 SQL语句优化
    服务器上文件打包下载
    ThreadPoolExcutor
    几种序列化与get、set方法的关系
    idea没有错误出现红色波浪线怎么去掉?
    两个对象的属性赋值
    RandomStringUtils的使用
    IDEA中类似eclipse的workSpace的实现
  • 原文地址:https://www.cnblogs.com/yuyuchen/p/8072169.html
Copyright © 2020-2023  润新知