引用自 https://blog.csdn.net/jam_fanatic/article/details/82818857

  这里使用的是google提供的一个工具  

1.引入依赖

    

    <!-- google生成二维码 -->
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.3.0</version>
    </dependency>

2.创建BufferedImageLuminanceSource类,该类继承LuminanceSource

package tacos.web;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import com.google.zxing.LuminanceSource;

public class BufferedImageLuminanceSource extends LuminanceSource {
     
        private final BufferedImage image;
        private final int left;
        private final int top;
     
        public BufferedImageLuminanceSource(BufferedImage image) {
            this(image, 0, 0, image.getWidth(), image.getHeight());
        }
     
        public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
            super(width, height);
     
            int sourceWidth = image.getWidth();
            int sourceHeight = image.getHeight();
            if (left + width > sourceWidth || top + height > sourceHeight) {
                throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
            }
     
            for (int y = top; y < top + height; y++) {
                for (int x = left; x < left + width; x++) {
                    if ((image.getRGB(x, y) & 0xFF000000) == 0) {
                        image.setRGB(x, y, 0xFFFFFFFF); // = white
                    }
                }
            }
     
            this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
            this.image.getGraphics().drawImage(image, 0, 0, null);
            this.left = left;
            this.top = top;
        }
     
        public byte[] getRow(int y, byte[] row) {
            if (y < 0 || y >= getHeight()) {
                throw new IllegalArgumentException("Requested row is outside the image: " + y);
            }
            int width = getWidth();
            if (row == null || row.length < width) {
                row = new byte[width];
            }
            image.getRaster().getDataElements(left, top + y, width, 1, row);
            return row;
        }
     
        public byte[] getMatrix() {
            int width = getWidth();
            int height = getHeight();
            int area = width * height;
            byte[] matrix = new byte[area];
            image.getRaster().getDataElements(left, top, width, height, matrix);
            return matrix;
        }
     
        public boolean isCropSupported() {
            return true;
        }
     
        public LuminanceSource crop(int left, int top, int width, int height) {
            return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
        }
     
        public boolean isRotateSupported() {
            return true;
        }
     
        public LuminanceSource rotateCounterClockwise() {
            int sourceWidth = image.getWidth();
            int sourceHeight = image.getHeight();
            AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
            BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
            Graphics2D g = rotatedImage.createGraphics();
            g.drawImage(image, transform, null);
            g.dispose();
            int width = getWidth();
            return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
        }
     
    }

3.创建生成二维码图片和解析二维码图片工具类

package tacos.web;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class DrawPictureUtils {
    
    public static void main(String[] args) {
 
        BufferedImage bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_BGR);
 
        final File file = new File("c:\\javaPic.png");
        
        try {
            if(file.exists()) {
                file.delete();
                file.createNewFile();
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
        
        
        writeImage(bi, "png", file);
        System.out.println("绘图成功");
        
    }
    
    /** 通过指定参数写一个图片  */
    public static boolean writeImage(BufferedImage bi, String picType, File file) {
        
        Graphics g = bi.getGraphics();
        
        //g.setColor(new Color(12, 123, 88));
        //g.drawLine(0, 100, 100, 100);
        //g.dispose();
        boolean val = false;
        try {
            val = ImageIO.write(bi, picType, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return val;
    }
}

4. 调用

package tacos.web;

public class QrCodeTest {
    public static void main(String[] args) throws Exception {
        // 存放在二维码中的内容
        String text = "我是哈哈";
        // 嵌入二维码的图片路径
        String imgPath = "/erweima/aa.jpg";
        // 生成的二维码的路径及名称
        String destPath = "/erweima/bb.jpg";
        
        //生成二维码
        QRCodeUtil.encode(text, imgPath,destPath, true);
        // 解析二维码
        String str = QRCodeUtil.decode(destPath);
        
        // 打印出解析出的内容
        System.out.println(str);
    }
}

5.成果

下面是要嵌入二维码的图片

下面是生成的二维码图片



这里有个问题,嵌入图片后,生成的二维码手机可以扫出来,但是调用decode方法去解析,解析不出来。

不嵌入图片,也就是encode第二个参数传null,是可以调用decode方法解析的。