swing的图像更改的这个这个额叫啥来着,就是文件??差不多
首先还是展示架构
image用来存储图片,api请求token,changecode用来存放图片转base64的代码(base64->changecode),ui放界面,mian用于启动!!,其余是百度图像的专用工具,你们申请然后使用人家的接口时就能看到
image文件夹存储图片,里边的不叫image的是基础图片,他们经过转换都叫image。
然后是引入依赖
<!--处理json--> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> <!--处理base64--> <dependency> <groupId>org.apache.directory.studio</groupId> <artifactId>org.apache.commons.codec</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.8</version> <!-- 或者使用最新版本 --> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.1.1</version> </dependency>
API(获取token)
package api; import com.google.gson.Gson; import okhttp3.*; import java.io.IOException; public class Api { static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build(); Token token=new Token(); public String Post(String base64) throws IOException { MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); // image 可以通过 getFileContentAsBase64("C:\fakepath\报名照片.jpg") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true RequestBody body = RequestBody.create(mediaType, "type=anime&image="+base64); System.out.println("格式:"+base64); Request request = new Request.Builder() .url("https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + token.getAuth()) .method("POST", body) .addHeader("Content-Type", "application/x-www-form-urlencoded") .addHeader("Accept", "application/json") .build(); Response response = HTTP_CLIENT.newCall(request).execute(); Gson gson = new Gson(); String jsonResponse = response.body().string(); Get_api yourObject = gson.fromJson(jsonResponse, Get_api.class); System.out.println(jsonResponse); return yourObject.image; } } class Get_api{ String image; String log_id; Get_api(){} }
imageToBase64.java
package changeCode; import java.io.IOException; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Base64; public class Image_Base64 { public String imageToBase64(String imagePath, boolean urlEncode) throws IOException { byte[] b = Files.readAllBytes(Paths.get(imagePath)); String base64 = Base64.getEncoder().encodeToString(b); if (urlEncode) { base64 = URLEncoder.encode(base64, "utf-8"); } return base64; } public static void base64ToImage(String base64Image) { // 解码Base64字符串并保存为图像文件 try { byte[] imageBytes = org.apache.commons.codec.binary.Base64.decodeBase64(base64Image); // 保存图像到文件 String imagePath = "src/main/image/image.jpg"; // 替换成实际的保存路径和文件名 Path path = Paths.get(imagePath); Files.write(path, imageBytes); System.out.println("Image saved successfully at: " + imagePath); } catch (Exception e) { e.printStackTrace(); } } }
Main.java
package ui; import api.Api; import changeCode.Image_Base64; import javax.swing.*; import javax.swing.filechooser.FileFilter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; public class Main { float proportion; JLabel lb; public void display(){ Font font = new Font("仿宋", Font.PLAIN, 25); JFrame jf =new JFrame(); JPanel Jp = new JPanel();//申请容器 jf.setLayout(null); jf.setSize(560, 400);//设置窗体大小 jf.setLocationRelativeTo(null);//设置窗体于屏幕中央位置 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//点击叉叉可以关闭窗体 jf.setVisible(true);//组件可见 Jp.setLayout(null); jf.setResizable(false);//窗体不可变大或者变小 jf.setContentPane(Jp);//不知道 JLabel jLabel=new JLabel("图片修改器:"); jLabel.setVisible(true); jLabel.setFont(font); jLabel.setBounds(5,5,160,40); Jp.add(jLabel); JButton jButton=new JButton("选择图片"); jButton.setFont(new Font("仿宋", Font.PLAIN, 20)); jButton.setVisible(true); jButton.setBounds(350,8,120,30); Jp.add(jButton); jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser jFileChooser=new JFileChooser(new File("src/main/image")); jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); jFileChooser.showOpenDialog(null); jFileChooser.addChoosableFileFilter(new FileFilter() { @Override public boolean accept(File f) { if(f.getName().toLowerCase().endsWith(".jpg")) { return true; } else if(f.getName().toLowerCase().endsWith(".png")) { return true; } return false; } @Override public String getDescription() { return null; } }); File chooseImage = jFileChooser.getSelectedFile(); System.out.println("src/main/image/"+chooseImage.getName()); if( chooseImage == null){ JOptionPane.showMessageDialog(null,"您已取消选择"); } else{ JOptionPane.showMessageDialog(null,"选择成功,请耐心等待"); Image_Base64 imageBase64=new Image_Base64(); try { String temp = imageBase64.imageToBase64("src/main/image/"+chooseImage.getName(),true); Api api=new Api(); imageBase64.base64ToImage(api.Post(temp)); ImageIcon last = refresh(); lb.setIcon(last); lb.setBounds((545 - (int)(300/proportion))/2, 60, last.getIconWidth(),last.getIconHeight());//设置图像的位置和大小 File file=new File("src/main/image/image.jpg"); } catch (IOException ioException) { ioException.printStackTrace(); } } } }); lb=new JLabel();//创建一个标签对象 //引入 ImageIcon last = refresh(); lb.setIcon(last); lb.setBounds((545 - (int)(300/proportion))/2, 60, last.getIconWidth(),last.getIconHeight());//设置图像的位置和大小 //要想图片整张显示填满窗体的话就要把图片的宽高改成和窗体大小一样 lb.setVisible(true); Jp.add(lb); } ImageIcon refresh(){ ImageIcon icon=new ImageIcon("src/main/image/image.jpg");//创建背景图片 //缩放 proportion= (float) icon.getIconHeight()/ (float) icon.getIconWidth(); Image newImage = icon.getImage().getScaledInstance((int) (300/proportion), 300, Image.SCALE_DEFAULT); ImageIcon last=new ImageIcon(newImage); return last; } }
mian.java
import api.Api; import api.Token; import changeCode.Image_Base64; import ui.Main; import java.io.IOException; public class mian { public static void main(String[] args) throws IOException { //Token t = new Token(); //System.out.println(t.getAuth()); Main m = new Main(); m.display(); } }