1. 程式人生 > >Java生成內嵌logo的二維碼

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 }