用Java手寫一個二維碼(Java)
阿新 • • 發佈:2021-01-01
首先匯入谷歌的依賴
<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();
}
}
然後就會生成這樣一個二維碼啦: