用Java手写一个二维码(Java)

首先导入谷歌的依赖

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.1.0</version>
        </dependency>
        //当然你也可以同时导入下面这个包,这个包集成了不少上面包的工具类,这里笔者只用了上面的这个包
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.1.0</version>
        </dependency>
package com.llq.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

public class QRCodeCreate {

    private static final int INSERTED_IMAGE_WIDTH = 60;

    private static final int INSERTED_IMAGE_HEIGHT = 60;

    private static final int QRCODE_SIZE = 200;

    public static void createQRCode(String content,int width,int height,String path,String logoPath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < bitMatrix.getWidth(); x++) {
            for (int y = 0; y < bitMatrix.getHeight(); y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? Color.black.getRGB(): Color.white.getRGB());
            }
        }
        insertImage(image,logoPath,true);
        OutputStream stream = new FileOutputStream(path);
        ImageIO.write(image,"JPG",stream);
    }

    public static void main(String[] args) {
        try {
            createQRCode("http://www.baidu.com",QRCODE_SIZE,QRCODE_SIZE,"C:\\Users\\cxk\\Desktop\\3.JPG","C:\\Users\\cxk\\Desktop\\图片\\a.jpg");
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void insertImage(BufferedImage historyImage,String insertedImagePath,boolean needCompress) throws IOException {
        File file = new File(insertedImagePath);
        if (!file.exists()) {
            System.err.println("" + insertedImagePath + "   该文件不存在!");
            return;
        }
        Image src = ImageIO.read(new File(insertedImagePath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        if (needCompress) { // 压缩LOGO
            if (width > INSERTED_IMAGE_WIDTH) {
                width = INSERTED_IMAGE_WIDTH;
            }
            if (height > INSERTED_IMAGE_HEIGHT) {
                height = INSERTED_IMAGE_HEIGHT;
            }
            //获取一个缩放的实例
            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
            g.dispose();
            src = image;
        }
        // 插入LOGO
        Graphics2D graph = historyImage.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(src, x, y, width, height, null);
        //创建阴影
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        //设置线条粗细
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }
}

然后就会生成这样一个二维码啦:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值