利用itext5、zxing、QRCore制作pdf、二维码图片插入pdf,并解析pdf中的二维码信息,手机可以实现扫描获取二维码的信息,并进行验证你的解析是否正确。
先是生成二维码图片并插入pdf中
package com.parseImage;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class CreateBarcodePdf {
public static void main(String... args) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new
//pdf输出地址
FileOutputStream("./src/test/resources/scanner.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
BarcodeQRCode barcodeQRCode = new BarcodeQRCode("http://memorynotfound.com", 1000, 1000, null);
Image codeQrImage = barcodeQRCode.getImage();
codeQrImage.scaleAbsolute(100, 100);
document.add(codeQrImage);
document.close();
}
}
效果图
抽取pdf中二维码图片
package com.parseImage;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
/**
-
Extracts images from a PDF file.
*/
public class ExtractImages {/**
- PDF to extract images from
*/
//pdf路径
public static final String SOURCE_PDF = “./src/test/resources/scanner.pdf”;
/**
-
Parses a PDF and extracts all the images.
-
@param filename the source PDF
-
@param destination the directory to save images
*/
public void extractImages(String filename, String destination)
throws IOException, DocumentException {
System.out.println("Processing PDF at " + filename);
System.out.println("Saving images to " + destination);PdfReader reader = new PdfReader(filename);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
ImageRenderListener listener = new ImageRenderListener(destination + “/Img”);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
parser.processContent(i, listener);
}
reader.close();
}
/**</
- PDF to extract images from