之前对二维码粗略的看了一下觉得可以自己实现一个二维码用来表白,程序员的浪漫。
其实生成一个二维码非常简单几个类就可以搞定
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Scanner;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class CreateQRcode {
public static void main(String[] args) {
int width = 500;
int height = 500;
String format = "png";
String content = " ";
//定义二维码的参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 1);
System.out.println("输入你要生成二维码的网址:");
Scanner scanner = new Scanner(System.in);
content = scanner.nextLine();
//生成二维码
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
File file = new File("D:/img.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, file);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
我就不解释代码了相信都看得懂
刚生成的二维码