Java生成内嵌logo的二维码
使用jar包maven配置:
1 <dependency> 2 <groupId>com.google.zxing</groupId> 3 <artifactId>core</artifactId> 4 <version>3.0.0</version> 5 </dependency> 6 <dependency> 7 <groupId>com.google.zxing</groupId> 8 <artifactId>javase</artifactId> 9 <version>3.0.0</version> 10 </dependency>
Java生成内嵌logo的二维码:
1 package ImagePackage; 2 3 import java.awt.AlphaComposite; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.File; 7 import java.io.IOException; 8 9 import javax.imageio.ImageIO; 10 11 import com.google.zxing.BarcodeFormat; 12 import com.google.zxing.BinaryBitmap; 13 import com.google.zxing.MultiFormatReader; 14 import com.google.zxing.MultiFormatWriter; 15 import com.google.zxing.NotFoundException; 16 import com.google.zxing.Result; 17 import com.google.zxing.WriterException; 18 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 19 import com.google.zxing.client.j2se.MatrixToImageConfig; 20 import com.google.zxing.client.j2se.MatrixToImageWriter; 21 import com.google.zxing.common.BitMatrix; 22 import com.google.zxing.common.HybridBinarizer; 23 24 public class QrcodeUtil { 25 26 27 public static void genQrcode(int width,int height,String format,String url,String savePath){ 28 try { 29 BitMatrix bitMatrix= new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height); 30 // 生成的二维码图片默认背景为白色,前景为黑色,但是在加入logo图像后会导致logo也变为黑白色,至于是什么原因还没有仔细去读它的源码 31 // 所以这里对其第一个参数黑色将ZXing默认的前景色0xFF000000稍微改了一下0xFF000001,最终效果也是白色背景黑色前景的二维码,且logo颜色保持原有不变 32 MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF); 33 // 这里要显式指定MatrixToImageConfig,否则还会按照默认处理将logo图像也变为黑白色(如果打算加logo的话,反之则不须传MatrixToImageConfig参数) 34 // 开始画二维码 35 BufferedImage barCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix,config); 36 generateSaveFile(barCodeImage,savePath); 37 System.out.println("qrcode write..."); 38 }catch (WriterException e) { 39 e.printStackTrace(); 40 } 41 42 } 43 44 /** 45 * 输出图片 46 */ 47 public static void generateSaveFile(BufferedImage buffImg, String savePath) { 48 int temp = savePath.lastIndexOf(".") + 1; 49 try { 50 File outFile = new File(savePath); 51 if(!outFile.exists()){ 52 outFile.createNewFile(); 53 } 54 ImageIO.write(buffImg, savePath.substring(temp), outFile); 55 System.out.println("ImageIO write..."); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 } 60 61 62 63 /** 64 * 为二维码图片增加logo头像 65 * @see 其原理类似于图片加水印 66 * @param image 二维码图片转化的BufferedImage对象 67 * @param logoImage logo头像转化的BufferedImage对象 68 */ 69 public static BufferedImage overlapImage(BufferedImage image, BufferedImage logoImage){ 70 int logoWidth = image.getWidth() / 5; // 设置logo图片宽度为二维码图片的五分之一 71 int logoHeight = image.getHeight() / 5; // 设置logo图片高度为二维码图片的五分之一 72 int logoX = (image.getWidth() - logoWidth) / 2; // 设置logo图片的位置,这里令其居中 73 int logoY = (image.getHeight() - logoHeight) / 2; // 设置logo图片的位置,这里令其居中 74 Graphics2D graphics = image.createGraphics(); 75 // 在图形和图像中实现混合和透明效果 76 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f)); 77 graphics.drawImage(logoImage, logoX, logoY, logoWidth, logoHeight, null); 78 graphics.dispose(); 79 return image; 80 } 81 82 83 84 public static void genQrcodeTest() { 85 int width = 300; 86 int height = 300; 87 String format = "png"; 88 String url = "https://www.baidu.com"; 89 String savePath = "D://test//new.png"; 90 genQrcode(width, height, format, url, savePath); 91 92 } 93 94 public static void logoQrcodeTest(){ 95 96 try{ 97 String imagePath = "D://test//new.png"; 98 File f = new File(imagePath); 99 BufferedImage image = ImageIO.read(f); 100 String logoPath = "D://test//logo.png"; 101 File logof = new File(logoPath); 102 BufferedImage logoImage = ImageIO.read(logof); 103 104 BufferedImage qrCodeImage = overlapImage(image,logoImage); 105 String newPath = "D://test//logoNew.png"; //内嵌logo后新二维码地址 106 generateSaveFile(qrCodeImage,newPath); 107 108 }catch(Exception e){ 109 e.printStackTrace(); 110 } 111 } 112 113 114 public static void main(String[] args) { 115 String savePath = "D://test//logoNew.png"; 116 //生成二维码 117 genQrcodeTest(); 118 119 //二维码内嵌logo 120 logoQrcodeTest(); 121 122 } 123 124 }