PDF二维码识别之JAVA实现

原理是将PDF文件转换为图片,再识别图片里的二维码

package com.bree.qrcode;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.bree.qrcode.service.impl.QrcodeServiceImpl;
import com.bree.qrcode.util.Base64Util;
import com.bree.qrcode.util.QrcodeUtil;

public class Test {

	
	/**
	 * Description: 将pdf文件转换为Base64编码
	 * @param  要转的的pdf文件
	 * @Author fuyuwei
	 * Create Date: 2015年8月3日 下午9:52:30
	 */
	public static String PDFToBase64(File file) {
	    FileInputStream fin =null;
	    BufferedInputStream bin =null;
	    ByteArrayOutputStream baos = null;
	    BufferedOutputStream bout =null;
	    try {
	        fin = new FileInputStream(file);
	        bin = new BufferedInputStream(fin);
	        baos = new ByteArrayOutputStream();
	        bout = new BufferedOutputStream(baos);
	        byte[] buffer = new byte[1024];
	        int len = bin.read(buffer);
	        while(len != -1){
	            bout.write(buffer, 0, len);
	            len = bin.read(buffer);
	        }
	        //刷新此输出流并强制写出所有缓冲的输出字节
	        bout.flush();
	        byte[] bytes = baos.toByteArray();
	        return Base64Util.encode(bytes).trim();

	    } catch (FileNotFoundException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }finally{
	        try {
	            fin.close();
	            bin.close();
	            bout.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	    return null;
	}
		
	public static void main(String[] args) {
		
		try {
			File file = new File("d://dzfp_23442000000001810329_20230711145556.pdf");
			String base64 = PDFToBase64(file);
			byte[] imageInByte = QrcodeUtil.pdf2Pic(base64);
			String text = QrcodeUtil.extractImages(imageInByte);		
			System.out.println(text);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
package com.bree.qrcode.util;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class QrcodeUtil {

	/**
	 * pdf 转 png
	 */
	public static byte[] pdfFileToImage(String base64) {
		byte[] result = null;
		try {
			PDDocument doc = PDDocument.load(Base64Util.decode(base64));
			PDFRenderer renderer = new PDFRenderer(doc);
			int pageCount = doc.getNumberOfPages();
			if (pageCount > 0) {
				BufferedImage image = renderer.renderImage(0, 2.0f);
				image.flush();
				ByteArrayOutputStream bs = new ByteArrayOutputStream();
				ImageOutputStream imOut;
				imOut = ImageIO.createImageOutputStream(bs);
				ImageIO.write(image, "png", imOut);
				result = bs.toByteArray();
				saveImage(result);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	//使用icepdf-core类库
	public static byte[] pdf2Pic(String base64) throws Exception {
		byte[] result = null;
        Document document = new Document();
        byte[] imageBytes = Base64Util.decode(base64);
        document.setByteArray(imageBytes, 0, imageBytes.length, "");
        //缩放比例
        float scale = 2.5f;
        //旋转角度
        float rotation = 0f;

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage)
                    document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                //String imgName = i + ".png";
                //System.out.println(imgName);
                File file = new File("C:\\Users\\40454\\Desktop\\pdf\\2.png");
                ImageIO.write(rendImage, "png", file);
                
            	ByteArrayOutputStream bos = new ByteArrayOutputStream();
            	ImageIO.write(rendImage, "png", bos);
            	result = bos.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            }
            image.flush();
        }
        document.dispose();
        return result;
    }
	

	public static void saveImage(byte[] result) {
		try {
			InputStream byteInputStream = null;
			byteInputStream = new ByteArrayInputStream(result);
			byteInputStream.close();
			
			String targetPath = "D:/"+UUID.randomUUID()+".png";
			File uploadFile = new File(targetPath);
			FileOutputStream fops;
			fops = new FileOutputStream(uploadFile);
			fops.write(readInputStream(byteInputStream));
			fops.flush();
			fops.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * pdf 转 png
	 */
	public static void pdfFileToImage() {
		//pdf文件
		File pdffile = new File("D:/4184126142.pdf");
		// 转成的 png 文件存储全路径及文件名
		String targetPath = "D:/4184126142.png";
			try {
				FileInputStream instream = new FileInputStream(pdffile);
				InputStream byteInputStream = null;
				try {
					PDDocument doc = PDDocument.load(instream);
					PDFRenderer renderer = new PDFRenderer(doc);
					int pageCount = doc.getNumberOfPages();
					if (pageCount > 0) {
						BufferedImage image = renderer.renderImage(0, 2.0f);
						image.flush();
						ByteArrayOutputStream bs = new ByteArrayOutputStream();
						ImageOutputStream imOut;
						imOut = ImageIO.createImageOutputStream(bs);
						ImageIO.write(image, "png", imOut);
						byteInputStream = new ByteArrayInputStream(bs.toByteArray());
						byteInputStream.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				File uploadFile = new File(targetPath);
				FileOutputStream fops;
				fops = new FileOutputStream(uploadFile);
				fops.write(readInputStream(byteInputStream));
				fops.flush();
				fops.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
	
	private static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}

	/**
	* 识别 png图片中的二维码信息
	*/
	public static String extractImages() throws Exception{
			String filename = "C:\\Users\\40454\\Desktop\\pdf\\4.jpg";
	        String returnResult = "";
	        MultiFormatReader multiFormatReader = new MultiFormatReader();
	        File file = new File(filename);
	        BufferedImage image = ImageIO.read(file);
	        
	        //int width = image.getWidth();
            //int height = image.getHeight();
            //图片缩小10%
            //width = width * 8/10;
           // height = height * 8/10;
            //BufferedImage updateImage=new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
	        
           // image = updateImage;
	        // 定义二维码参数
	        Map hints = new HashMap();
	        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
	        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
	        
	        // 获取读取二维码结果
	        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
	        Result result = null;
	        try {
	            result = multiFormatReader.decode(binaryBitmap, hints);
	            returnResult = result.getText();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return returnResult;
	 }
	
	/**
	* 识别 png图片中的二维码信息
	*/
	public static String extractImages(byte[] imageInByte) throws Exception{
	        String returnResult = "";
	   
	        try {
	        	MultiFormatReader multiFormatReader = new MultiFormatReader();
	 	        InputStream in = new ByteArrayInputStream(imageInByte);
	 	        BufferedImage image = ImageIO.read(in);
	 	        // 定义二维码参数
	 	        Map hints = new HashMap();
	 	        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

	 	        // 获取读取二维码结果
	 	        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
	        	Result result = multiFormatReader.decode(binaryBitmap, hints);
	            returnResult = result.getText();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return returnResult;
	 }

	 /**
     * 图片转为base64编码,其他文件也是通用的
     * @param file    要转换的文件
     * @return
     * @throws Exception
     */
    public static String img2BASE64Encoder(File file) throws  Exception{

        InputStream in = null;
        byte[] data = null;
        try {
            // 读取图片字节数组
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 返回Base64编码过的字节数组字符串
        return Base64Util.encode(data);

    }
    
	public static String pdfToBase64(String filePath) {
		String result = null;
		try {
			File file = new File(filePath);
			result = img2BASE64Encoder(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	 /**
     * 转换全部的pdf
     * @param fileAddress 文件地址
     * @param filename PDF文件名
     * @param type 图片类型
     */
    public static void pdf2png(String fileAddress,String filename,String type) {
        // 将pdf装图片 并且自定义图片得格式大小
        File file = new File(fileAddress+"\\"+filename+".pdf");
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
                // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
                ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
   
 
/**
     *自由确定起始页和终止页
     * @param fileAddress 文件地址
     * @param filename pdf文件名
     * @param indexOfStart 开始页  开始转换的页码,从0开始
     * @param indexOfEnd 结束页  停止转换的页码,-1为全部
     * @param type 图片类型
     */
    public static void pdf2png(String fileAddress,String filename,int indexOfStart,int indexOfEnd,String type) {
        // 将pdf装图片 并且自定义图片得格式大小
        File file = new File(fileAddress+"\\"+filename+".pdf");
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = indexOfStart; i < indexOfEnd; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
                // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
                ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	public static void pdf2Pic(String pdfPath, String path) throws Exception {
        Document document = new Document();
        document.setFile(pdfPath);
        //缩放比例
        float scale = 2.5f;
        //旋转角度
        float rotation = 0f;

        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage)
                    document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                String imgName = i + ".png";
                System.out.println(imgName);
                File file = new File(path + imgName);
                ImageIO.write(rendImage, "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
        }
        document.dispose();
    }
	
	/**
	 * 将pdf指定页转换为图片
	 * @param imagePath 保存到图片
	 * @param pdfUrl pdf地址
     * @param dpi 转换质量,越大质量越好,最好不超过200
	 * @param page 指定页码,从1 开始
	 * @param imgType jpg/png
	 * @throws IOException
	 */
	public static boolean PDFToImg(String imagePath, String pdfUrl, int dpi, int page, String imgType){
		boolean result = false;
		File imgFile = new File(imagePath);
		File file = new File(pdfUrl);
		/* dpi越大转换后越清晰,相对转换速度越慢 */
		try(FileImageOutputStream imageOutput = new FileImageOutputStream(imgFile);
				FileInputStream inputStream = new FileInputStream(file);
				PDDocument pdDocument = PDDocument.load(inputStream);) {
			PDFRenderer renderer = new PDFRenderer(pdDocument);
			int pages = pdDocument.getNumberOfPages();
			if (page <= pages && page > 0) {
				
				BufferedImage bufferedImage = renderer.renderImageWithDPI(page-1,dpi,ImageType.RGB);
				ImageIO.write(bufferedImage, imgType, imageOutput);
			}
			result = true;
		} catch (Exception e) {
    		e.printStackTrace();
		}
		return result;
	}

	
	public static void main(String[] args) {
		
		//pdfFileToImage();

		try {
			//String result = extractImages();
			//System.out.println(result);
			//String filePath = "C:\\Users\\40454\\Desktop\\pdf\\2.pdf";
			//String res = pdfToBase64(filePath);
			//System.out.println(res);
			
			//pdf2png("C:\\Users\\40454\\Desktop\\pdf","2","png");
			//pdf2Pic("C:\\Users\\40454\\Desktop\\pdf\\2.pdf","C:\\Users\\40454\\Desktop\\pdf\\");
			
			//dpi最好不超过200
			boolean r = PDFToImg("C:\\Users\\40454\\Desktop\\pdf\\2.jpg", "C:\\Users\\40454\\Desktop\\pdf\\2.pdf", 150, 1, "jpg");

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

识别结果

如有问题,请私信。

xObP8s/gudi/zrPMoaJKU7K5u7e+s7/Os8yhokpBVkHP4LnYv86zzMjn0OjSqtKyv8nS1MGqz7VRUaGjDQoNCtf31d8gUVEgNDA0NTQwMjI5

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liberty888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值