内网环境开发,内外网不互通,想拿东西进出都得申请,有时候写代码时总结了一些帮助开发的工具类,想拿出来做点笔记方便后续的使用,但是由于内网原因,只能再敲一遍代码,这属实是很难受,由于内网仓库二维码开发工具包都有,所以就有了将文件生成二维码再手机扫码拿出文件的想法。
实 *** (完整类在文末) 所需jar包<dependency>
<groupId>com.google.zxinggroupId>
<artifactId>javaseartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>com.google.zxinggroupId>
<artifactId>coreartifactId>
<version>3.4.1version>
dependency>
将字符串生成二维码到指定位置
/**
* 将文本内容生成二维码
*
* @param content 文本内容
* @param width 宽度
* @param height 高度
* @param outPath 输出路径
* @param format 格式默认PNG
* @param imageName 文件名 如 图1.png
* @return 输出图片路径
* @date 2022/4/27 21:04
*/
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<EncodeHintType, String> 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;
}
将指定文件内容生成二维码
/**
* 将指定文件内容生成二维码
*
* @param targetFile 文件
* @param width 宽度
* @param height 高度
* @param outPath 输出路径
* @param format 格式默认PNG
* @param imageName 文件名 如 图1.png
* @return 输出图片路径
* @date 2022/4/27 21:04
*/
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<EncodeHintType, String> 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<File> 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<String> 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
/**
* 读取二维码内容
*
* @param imgPath
* @return java.lang.String
* @date 2022/4/27 21:28
*/
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<DecodeHintType, Object> decodeHints = new HashMap<>(16);
decodeHints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHARSET);
Result result = new MultiFormatReader().decode(bitmap, decodeHints);
return result.getText();
}
读取二维码内容 将文件以指定名字生成到指定文件夹下
/**
* 读取二维码内容 将文件以指定名字生成到指定文件夹下
*
* @param imgPath 二维码图片的绝对路径
* @param targetPath 目标文件夹
* @param fileName 目标文件名
* @return java.lang.String
* @date 2022/4/27 22:11
*/
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<DecodeHintType, Object> 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;
/**
* 生成二维码工具类
*
* @author: kusch
* @create: 2022-04-27 20:49
**/
@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";
/**
* 将文本内容生成二维码
*
* @param content 文本内容
* @param width 宽度
* @param height 高度
* @param outPath 输出路径
* @param format 格式默认PNG
* @param imageName 文件名 如 图1.png
* @return 输出图片路径
* @date 2022/4/27 21:04
*/
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<EncodeHintType, String> 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);
}
/**
* 将指定文件内容生成二维码
*
* @param targetFile 文件
* @param width 宽度
* @param height 高度
* @param outPath 输出路径
* @param format 格式默认PNG
* @param imageName 文件名 如 图1.png
* @return 输出图片路径
* @date 2022/4/27 21:04
*/
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<EncodeHintType, String> 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<File> 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<String> 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();
}
/**
* 读取二维码内容
*
* @param imgPath
* @return java.lang.String
* @date 2022/4/27 21:28
*/
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<DecodeHintType, Object> decodeHints = new HashMap<>(16);
decodeHints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHARSET);
Result result = new MultiFormatReader().decode(bitmap, decodeHints);
return result.getText();
}
/**
* 读取二维码内容 将文件以指定名字生成到指定文件夹下
*
* @param imgPath 二维码图片的绝对路径
* @param targetPath 目标文件夹
* @param fileName 目标文件名
* @return java.lang.String
* @date 2022/4/27 22:11
*/
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<DecodeHintType, Object> 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();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)