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();
                }
            }
        }
    }

 

 

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