解析用了谷歌的zxing,pom:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
工具类:
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.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.util.*;
@Slf4j
public class QRCodeUtil {
private static final Logger logger = LoggerFactory.getLogger(QRCodeUtil.class);
public static List<File> searchFiles(File folder, final String keyword) {
List<File> result = new ArrayList<File>();
if (folder.isFile())
result.add(folder);
File[] subFolders = folder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
if (file.getName().toLowerCase().endsWith(keyword)) {
return true;
}
return false;
}
});
if (subFolders != null) {
for (File file : subFolders) {
if (file.isFile()) {
// 如果是文件则将文件添加到结果列表中
result.add(file);
} else {
// 如果是文件夹,则递归调用本方法,然后把所有的文件加到结果列表中
result.addAll(searchFiles(file, keyword));
}
}
}
return result;
}
/**
* 创建二维码
* @param url
* @param fileName
* @return
* @throws IOException
*/
// public static String createQRCode(String url,String fileDirectory,String fileName) throws IOException {
// int width = 500;
// int height = 500;
// String format = "png";
// Hashtable hints = new Hashtable();
// hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
// hints.put(EncodeHintType.MARGIN, 2);
// try {
// BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
//
// File fileDir = new File(fileDirectory);
// FileToolUtil.judeDirExists(fileDir);
// Path file = new File(fileDirectory,fileName+".png").toPath();//在D盘生成二维码图片
// MatrixToImageWriter.writeToPath(bitMatrix, format, file);
// BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
// ByteArrayOutputStream os = new ByteArrayOutputStream();//新建流。
// ImageIO.write(image, format, os);//利用ImageIO类提供的write方法,将bi以png图片的数据模式写入流。
// byte b[] = os.toByteArray();//从流中获取数据数组。
// String str = new BASE64Encoder().encode(b);
// IOUtils.closeQuietly(os);
// return str;
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }finally {
// //DeleteFileUtil.delete(fileDirectory);
// }
// return "NULL";
// }
/**
* 解析出二维码的url
* @param file
* @param fileDirectory
* @throws NotFoundException
*/
public static void anlaysisQRCode(File file,String fileDirectory) throws NotFoundException {
MultiFormatReader formatReader=new MultiFormatReader();
BufferedImage image=null;
try {
image = ImageIO.read(file);
BinaryBitmap binaryBitmap =new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Hashtable hints=new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
Result result=formatReader.decode(binaryBitmap,hints);
logger.info("解析结果:"+result.toString());
logger.info("解析格式:"+result.getBarcodeFormat());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 解析二维码图片
* @param filePath 图片路径
*/
public static String decodeQR(String filePath) {
if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
return "二维码图片不存在!";
}
BufferedImage image;
Result result = null;
try {
image = ImageIO.read(new File(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
result = new MultiFormatReader().decode(binaryBitmap, hints);
System.out.println("图片中内容: " + result.getText());
System.out.println("图片中格式: " + result.getBarcodeFormat());
} catch (Exception e) {
e.printStackTrace();
}
return result.getText();
}
// public static void main(String[] args) {
// String filePath = "D://42b0b8a4db194e8cbc048dd7c1b86f0e.png";
// decodeQR(filePath);
// }
}
main方法测试:
public static void main(String[] args) {
List<File> files = searchFiles(new File("F:\\code"), "");
System.out.println("共找到:" + files.size() + "个文件");
ArrayList<String> list = new ArrayList<>();
for (File file : files) {
list.add(file.getAbsolutePath());
// System.out.println(file.getAbsolutePath());
}
for (String code : list) {
String decodeQR = decodeQR(code);
System.out.println("我看一下"+decodeQR);
}
}
效果: