Java文字識別軟體-呼叫百度ocr實現文字識別
阿新 • • 發佈:2019-01-06
java_baidu_ocr
Java呼叫百度OCR文字識別API實現圖片文字識別軟體
專案原始碼在文末,放到了GitHub上 - https://github.com/Ymy214/java_baidu_ocr
- 識別圖一
圖一識別結果
識別圖二
圖二識別結果
識別圖三
- 圖三識別結果
軟體介面
Java原始碼:
package baiduOcr; import java.awt.BorderLayout; import java.awt.Font; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.util.HashMap; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import org.json.JSONArray; import org.json.JSONObject; import com.baidu.aip.ocr.AipOcr; @SuppressWarnings("serial") public class FileChooserOCR extends JFrame implements ActionListener { // 設定APPID/AK/SK public static final String appId = "15289864"; public static final String apiKey = "j0pj5Y7HVElkLnmn2LEXKeyO"; public static final String secretKey = "FKVbH7EBcGy4DIaqPnXcqE47eACzn2W7"; AipOcr client = new AipOcr(appId, apiKey, secretKey); JButton open, b1, b2, b3; JPanel ocrPanel; JTextArea ocrText; JScrollPane areaScroll; // 構造方法 public FileChooserOCR() { ocrPanel = new JPanel(); ocrPanel.setLayout(new BorderLayout()); open = new JButton("選擇圖片>>>文字識別"); b1 = new JButton("清空"); b2 = new JButton("複製"); b3 = new JButton("配置"); open.setFont(new Font("宋體", Font.BOLD, 18)); b3.setFont(new Font("宋體", Font.BOLD, 18)); b2.setFont(new Font("宋體", Font.BOLD, 18)); b1.setFont(new Font("宋體", Font.BOLD, 18)); //文字域的滾動條 // areaScroll = new JScrollPane(ocrText); // areaScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // areaScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //新增監聽 open.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); ocrText = new JTextArea("輸出內容。。。"); ocrText.setEditable(true); ocrText.setVisible(true); ocrText.setFont(new Font("宋體", Font.BOLD, 18)); ocrPanel.add(open, BorderLayout.NORTH); ocrPanel.add(b1, BorderLayout.EAST); ocrPanel.add(b2, BorderLayout.WEST); ocrPanel.add(b3, BorderLayout.SOUTH); ocrPanel.add(ocrText, BorderLayout.CENTER); // ocrPanel.add(areaScroll); ocrPanel.setSize(300, 300); this.add(ocrPanel); this.setBounds(400, 200, 900, 600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { ocrText.setText("已清空內容。。。"); } if(e.getSource()==b2) { String jianqieban = ocrText.getText(); setSysClipboardText(jianqieban); } if (e.getSource()==b3) { //日後實現 } if(e.getSource()==open) { System.out.println(e.getSource()); // TODO Auto-generated method stub JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); jfc.showDialog(new JLabel(), "選擇"); File file = jfc.getSelectedFile(); if (file.isDirectory()) { System.out.println("(選擇目錄) $ " + file.getAbsolutePath()); System.out.println("請選擇圖片。。。"); } else if (file.isFile()) { System.out.println("(選擇檔案) $ " + file.getAbsolutePath()); ocrText.setText("正在識別。。。"); String ocrStr = this.imgOcr(file.getAbsolutePath()); ocrText.setText(ocrStr); } System.out.println("正在識別>>>"+jfc.getSelectedFile().getName()); } } //複製到剪貼簿 public static void setSysClipboardText(String writeMe) { Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable tText = new StringSelection(writeMe); clip.setContents(tText, null); } // 主方法 public static void main(String[] args) { FileChooserOCR ocrWin = new FileChooserOCR(); ocrWin.setVisible(true); } /* * 文字識別方法 */ public String imgOcr(String imgpath) { // 傳入可選引數呼叫介面 HashMap<String, String> options = new HashMap<String, String>(); options.put("language_type", "CHN_ENG"); options.put("detect_direction", "true"); options.put("detect_language", "true"); options.put("probability", "true"); // 引數為本地路徑 JSONObject res = client.basicGeneral(imgpath, options); //解析json------------- JSONArray wordsResult = (JSONArray)res.get("words_result"); String ocrStr = "\n"; for(Object obj : wordsResult) { JSONObject jo = (JSONObject)obj; ocrStr += jo.getString("words") + "\n"; } //解析json------------- return ocrStr; // return res.toString(2); // 引數為二進位制陣列 // byte[] file = readFile("test.jpg"); // res = client.basicGeneral(file, options); // System.out.println(res.toString(2)); // 通用文字識別, 圖片引數為遠端url圖片 // JSONObject res = client.basicGeneralUrl(url, options); // System.out.println(res.toString(2)); } }
- 最後,附上完整專案GitHub地址:
- https://github.com/Ymy214/java_baidu_ocr