java 图片解析

Zxing读取图片的条码

注意点:

     PDF文件转义为jpg

    jpg文件的像素为:1080(自己测试得到)

   1080解析错误,就可以截图处理。基本都可以解析成功

第一步:导包:pom.xml

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
        </dependency>

第二步: 解决代码

    /**
     * @param imgPath
     * @return String
     */
    public static String decode(String imgPath) {
        try {
            BufferedImage image = ImageIO.read(new File(imgPath));
            if (image == null) {
                return "b";
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
 
            Map<DecodeHintType, Object> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET, "GBK");
            hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
 
            Result result = new MultiFormatReader().decode(bitmap, hints);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "a";
    }

测试代码:

   读取指定目录下的jpg文件

   读取jpg文件的目录:C:\\Users\\admin\\Desktop\\D24\\jpg\\screenshots\\jpg3

  解析后,解决的结果存在txt文档中目录:C:\\Users\\admin\\Desktop\\D24\\txt\\screenshots\\

public static void main(String[] args) throws IOException {        
        List<String> fileNames1 = new ArrayList<String>();
       findFileList(new File("C:\\Users\\admin\\Desktop\\D24\\jpg\\screenshots\\jpg3"),fileNames1 );
       for (String value :  fileNames1) {
            int begin= value.indexOf("jpg3");
           int last=value.length();
           File file = new File("C:\\Users\\admin\\Desktop\\D24\\txt\\screenshots\\"+value.substring(begin,last)+".txt");
           String imageBarcode = decode(value);
            FileParsingWriter(imageBarcode,file);
         }
      }

 

/**
     * 读取目录下的所有文件
     * 
     * @param dir
     *            目录
     * @param fileNames
     *            保存文件名的集合
     * @return
     */
    public static void findFileList(File dir, List<String> fileNames) {
        if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录
            return;
        }
        String[] files = dir.list();// 读取目录下的所有目录文件信息
        for (int i = 0; i < files.length; i++) {// 循环,添加文件名或回调自身
            File file = new File(dir, files[i]);
            if (file.isFile()) {// 如果文件
                fileNames.add(dir + "\\" + file.getName());// 添加文件全路径名
            } else {// 如果是目录
                findFileList(file, fileNames);// 回调自身继续查询
            }
        }
    }

public static void FileParsingWriter(String imageBarcode,File file) {
        FileWriter fw = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file);
            fw.write(imageBarcode.toUpperCase()+"|");//向文件中写内容
            fw.flush();
            System.out.println("写数据成功!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章