1. 程式人生 > >二維碼的生成&加背景圖片的巢狀->支付寶(Java)(一)

二維碼的生成&加背景圖片的巢狀->支付寶(Java)(一)

匯入google.zxing包
在pom.xml 中新增

    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.1.0</version>
 </dependency>
 
<dependency>
       <groupId>com.twelvemonkeys.imageio</groupId>
       <artifactId>imageio-jpeg</artifactId>
       <version>3.0-rc5</version>
</dependency>

實現編碼

public class ZXingCode {
 
    private static final int QRCOLOR = 0xFF000000; // 預設是黑色
    private static final int BGWHITE = 0xFFFFFFFF; // 背景顏色
 
    private static final int WIDTH = 200; // 二維碼寬
    private static final int HEIGHT = 200; // 二維碼高
 
    // 用於設定QR二維碼引數
    private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
        private static final long serialVersionUID = 1L;
 
        {
            put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 設定QR二維碼的糾錯級別(H為最高級別)具體級別資訊
            put(EncodeHintType.CHARACTER_SET, "utf-8");// 設定編碼方式
            put(EncodeHintType.MARGIN, 0);
        }
    };
 
 
    // 生成帶logo的二維碼圖片
    public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl) {
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 引數順序分別為:編碼內容,編碼型別,生成圖片寬度,生成圖片高度,設定引數
            BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
 
            // 開始利用二維碼資料建立Bitmap圖片,分別設為黑(0xFFFFFFFF)白(0xFF000000)兩色
            for (int x = 0; x < WIDTH; x++) {
                for (int y = 0; y < HEIGHT; y++) {
                    image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
                }
            }
 
            int width = image.getWidth();
            int height = image.getHeight();
            if (Objects.nonNull(logoFile) && logoFile.exists()) {
                // 構建繪圖物件
                Graphics2D g = image.createGraphics();
                // 讀取Logo圖片
                BufferedImage logo = ImageIO.read(logoFile);
                // 開始繪製logo圖片
                g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
                g.dispose();
                logo.flush();
            }
 
            image.flush();
 
            ImageIO.write(image, "png", codeFile); // TODO
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) throws WriterException {
        File logoFile = new File("C://Users//mayn//Desktop//型號//小米//hongmi 5A//5a9d2490N317048d9.jpg");
        File QrCodeFile = new File("C://Users//mayn//Desktop//05.png");
        String url = "https://www.baidu.com/";
        String note = "訪問百度連線";
        drawLogoQRCode(logoFile, QrCodeFile, url);
       
}

3.合成二維碼

public class mergeImage {
 
 
    public static void mergeImage(String bigPath, String smallPath, String x, String y) throws IOException {
 
        try {
            BufferedImage small;
            BufferedImage big = ImageIO.read(new File(bigPath));
            if (smallPath.contains("http")) {
 
                URL url = new URL(smallPath);
                small = ImageIO.read(url);
            } else {
                small = ImageIO.read(new File(smallPath));
            }
 
            Graphics2D g = big.createGraphics();
 
            float fx = Float.parseFloat(x);
            float fy = Float.parseFloat(y);
            int x_i = (int) fx;
            int y_i = (int) fy;
            g.drawImage(small, x_i, y_i, small.getWidth(), small.getHeight(), null);
            g.dispose();
            ImageIO.write(big, "png", new File(bigPath));
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
}

4.執行

public static void main(String[] args) throws WriterException {
        try {
            mergeImage.mergeImage("C://Users//mayn//Desktop//origin.png", "C://Users//mayn//Desktop//05.png", "63", "163");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

程式碼轉載:https://blog.csdn.net/FTL_NXY/article/details/81483343
做一備忘