12.8每日总结
今天接着进行软件构造实验二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | package Vfx; import okhttp3.*; import org.json.JSONObject; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Base64; import java.net.URLEncoder; public class Picture { private static final String API_KEY = "azh8NUHqQ6xfYxsycMj5064k" ; private static final String SECRET_KEY = "RCrBgDCGT0gA0H0XuHHf8POXX5ExAi4G" ; private static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build(); private static byte [] originalImageData; private static JLabel processedLabel; // 将processedLabel定义为类级别的变量 public static void main(String[] args) { SwingUtilities.invokeLater(() -> createAndShowGUI()); } private static void createAndShowGUI() { JFrame frame = new JFrame( "图像处理 GUI" ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize( 800 , 800 ); JPanel panel = new JPanel( new GridBagLayout()); JLabel originalLabel = new JLabel( "原始图像" ); processedLabel = new JLabel( "处理后的图像" ); JButton loadButton = new JButton( "加载图像" ); JButton executeButton = new JButton( "确定" ); // 设置按钮的首选大小 loadButton.setPreferredSize( new Dimension( 150 , 40 )); executeButton.setPreferredSize( new Dimension( 150 , 40 )); // 下拉框,用于选择处理方式 String[] options = { "增强图像" , "动漫化" }; JComboBox<String> processingOptions = new JComboBox<>(options); GridBagConstraints gbc = new GridBagConstraints(); // 添加“加载图像”按钮 gbc.gridx = 0 ; gbc.gridy = 0 ; gbc.insets = new Insets( 10 , 10 , 10 , 10 ); panel.add(loadButton, gbc); // 添加“原始图像”标签 gbc.gridx = 1 ; gbc.gridy = 0 ; gbc.insets = new Insets( 10 , 10 , 10 , 10 ); panel.add(originalLabel, gbc); // 添加下拉框 gbc.gridx = 0 ; gbc.gridy = 1 ; gbc.insets = new Insets( 10 , 10 , 10 , 10 ); panel.add(processingOptions, gbc); // 添加“确定”按钮 gbc.gridx = 1 ; gbc.gridy = 1 ; gbc.insets = new Insets( 10 , 10 , 10 , 10 ); panel.add(executeButton, gbc); // 添加“处理后的图像”标签 gbc.gridx = 1 ; gbc.gridy = 2 ; gbc.insets = new Insets( 10 , 10 , 10 , 10 ); panel.add(processedLabel, gbc); loadButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { originalImageData = readImageFile(chooseImageFile()); displayImage(originalLabel, originalImageData); } catch (IOException ex) { ex.printStackTrace(); } } }); executeButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (originalImageData != null ) { try { String selectedOption = (String) processingOptions.getSelectedItem(); if ( "增强图像" .equals(selectedOption)) { enhanceImage(); } else if ( "动漫化" .equals(selectedOption)) { cartoonizeImage(); } } catch (IOException ex) { ex.printStackTrace(); } } else { JOptionPane.showMessageDialog( null , "请先选择图像再进行处理!" ); } } }); frame.setLayout( new BorderLayout()); frame.add(panel, BorderLayout.NORTH); // 设置图片标签的固定大小 originalLabel.setPreferredSize( new Dimension( 400 , 300 )); processedLabel.setPreferredSize( new Dimension( 400 , 300 )); frame.setVisible( true ); } private static void enhanceImage() throws IOException { String originalImageBase64 = getFileContentAsBase64(originalImageData, true ); MediaType mediaType = MediaType.parse( "application/x-www-form-urlencoded" ); RequestBody body = RequestBody.create(mediaType, "image=" + originalImageBase64); Request request = new Request.Builder() .url( "https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance?access_token=" + getAccessToken()) .method( "POST" , body) .addHeader( "Content-Type" , "application/x-www-form-urlencoded" ) .addHeader( "Accept" , "application/json" ) .build(); Response response = HTTP_CLIENT.newCall(request).execute(); String processedImageBase64 = new JSONObject(response.body().string()).getString( "image" ); byte [] processedImageData = Base64.getDecoder().decode(processedImageBase64); displayImage(processedLabel, processedImageData); } private static void cartoonizeImage() throws IOException { String originalImageBase64 = getFileContentAsBase64(originalImageData, true ); MediaType mediaType = MediaType.parse( "application/x-www-form-urlencoded" ); RequestBody body = RequestBody.create(mediaType, "image=" + originalImageBase64); Request request = new Request.Builder() .url( "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + getAccessToken()) .method( "POST" , body) .addHeader( "Content-Type" , "application/x-www-form-urlencoded" ) .addHeader( "Accept" , "application/json" ) .build(); Response response = HTTP_CLIENT.newCall(request).execute(); String processedImageBase64 = new JSONObject(response.body().string()).getString( "image" ); byte [] processedImageData = Base64.getDecoder().decode(processedImageBase64); displayImage(processedLabel, processedImageData); } private static String chooseImageFile() { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog( null ); if (result == JFileChooser.APPROVE_OPTION) { return fileChooser.getSelectedFile().getPath(); } return null ; } private static String getFileContentAsBase64( byte [] imageData, boolean urlEncode) throws IOException { String base64 = Base64.getEncoder().encodeToString(imageData); if (urlEncode) { base64 = URLEncoder.encode(base64, "utf-8" ); } return base64; } private static String getAccessToken() throws IOException { MediaType mediaType = MediaType.parse( "application/x-www-form-urlencoded" ); RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY + "&client_secret=" + SECRET_KEY); Request request = new Request.Builder() .url( "https://aip.baidubce.com/oauth/2.0/token" ) .method( "POST" , body) .addHeader( "Content-Type" , "application/x-www-form-urlencoded" ) .build(); Response response = HTTP_CLIENT.newCall(request).execute(); return new JSONObject(response.body().string()).getString( "access_token" ); } private static byte [] readImageFile(String fileName) throws IOException { Path path = Paths.get(fileName); return Files.readAllBytes(path); } private static void displayImage(JLabel label, byte [] imageData) { ImageIcon icon = new ImageIcon(imageData); // 设置图片标签的图标并强制重绘 label.setIcon(icon); label.revalidate(); label.repaint(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix