方案一:(此方案也可生成文件流)
1、导入maven
<!--二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
2、生成本地文件
package com.code;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileSystemView;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
public class TwoCodeUtil {
public static void main(String[] args) {
//扫码内容地址
String url = "https://blog.csdn.net/a985548426";
//地址路径(本地地址)
String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode";
//文件名
String fileName = "temp.jpg";
File file = createQrCode(url, path, fileName);
//上传到OSS返回链接
System.out.println(file);
System.out.println(file.getName());
}
public static File createQrCode(String url, String path, String fileName) {
try {
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
File file = new File(path, fileName);
if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {
writeToFile(bitMatrix, "jpg", file);
return file;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
}
main运行结果:
C:\Users\EDZ\Desktop\testQrcode\temp.jpg
temp.jpg
方案二:(此方案也可生成本地文件)
1、导入maven
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.1</version>
</dependency>
2、生成本地文件或文件流
package com.code;
import cn.hutool.extra.qrcode.QrCodeUtil;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
public class HuToolCodeUtil {
public static void main(String[] args) throws IOException {
//1.二维码访问地址
String qrCodeImgUrl = "https://blog.csdn.net/a985548426";
String fileName = "code";
String imageType = "png";
//2.生成图片流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
QrCodeUtil.generate(qrCodeImgUrl, 200, 200, imageType , baos);
//3.上传图片到阿里云(将文件流上传到oss)
// String url = OssBootUtil.upload(swapStream, fileName+"."+imageType);
//4.将流转到本地文件
final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
InputStream input = swapStream;
int index = 0;
byte[] bytes = new byte[1024];
//本地文件地址
String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode/";
FileOutputStream downloadFile = new FileOutputStream(path+fileName+"."+imageType);
while (true) {
try {
if (!((index = input.read(bytes)) != -1)) break;
} catch (IOException e) {
e.printStackTrace();
}
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
downloadFile.close();
input.close();
}
}
main 运行结果
好了,最后感谢支持,希望对你有用!