利用javaswing+百度云图像识别接口做一个简单的动植物图像识别
import com.baidu.aip.imageclassify.AipImageClassify; import com.sun.prism.PresentableState; import org.json.JSONObject; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; public class SwingDemo1 { private JFrame mainFrame; //以下是百度云图像ID密匙 public static final String APP_ID = "****"; public static final String API_KEY = "****"; public static final String SECRET_KEY = "******"; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; private BufferedImage bufferedImage; private Image image; public SwingDemo1() { prepareGUI(); } public static void main(String[] args) { SwingDemo1 swingControlDemo = new SwingDemo1(); swingControlDemo.showEventDemo(); } private void prepareGUI() { mainFrame = new JFrame("图片检测"); mainFrame.setSize(400, 400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("", JLabel.CENTER); statusLabel = new JLabel("", JLabel.CENTER); statusLabel.setSize(350, 100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent) { System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showEventDemo() { headerLabel.setText("选择识别"); JButton okButton = new JButton("上传动物图片"); JButton submitButton = new JButton("上传植物图片"); okButton.setActionCommand("OK"); submitButton.setActionCommand("Submit"); okButton.addActionListener(new ButtonClickListener()); submitButton.addActionListener(new ButtonClickListener()); controlPanel.add(okButton); controlPanel.add(submitButton); mainFrame.setVisible(true); } private class ButtonClickListener implements ActionListener { public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("OK")) { Pattern p = Pattern.compile("\"(.*?)\""); String address=out(); AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY); client.setConnectionTimeoutInMillis(2000); JSONObject res = client.animalDetect(address, new HashMap<String, String>()); String a = findS(res); createT(address,a); } else if (command.equals("Submit")) { String address1=out(); AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY); client.setConnectionTimeoutInMillis(2000); JSONObject res1 = client.plantDetect(address1, new HashMap<String, String>()); String a1 = findS1(res1); createT(address1,a1); } } private void createT(String address, String a) { statusLabel.setSize(400,300); statusLabel.setLocation(580,200); statusLabel.setLayout(null); ImageIcon imageIcon=new ImageIcon(address); statusLabel.setIcon(imageIcon); statusLabel.setBounds(50, 50, imageIcon.getIconWidth(), imageIcon.getIconHeight()); mainFrame.add(statusLabel); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); statusLabel.setText(a); JButton okButton=new JButton(); okButton.setBounds(imageIcon.getIconWidth()/2, imageIcon.getIconHeight()+100, 100, 50); } private String findS(JSONObject res) { String result = res.getJSONArray("result").get(0).toString(); ArrayList<String> arrayList = new ArrayList<>(); Pattern p = Pattern.compile("\"(.*?)\""); Matcher m = p.matcher(result); while (m.find()) { arrayList.add(m.group().toString()); } return arrayList.get(3); } private String findS1(JSONObject res) { String result = res.getJSONArray("result").get(0).toString(); ArrayList<String> arrayList = new ArrayList<>(); Pattern p = Pattern.compile("\"(.*?)\""); Matcher m = p.matcher(result); while (m.find()) { arrayList.add(m.group().toString()); } return arrayList.get(2); } } private String out() {
//文件选择地址 JFileChooser jfchooser = new JFileChooser("C:\\Users\\zber\\Desktop"); FileNameExtensionFilter filter = new FileNameExtensionFilter("png", "jpg"); jfchooser.setFileFilter(filter); int option = jfchooser.showOpenDialog(null); if (option == JFileChooser.APPROVE_OPTION) { //说明选定了一个文件 String adress = jfchooser.getSelectedFile().getPath();//把文件地址取出给adress变量 return adress; } return null; } }