admin
2024-07-25 47e3087067abd35e6337c011f96d2338c0bb1aae
src/main/java/org/yeshi/utils/QRCodeUtil.java
@@ -26,6 +26,7 @@
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
@@ -34,11 +35,11 @@
 * 二维码生成
 * 
 * @author Administrator
 *
 */
public class QRCodeUtil {
   private static final String CHARSET = "UTF-8";
   private static final String FORMAT_NAME = "JPG";
   // 二维码尺寸
   int QRCODE_SIZE = 200;
   // LOGO宽度
@@ -69,18 +70,20 @@
   /**
    * user: Rex date: 2016年12月29日 上午12:31:29
    * 
    * @param content
    *            二维码内容
    * @param logoImgPath
    *            Logo
    * @param needCompress
    *            是否压缩Logo
     * @param content      二维码内容
     * @param logoImgPath  Logo
     * @param needCompress 是否压缩Logo
    * @return 返回二维码图片
    * @throws WriterException
    * @throws IOException
    *             BufferedImage TODO 创建二维码图片
    */
   private BufferedImage createImage(String content, InputStream logoImg, boolean needCompress)
            throws WriterException, IOException {
        return this.createImage(content, logoImg, 0xFF000000, 0xFFFFFFFF, needCompress);
    }
    public BufferedImage createImage(String content, InputStream logoImg, int color, int bgColor, boolean needCompress)
         throws WriterException, IOException {
      Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
@@ -94,33 +97,38 @@
      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      for (int x = 0; x < width; x++) {
         for (int y = 0; y < height; y++) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                image.setRGB(x, y, bitMatrix.get(x, y) ? color : bgColor);
         }
      }
      if (logoImg == null) {
         return image;
      }
      
        if (logoImg != null) {
      // 插入图片
      insertImage(image, logoImg, needCompress);
        }
      return image;
   }
   /**
    * user: Rex date: 2016年12月29日 上午12:31:29
    * 
    * @param content
    *            二维码内容
    * @param logoImgPath
    *            Logo
    * @param needCompress
    *            是否压缩Logo
     * @param content      二维码内容
     * @param logoImgPath  Logo
     * @param needCompress 是否压缩Logo
    * @return 返回二维码图片
    * @throws WriterException
    * @throws IOException
    *             BufferedImage TODO 创建二维码图片
    */
   private BufferedImage createImageDeleteWhite(String content, InputStream logoImg, boolean needCompress)
            throws WriterException, IOException {
        return createImageDeleteWhite(content, logoImg, 0xFF000000, 0xFFFFFFFF, needCompress);
    }
    public BufferedImage createImageDeleteWhite(String content, InputStream logoImg, int color, int bgColor, boolean needCompress)
         throws WriterException, IOException {
      Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
      hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
@@ -137,7 +145,7 @@
      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      for (int x = 0; x < width; x++) {
         for (int y = 0; y < height; y++) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                image.setRGB(x, y, bitMatrix.get(x, y) ? color : bgColor);
         }
      }
      if (logoImg == null) {
@@ -145,21 +153,19 @@
      }
      
      // 插入图片
        if (logoImg != null) {
      insertImage(image, logoImg, needCompress);
        }
      return image;
   }
   
   /**
    * user: Rex date: 2016年12月29日 上午12:30:09
    * 
    * @param source
    *            二维码图片
    * @param logoImgPath
    *            Logo
    * @param needCompress
    *            是否压缩Logo
    * @throws IOException
    *             void TODO 添加Logo
     * @param source       二维码图片
     * @param logoImgPath  Logo
     * @param needCompress 是否压缩Logo
     * @throws IOException void TODO 添加Logo
    */
   private void insertImage(BufferedImage source, InputStream logoImg, boolean needCompress) throws IOException {
      Image src = ImageIO.read(logoImg);
@@ -196,16 +202,11 @@
   /**
    * user: Rex date: 2016年12月29日 上午12:32:32
    * 
    * @param content
    *            二维码内容
    * @param logoImgPath
    *            Logo
    * @param destPath
    *            二维码输出路径
    * @param needCompress
    *            是否压缩Logo
    * @throws Exception
    *             void TODO 生成带Logo的二维码
     * @param content      二维码内容
     * @param logoImgPath  Logo
     * @param destPath     二维码输出路径
     * @param needCompress 是否压缩Logo
     * @throws Exception void TODO 生成带Logo的二维码
    */
   public void encode(String content, InputStream logoImg, String destPath, boolean needCompress) throws Exception {
      BufferedImage image = createImage(content, logoImg, needCompress);
@@ -215,12 +216,9 @@
   /**
    * user: Rex date: 2016年12月29日 上午12:35:44
    * 
    * @param content
    *            二维码内容
    * @param destPath
    *            二维码输出路径
    * @throws Exception
    *             void TODO 生成不带Logo的二维码
     * @param content  二维码内容
     * @param destPath 二维码输出路径
     * @throws Exception void TODO 生成不带Logo的二维码
    */
   public void encode(String content, String destPath) throws Exception {
      encode(content, null, destPath, false);
@@ -243,16 +241,11 @@
   /**
    * user: Rex date: 2016年12月29日 上午12:36:58
    * 
    * @param content
    *            二维码内容
    * @param logoImgPath
    *            Logo
    * @param output
    *            输出流
    * @param needCompress
    *            是否压缩Logo
    * @throws Exception
    *             void TODO 生成带Logo的二维码,并输出到指定的输出流
     * @param content      二维码内容
     * @param logoImgPath  Logo
     * @param output       输出流
     * @param needCompress 是否压缩Logo
     * @throws Exception void TODO 生成带Logo的二维码,并输出到指定的输出流
    */
   public void encode(String content, InputStream logoImg, OutputStream output, boolean needCompress)
         throws Exception {
@@ -261,11 +254,9 @@
   }
   /**
    *
    * @param content
    * @param logoImg
    * @param output
    *            二进制输出流
     * @param output       二进制输出流
    * @param needCompress
    * @throws Exception
    */
@@ -276,11 +267,9 @@
   }
   
   /**
    *
    * @param content
    * @param logoImg
    * @param output
    *            二进制输出流
     * @param output       二进制输出流
    * @param needCompress
    * @throws Exception
    */
@@ -293,12 +282,9 @@
   /**
    * user: Rex date: 2016年12月29日 上午12:38:02
    * 
    * @param content
    *            二维码内容
    * @param output
    *            输出流
    * @throws Exception
    *             void TODO 生成不带Logo的二维码,并输出到指定的输出流
     * @param content 二维码内容
     * @param output  输出流
     * @throws Exception void TODO 生成不带Logo的二维码,并输出到指定的输出流
    */
   public void encode(String content, OutputStream output) throws Exception {
      encode(content, null, output, false);
@@ -307,11 +293,9 @@
   /**
    * user: Rex date: 2016年12月29日 上午12:39:10
    * 
    * @param file
    *            二维码
     * @param file 二维码
    * @return 返回解析得到的二维码内容
    * @throws Exception
    *             String TODO 二维码解析
     * @throws Exception String TODO 二维码解析
    */
   public String decode(File file) throws Exception {
      BufferedImage image;
@@ -332,11 +316,9 @@
   /**
    * user: Rex date: 2016年12月29日 上午12:39:48
    * 
    * @param path
    *            二维码存储位置
     * @param path 二维码存储位置
    * @return 返回解析得到的二维码内容
    * @throws Exception
    *             String TODO 二维码解析
     * @throws Exception String TODO 二维码解析
    */
   public String decode(String path) throws Exception {
      return decode(new File(path));