ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

文件二维码识别工具类 - java

2021-07-16 09:35:17  阅读:307  来源: 互联网

标签:java String int BufferedImage content return 二维码 new 识别


 

    public String deEncodeByPath(String path) {
        String content="";
        String fileType = path.substring(path.lastIndexOf(".")+1);
        if("pdf".equalsIgnoreCase(fileType)){
            content=decodeQrCodeFile(path.replaceAll("\\\\","/"),"pdf");
        }else{
            content=decodeQrCodeFile(path.replaceAll("\\\\","/"),"img");
        }
        return content;
    }



    public String decodeQrCodeFile(String filePath, String type) {
        String content = "";
        BufferedImage image = null;
        if ("pdf".equals(type)) {
            int[] dpiKey = { 80, 90, 100, 105,110,115, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240 };
            for (int dpi : dpiKey) {
                try {
                    image = pdf2Image(filePath, dpi);
                    content = getQrCodeResult(image);
                    return content;
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        content = getQrCodeResult(zoomInImage(image));
                        return content;
                    } catch (Exception e2) {
                    }
                    continue;
                }
            }
        } else if ("img".equals(type)) {
            try {
                image = ImageIO.read(new File(filePath));
                content = getQrCodeResult(image);
            } catch (Exception e) {
                int[][] dpiKey = { { 935, 661 }, { 1052, 744 }, { 1169, 826 }, { 1286, 909 }, { 1403, 992 },
                        { 1520, 1074 }, { 1637, 1157 }, { 1753, 1240 }, { 1870, 1322 }, { 1987, 1405 }, { 2104, 1488 },
                        { 2221, 1570 }, { 2338, 1653 }, { 2455, 1736 }, { 2572, 1818 }, { 2689, 1901 },
                        { 2806, 1984 } };
                for (int i = 0, len = dpiKey.length; i < len; i++) {
                    BufferedImage img = null;
                    try {
                        img = zoomInImage(image, dpiKey[i][0], dpiKey[i][1]);
                        content = getQrCodeResult(img);
                        return content;
                    } catch (Exception e1) {
                        try {
                            content = getQrCodeResult(zoomInImage(img));
                            return content;
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                        e1.printStackTrace();
                        continue;
                    }
                }
                e.printStackTrace();
            }

        }
        return content;
    }

    public static BufferedImage pdf2Image(String pdfFilePath, int dpi) {
        File file = new File(pdfFilePath);
        PDDocument pdDocument;
        try {
            pdDocument = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            int pages = pdDocument.getNumberOfPages();
            for (int i = 0; i < pages;i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, dpi, ImageType.RGB);
                return image;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getQrCodeResult(BufferedImage image){
        String content="";
        try {
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Hashtable<DecodeHintType,Object> hints = new Hashtable<DecodeHintType,Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);

            //hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
            Result result = new QRCodeReader().decode(binaryBitmap, hints);// 对图像进行解码
            content=result.getText();
            logger.info(content);
            //content = checkContentIsUrl(content);
        }catch (NotFoundException e) {
            e.printStackTrace();
            throw new BizException("解析失败,请重新上传!");
        } catch (ChecksumException e) {
            e.printStackTrace();
            throw new BizException("解析失败,请重新上传!");
        } catch (FormatException e) {
            e.printStackTrace();
            throw new BizException("解析失败,请重新上传!");
        }
        return content;
    }

    public BufferedImage zoomInImage(BufferedImage originalImage)
    {
        float sc = ((float)(931))/(float)originalImage.getWidth();
        int sheight =(int)( sc*originalImage.getHeight());
        BufferedImage newImage = new BufferedImage(931, sheight, originalImage.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage.getScaledInstance(931, sheight,Image.SCALE_AREA_AVERAGING), 0, 0, 931, sheight, null);
        g.dispose();
        return newImage;
    }

    public static BufferedImage zoomInImage(BufferedImage originalImage,int width,int height)
    {
        float sc = ((float)(width))/(float)originalImage.getWidth();
        int sheight =(int)( sc*originalImage.getHeight());

        BufferedImage newImage = new BufferedImage(width, height,originalImage.getType());
        Graphics2D g = newImage.createGraphics();
        g.setBackground(Color.WHITE);
        g.clearRect(0, 0, newImage.getWidth(), newImage.getHeight());
        g.drawImage(originalImage.getScaledInstance(width, sheight,Image.SCALE_AREA_AVERAGING), 0, 0, width, height, null);
        g.dispose();
        return newImage;
    }

标签:java,String,int,BufferedImage,content,return,二维码,new,识别
来源: https://www.cnblogs.com/LCJgogogo/p/15018614.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有