
内网环境开发,内外网不互通,想拿东西进出都得申请,有时候写代码时总结了一些帮助开发的工具类,想拿出来做点笔记方便后续的使用,但是由于内网原因,只能再敲一遍代码,这属实是很难受,由于内网仓库二维码开发工具包都有,所以就有了将文件生成二维码再手机扫码拿出文件的想法。
实操(完整类在文末) 所需jar包将字符串生成二维码到指定位置com.google.zxing javase 3.4.1 com.google.zxing core 3.4.1
public static String generateQRImage(String content,
int width,
int height,
String outPath,
String format,
String charset,
String imageName) throws Exception {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map encodeHintTypeStringMap = new HashMap<>();
encodeHintTypeStringMap.put(EncodeHintType.CHARACTER_SET, charset);
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE,
width, height, encodeHintTypeStringMap);
File file = new File(outPath);
mkdirs(file);
String fullPath = outPath + File.separator + imageName;
Path target = FileSystems.getDefault().getPath(fullPath);
MatrixToImageWriter.writeToPath(bitMatrix, format, target);
return fullPath;
}
将指定文件内容生成二维码
public static String generateQRImage(File targetFile,
int width,
int height,
String outPath,
String format,
String charset,
String imageName) throws Exception {
String content = Files.readString(targetFile.toPath(), Charset.forName(DEFAULT_CHARSET));
QRCodeWriter qrCodeWriter = new QRCodeWriter();
File file = new File(outPath);
mkdirs(file);
Map encodeHintTypeStringMap = new HashMap<>();
encodeHintTypeStringMap.put(EncodeHintType.CHARACTER_SET, charset);
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE,
width, height, encodeHintTypeStringMap);
String fullPath = outPath + File.separator + imageName;
Path target = FileSystems.getDefault().getPath(fullPath);
MatrixToImageWriter.writeToPath(bitMatrix, format, target);
return fullPath;
}
public static String generateQRImageByFiles(List targetFiles, String outPath) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
for (File item : targetFiles) {
// 默认文件名
String defaultName = item.getName() + ".png";
String re = generateQRImage(item, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, DEFAULT_FORMAT, DEFAULT_CHARSET, defaultName);
stringBuilder.append(re).append("n");
}
return stringBuilder.toString();
}
public static String generateQRImageByFilePaths(List targetFilesPaths, String outPath) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
for (String item : targetFilesPaths) {
File file = new File(item);
// 默认文件名
String defaultName = file.getName() + ".png";
String re = generateQRImage(file, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, DEFAULT_FORMAT, DEFAULT_CHARSET, defaultName);
stringBuilder.append(re).append("n");
}
return stringBuilder.toString();
}
读取二维码内容为String
public static String decodeQrCode(String imgPath) throws Exception {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imgPath));
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
HashMap decodeHints = new HashMap<>(16);
decodeHints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHARSET);
Result result = new MultiFormatReader().decode(bitmap, decodeHints);
return result.getText();
}
读取二维码内容 将文件以指定名字生成到指定文件夹下
public static String decodeQrCodeToPath(String imgPath, String targetPath, String fileName) throws Exception {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imgPath));
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
HashMap decodeHints = new HashMap<>(16);
decodeHints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHARSET);
Result result = new MultiFormatReader().decode(bitmap, decodeHints);
String text = result.getText();
//将文件以指定名字生成到指定文件夹下
File dir = new File(targetPath);
mkdirs(dir);
File targetFile = new File(targetPath + File.separator + fileName);
if (!targetFile.exists()) {
targetFile.createNewFile();
}
FileWriter fileWriter = new FileWriter(targetFile);
fileWriter.write(text);
fileWriter.flush();
fileWriter.close();
return targetFile.getAbsolutePath();
}
使用实例
public static void main(String[] args) throws Exception {
String outPath = "/Users/Documents/myProject/back/src/main/resources";
String file2 = "/Users/Documents/myProject/back/src/main/resources/application.properties";
//将file2文件生成二维码输出到outpath下
String image = GenerateUtil.generateQRImage(new File(file2), outPath);
//解析 刚才输出的二维码图片 到 outPath下并命名为application-copy.yml
System.out.println(GenerateUtil.decodeQrCodeToPath(image, outPath, "application-copy.yml"));
}
附完整类
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
public class GenerateUtil {
private final static int DEFAULT_WIDTH = 400;
private final static int DEFAULT_HEIGHT = 400;
private final static String DEFAULT_FORMAT = "PNG";
private final static String DEFAULT_CHARSET = "UTF-8";
public static String generateQRImage(String content,
int width,
int height,
String outPath,
String format,
String charset,
String imageName) throws Exception {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map encodeHintTypeStringMap = new HashMap<>();
encodeHintTypeStringMap.put(EncodeHintType.CHARACTER_SET, charset);
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE,
width, height, encodeHintTypeStringMap);
File file = new File(outPath);
mkdirs(file);
String fullPath = outPath + File.separator + imageName;
Path target = FileSystems.getDefault().getPath(fullPath);
MatrixToImageWriter.writeToPath(bitMatrix, format, target);
return fullPath;
}
private static void mkdirs(File file) {
if (!file.exists() || !file.isDirectory()) {
file.mkdirs();
}
}
public static String generateQRImage(String content, String outPath, String format, String imageName)
throws Exception {
return generateQRImage(content, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, format, DEFAULT_CHARSET, imageName);
}
public static String generateQRImage(String content, String outPath, String imageName) throws Exception {
return generateQRImage(content, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, DEFAULT_FORMAT, DEFAULT_CHARSET, imageName);
}
public static String generateQRImage(String content, String outPath) throws Exception {
// 默认文件名
String defaultName = "qrCode" + System.currentTimeMillis() + ".png";
return generateQRImage(content, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, DEFAULT_FORMAT, DEFAULT_CHARSET, defaultName);
}
public static String generateQRImage(File targetFile,
int width,
int height,
String outPath,
String format,
String charset,
String imageName) throws Exception {
//读取目标文件的内容,实际文件如太大生成的二维码就黑压压一片根本没眼看
String content = Files.readString(targetFile.toPath(), Charset.forName(DEFAULT_CHARSET));
QRCodeWriter qrCodeWriter = new QRCodeWriter();
File file = new File(outPath);
mkdirs(file);
Map encodeHintTypeStringMap = new HashMap<>();
encodeHintTypeStringMap.put(EncodeHintType.CHARACTER_SET, charset);
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE,
width, height, encodeHintTypeStringMap);
String fullPath = outPath + File.separator + imageName;
Path target = FileSystems.getDefault().getPath(fullPath);
MatrixToImageWriter.writeToPath(bitMatrix, format, target);
return fullPath;
}
public static String generateQRImage(File targetFile, String outPath, String format, String imageName)
throws Exception {
return generateQRImage(targetFile, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, format, DEFAULT_CHARSET, imageName);
}
public static String generateQRImage(File targetFile, String outPath, String imageName) throws Exception {
return generateQRImage(targetFile, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, DEFAULT_FORMAT, DEFAULT_CHARSET, imageName);
}
public static String generateQRImage(File targetFile, String outPath) throws Exception {
// 默认文件名
String defaultName = targetFile.getName() + ".png";
return generateQRImage(targetFile, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, DEFAULT_FORMAT, DEFAULT_CHARSET, defaultName);
}
public static String generateQRImageByFiles(List targetFiles, String outPath) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
for (File item : targetFiles) {
// 默认文件名
String defaultName = item.getName() + ".png";
String re = generateQRImage(item, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, DEFAULT_FORMAT, DEFAULT_CHARSET, defaultName);
stringBuilder.append(re).append("n");
}
return stringBuilder.toString();
}
public static String generateQRImageByFilePaths(List targetFilesPaths, String outPath) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
for (String item : targetFilesPaths) {
File file = new File(item);
// 默认文件名
String defaultName = file.getName() + ".png";
String re = generateQRImage(file, DEFAULT_WIDTH, DEFAULT_HEIGHT,
outPath, DEFAULT_FORMAT, DEFAULT_CHARSET, defaultName);
stringBuilder.append(re).append("n");
}
return stringBuilder.toString();
}
public static String decodeQrCode(String imgPath) throws Exception {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imgPath));
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
HashMap decodeHints = new HashMap<>(16);
decodeHints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHARSET);
Result result = new MultiFormatReader().decode(bitmap, decodeHints);
return result.getText();
}
public static String decodeQrCodeToPath(String imgPath, String targetPath, String fileName) throws Exception {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imgPath));
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarizer);
HashMap decodeHints = new HashMap<>(16);
decodeHints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHARSET);
Result result = new MultiFormatReader().decode(bitmap, decodeHints);
String text = result.getText();
//将文件以指定名字生成到指定文件夹下
File dir = new File(targetPath);
mkdirs(dir);
File targetFile = new File(targetPath + File.separator + fileName);
if (!targetFile.exists()) {
targetFile.createNewFile();
}
FileWriter fileWriter = new FileWriter(targetFile);
fileWriter.write(text);
fileWriter.flush();
fileWriter.close();
return targetFile.getAbsolutePath();
}
}