Java教程

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

本文主要是介绍文件二维码识别工具类 - java,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

    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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!