1. 程式人生 > 實用技巧 >生成二維碼圖片和解析二維碼圖片

生成二維碼圖片和解析二維碼圖片

  引用自 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方法解析的。