자바 QR 코드 생 성, 로고, 아래쪽 텍스트

5442 단어
maven

   com.google.zxing
   core
   2.2

도구 클래스

import cn.hutool.core.codec.Base64Encoder;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class QRCodeUtils {
 
    /**
     *      
     *
     */
    public static BufferedImage getImg(String content, int size) throws Exception {
        //    ,      
        Map hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //    (H     )
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //     ,  0~4
        hints.put(EncodeHintType.MARGIN, 1);
        //    
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
 
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        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);
            }
        }
 
        return image;
    }
 
    /**
     *    byte
     *
     */
    public static byte[] getByte(BufferedImage img) throws Exception {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        ImageIO.write(img, "png", bao);
        return bao.toByteArray();
    }
 
    /**
     *     logo
     */
    public static BufferedImage withLogo(BufferedImage img, String logoPath) throws IOException {
        int matrixWidth = img.getWidth();
        int matrixHeigh = img.getHeight();
        //  Logo  
        BufferedImage logo = ImageIO.read(new File(logoPath));
        //       ,       
        Graphics2D g2 = img.createGraphics();
        //      
        g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);
        BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        //       
        g2.setStroke(stroke);
        //         
        RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);
        g2.setColor(Color.white);
        //       
        g2.draw(round);
        //  logo        
        BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        //       
        g2.setStroke(stroke2);
        RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);
        g2.setColor(new Color(128,128,128));
        //       
        g2.draw(round2);

        /*
              
             ,      ,      ,       
        
        String text = "     !";
        int fontSize = 30;
        if (StringUtils.isNotEmpty(text)) {
            //          
            BufferedImage outImage = new BufferedImage(matrixWidth, matrixHeigh+fontSize+10, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D outg = outImage.createGraphics();
            //         
            outg.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null);
            outg.setColor(Color.BLACK);
            //  、  、  
            outg.setFont(new Font("  ",Font.BOLD,fontSize));
            int strWidth = outg.getFontMetrics().stringWidth(text);
            //        
            outg.drawString(text, (matrixWidth-strWidth)/2 , matrixHeigh+(outImage.getHeight()-matrixHeigh)/2+10);
            outg.dispose();
            outImage.flush();
            img = outImage;
        }*/
 
        g2.dispose();
        img.flush();
        return img;
    }

}

테스트
public static void main(String[] args) throws Exception {
        //ImageIO.write(QRCodeUtils.getImg("a", 600), "jpg", new File("/develop/data/test/qr.jpg"));
        //ImageIO.write(QRCodeUtils.withLogo( QRCodeUtils.getImg("a", 600), "/develop/data/test/log.png" ),
        //        "png", new File("/develop/data/test/qr.png"));

        //  ImageIO  jpg    ,   byte
        OutputStream out = new FileOutputStream("/develop/data/test/qr.jpg");
        out.write(QRCodeUtils.getByte(QRCodeUtils.withLogo( QRCodeUtils.getImg("a", 300), "/develop/data/test/log.png" )));
        out.flush();
        out.close();

        // base64
        //OutputStream out = new FileOutputStream("/develop/data/test/qr.jpg");
        //out.write(Base64.decodeBase64(
        //        Base64.encodeBase64String(
        //                QRCodeUtils.getByte(getImg("a", 600))
        //        )
        //));
        //out.flush();
        //out.close();

    }

좋은 웹페이지 즐겨찾기