利用Java生成二维码

1、利用Java生成带logo的二维码,以及可以改变二维码的颜色。

导入连个包:javase-3.3.0.jar和core-3.3.0.jar

2、代码:

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

import javax.imageio.ImageIO;

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;

/**
 * 二维码工具类
 * 
 */
public class QRCodeUtil {
    private static final String CHARSET = "utf-8";
    private static final String FORMAT = "JPG";

    public static void main(String[] args) throws Exception {
        String text = "http://www.baidu.com";
        String finame=QRCodeUtil.encode(text, "/Users/zhangjinru/背景图/666.jpg", "/Users/zhangjinru", "qrcode", true,900,60);
        System.out.println(finame);
    }
    
    /**
     * 
     * @param content 二维码内容
     * @param logoPath logo图片的路径
     * @param destPath 二维码保存的文件夹
     * @param fileName 二维码名字
     * @param needCompress 是否压缩logo图片
     * @param qrcode_size 二维码大小
     * @param logo_size logo 大小
     * @return 返回生成二维码的路径
     * @throws Exception
     */
    public static String encode(String content, String logoPath, String destPath, String fileName, boolean needCompress,int qrcode_size,int logo_size) throws Exception {
        BufferedImage image = createImage(content, logoPath, needCompress,qrcode_size,logo_size);
        File file = new File(destPath);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        fileName = fileName.substring(0, fileName.indexOf(".")>0?fileName.indexOf("."):fileName.length())  + "." + FORMAT.toLowerCase();
        ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));
        return destPath+File.separator+fileName;
    }

    private static BufferedImage createImage(String content, String logoPath, boolean needCompress,int qrcode_size,int logo_size) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrcode_size, qrcode_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);//改变二维码的颜色
            }
        }
        if (logoPath == null || "".equals(logoPath)) {
            return image;
        }
        // 插入图片
        QRCodeUtil.insertImage(image, logoPath, needCompress,qrcode_size,logo_size);
        return image;
    }

    private static void insertImage(BufferedImage source, String logoPath, boolean needCompress,int qrcode_size,int logo_size ) throws Exception {
        File file = new File(logoPath);
        if (!file.exists()) {
            throw new Exception("logo file not found.");
        }
        Image src = ImageIO.read(new File(logoPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        if (needCompress) { // 压缩LOGO
            if (width > logo_size) {
                width = logo_size;
            }
            if (height > logo_size) {
                height = logo_size;
            }
            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 = source.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();
    }

}
原文地址:https://www.cnblogs.com/zhangjinru123/p/7995344.html