package tupian; /* import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Base64; public class ToAnime { private JFrame frame; private JLabel selectImageLabel; private String selectedImagePath = "路径1"; private JLabel resultImageLabel; public ToAnime() { frame = new JFrame("人物动漫化"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); JPanel selectImagePanel = new JPanel(); selectImagePanel.setLayout(new BorderLayout()); selectImageLabel = new JLabel(); selectImageLabel.setIcon(new ImageIcon(selectedImagePath)); selectImagePanel.add(selectImageLabel, BorderLayout.CENTER); JButton selectImageButton = new JButton("选择图片"); selectImageButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(frame); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); selectedImagePath = selectedFile.getAbsolutePath(); selectImageLabel.setIcon(new ImageIcon(selectedImagePath)); } } }); selectImagePanel.add(selectImageButton, BorderLayout.SOUTH); JPanel resultImagePanel = new JPanel(); resultImagePanel.setLayout(new BorderLayout()); resultImageLabel = new JLabel(); resultImageLabel.setIcon(new ImageIcon(selectedImagePath)); resultImagePanel.add(resultImageLabel, BorderLayout.CENTER); JButton convertButton = new JButton("开始转化"); convertButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (selectedImagePath.equals("路径1")) { JOptionPane.showMessageDialog(frame, "未选择图片"); } else { // 调用图片处理方法,返回处理后的Base64编码 String processedImage = processImage(selectedImagePath); resultImageLabel.setIcon(new ImageIcon(processedImage)); } } }); resultImagePanel.add(convertButton, BorderLayout.SOUTH); frame.add(selectImagePanel, BorderLayout.WEST); frame.add(resultImagePanel, BorderLayout.CENTER); frame.setSize(800, 600); frame.setVisible(true); } private String processImage(String imagePath) { // 处理图片的逻辑,这里只是示例,你可以根据具体需求进行实现 try { File file = new File(imagePath); FileInputStream fileInputStream = new FileInputStream(file); byte[] buffer = new byte[(int) file.length()]; fileInputStream.read(buffer); String base64Image = Base64.getEncoder().encodeToString(buffer); fileInputStream.close(); return base64Image; } catch (IOException exception) { exception.printStackTrace(); } return null; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new ToAnime(); } }); } } */ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Base64; import javax.swing.*; public class ToAnime { private JFrame frame; private JDialog helpDialog; private String selectedImagePath = "images/pic.jpg"; public ToAnime() { frame = new JFrame(); GUI gui = new GUI(frame); JPanel panel = new JPanel(); panel.setLayout(null); JLabel titleLabel = new JLabel("人物动漫化"); titleLabel.setBounds(0, 10, 800, 30); titleLabel.setFont(new Font("宋体",Font.BOLD,30)); titleLabel.setHorizontalAlignment(SwingConstants.CENTER); panel.add(titleLabel); // 左边选择图片区域 ImageIcon imageIcon = new ImageIcon(selectedImagePath); Image image1 = imageIcon.getImage(); Image image2 = image1.getScaledInstance(200,200,Image.SCALE_FAST); JLabel imageSelectionLabel = new JLabel(new ImageIcon(image2)); imageSelectionLabel.setBounds(100, 50, 200, 200); imageSelectionLabel.setHorizontalAlignment(SwingConstants.CENTER); panel.add(imageSelectionLabel); JButton chooseImageButton = new JButton("选择图片"); chooseImageButton.setBounds(150, 280, 100, 30); panel.add(chooseImageButton); chooseImageButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(frame); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); selectedImagePath = selectedFile.getAbsolutePath(); imageSelectionLabel.setIcon(new ImageIcon(selectedImagePath)); } } }); // 右边展示图片区域 JLabel imageDisplayLabel = new JLabel(); imageDisplayLabel.setBounds(400, 50, 200, 200); imageDisplayLabel.setHorizontalAlignment(SwingConstants.CENTER); panel.add(imageDisplayLabel); JButton startConversionButton = new JButton("开始转化"); startConversionButton.setBounds(450, 280, 100, 30); panel.add(startConversionButton); startConversionButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (selectedImagePath.equals("images/pic.jpg")) { JOptionPane.showMessageDialog(frame, "未选择图片"); } else { // 调用图片处理方法,返回处理后的Base64编码 Baidu baidu = new Baidu(); // String processedImage = processImage(selectedImagePath); String result = baidu.selfie_anime(selectedImagePath); imageDisplayLabel.setIcon(new ImageIcon(result)); } } }); frame.getContentPane().add(panel); // “帮助” - “如何使用” 弹出提示框 helpDialog = new JDialog(frame, "帮助", true); helpDialog.setSize(300, 200); helpDialog.setLayout(null); JLabel helpTextLabel = new JLabel("帮助"); helpTextLabel.setBounds(0, 0, 300, 30); helpTextLabel.setHorizontalAlignment(SwingConstants.CENTER); helpDialog.add(helpTextLabel); helpDialog.setLocationRelativeTo(null); JButton okButton = new JButton("确定"); okButton.setBounds(100, 100, 100, 30); helpDialog.add(okButton); ActionListener helpActionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { helpDialog.setVisible(true); } }; okButton.addActionListener(helpActionListener); frame.setVisible(true); } private String processImage(String imagePath) { // 处理图片的逻辑,这里只是示例,你可以根据具体需求进行实现 try { File file = new File(imagePath); FileInputStream fileInputStream = new FileInputStream(file); byte[] buffer = new byte[(int) file.length()]; fileInputStream.read(buffer); String base64Image = Base64.getEncoder().encodeToString(buffer); fileInputStream.close(); return base64Image; } catch (IOException exception) { exception.printStackTrace(); } return null; } private ImageIcon To200 (ImageIcon a){ Image image1 = a.getImage(); Image image2 = image1.getScaledInstance(200,200,Image.SCALE_FAST); ImageIcon rs = new ImageIcon(image2); return rs; } public static void main(String[] args) { ToAnime app = new ToAnime(); } }
标签:总结,23.12,java,每日,add,selectedImagePath,new,import,frame From: https://www.cnblogs.com/laobing6214/p/17883722.html