package com.example.jinshui.test;import jp.sourceforge.qrcode.QRCodeDecoder;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;//解析全部文件夹下的二维码public class FileTest { public static void main(String[] args) throws IOException { String path="C:\\Users\\huang\\Documents\\WeChat Files"; File file=new File(path); File[] fileList = file.listFiles(); System.out.println("该目录下对象个数:"+fileList.length); for (int i = 0; i < fileList.length; i++) { if (fileList[i].isFile()) { //文件的完整路径和文件名 System.out.println("文件:"+fileList[i]); //读取 BufferedImage bufferedImage = ImageIO.read(fileList[i]); //QRCode解码器 QRCodeDecoder codeDecoder = new QRCodeDecoder(); /** *codeDecoder.decode(new MyQRCodeImage()) *这里需要实现QRCodeImage接口,然后解析出二维码的信息 */ //通过解析二维码获得信息 String result = new String(codeDecoder.decode(new ImageQRCode(bufferedImage)), "utf-8"); System.out.println(result); } if (fileList[i].isDirectory()) { System.out.println("文件夹:"+fileList[i]); } } }}
package com.example.jinshui.test;import jp.sourceforge.qrcode.data.QRCodeImage;import java.awt.image.BufferedImage;/** * 调用QRCodeImage接口, * 设置解码的图片信息 * 告诉codeDecoder.decode()将要解析的图片类型 */public class ImageQRCode implements QRCodeImage{ BufferedImage bufferedImage; public ImageQRCode(BufferedImage bufferedImage){ this.bufferedImage=bufferedImage; } //宽 @Override public int getWidth() { return bufferedImage.getWidth(); } //高 @Override public int getHeight() { return bufferedImage.getHeight(); } //像素还是颜色 @Override public int getPixel(int i, int j) { return bufferedImage.getRGB(i,j); }}
package com.example.jinshui.test;import jp.sourceforge.qrcode.QRCodeDecoder;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;public class OneReadQRCode { //单张图片解析 public static void main(String[] args) throws IOException { //图片路径 File file = new File("C:\\Users\\huang\\Documents\\WeChat Files\\二维码文件-01.png"); //读取图片到缓冲区 BufferedImage bufferedImage = ImageIO.read(file); //QRCode解码器 QRCodeDecoder codeDecoder = new QRCodeDecoder(); /** *codeDecoder.decode(new MyQRCodeImage()) *这里需要实现QRCodeImage接口,移步最后一段代码 */ //通过解析二维码获得信息 String result = new String(codeDecoder.decode(new ImageQRCode(bufferedImage)), "utf-8"); System.out.println(result); }}
QRCode包下载地址:
主要为了记录和方便以后查找。