借助第三方Jar包实现二维码:
http://central.maven.org/maven2/com/google/zxing/core/3.2.0/core-3.2.0.jar
pom.xml 文件依赖
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
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;
//java 实现二维码
public class Pro_QRcode {
public static void main(String[] args) {
int width = 300;
int height = 300;
String format = "png";
String contents = "http://985571285.iteye.com/blog/2330432";
//定义参数
HashMap hints = new HashMap();
//字符集
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
//纠正等级分为:L(纠错等级%7)M(纠错等级%15)Q(纠错等级%25)H(纠错等级%30);
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
//外边距
hints.put(EncodeHintType.MARGIN, 0);
try {
BitMatrix matrix =
new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,hints);
//写出路径
Path file = new File("D:/PIC.png").toPath();
try {
MatrixToImageWriter.writeToPath(matrix, format, file);
} catch (IOException e) {
e.printStackTrace();
}
} catch (WriterException e) {
e.printStackTrace();
}
}
}
二 、流输出
https://www.cnblogs.com/jston/archive/2013/01/30/2883460.html