1、需要引入下面的pom坐标如下
2.完整代码如下
import com.github.binarywang.utils.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import java.awt.image.BufferedImage;
import java.util.HashMap;
/**
* 获取pdf中固定位置图片的二维码,然后解析
*/
public class GetCoordinatesOfImage {
public static void main(String[] args) throws Exception {
PdfDocument doc = new PdfDocument("D://chukoupingzheng.pdf");
PdfPageBase page = doc.getPages().get(0);
BufferedImage[] bufferedImages = page.extractImages();
//获取最后一张图片
BufferedImage outImage = bufferedImages[bufferedImages.length - 1];
MultiFormatReader formatReader = new MultiFormatReader();
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(outImage)));
// 定义二维码的参数
HashMap hints = new HashMap();
//设置编码字符集
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//处理读取结果
Result result = formatReader.decode(binaryBitmap, hints);
System.out.println("解析结果:" + result.toString());
System.out.println("二维码格式类型:" + result.getBarcodeFormat());
System.out.println("result:" + result.toString().replace("*",""));
}
}
3.运行结果如下
4.示例pdf部分,标注图片索引
测试代码解析的是最后一张二维码图片,可根据位置自行调整