基于百度API的图像处理实现

发布时间 2023-12-26 19:50:28作者: 突破铁皮

软件构造的小实验,现给出源码造福未来学弟

依赖

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.1</version> <!-- 请替换为最新版本 -->
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160810</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/com.github.insubstantial/substance -->
<dependency>
     <groupId>com.github.insubstantial</groupId>
     <artifactId>substance</artifactId>
     <version>7.3</version>
</dependency>

图像功能

图片清晰度增强
package com.std.www.homework.work2;

import okhttp3.*;
import org.json.JSONObject;

import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class PictureClear {
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static String getPicture(String path) throws IOException{
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "image="+getFileContentAsBase64(path,true));
        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 res= response.body().string();
        return res;
    }
    /**
     * 获取文件base64编码
     *
     * @param path      文件路径
     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    private static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    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");
    }

}
人像动漫化
 package com.std.www.homework.work2;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class PictureAnime {
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static String getPicture(String path) throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "image="+getFileContentAsBase64(path,true));
        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 res= response.body().string();
        return res;
    }
    /**
     * 获取文件base64编码
     *
     * @param path      文件路径
     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    private static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    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");
    }
}

 

黑白图像上色
package com.std.www.homework.work2;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class PictureColourize {
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static String getPicture(String path) throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "image="+getFileContentAsBase64(path,true));
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/colourize?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 res= response.body().string();
        return res;
    }
    /**
     * 获取文件base64编码
     *
     * @param path      文件路径
     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    private static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    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");
    }

}
图像色彩增强
 package com.std.www.homework.work2;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class PictureColorEnhance {
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static String getPicture(String path) throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "image="+getFileContentAsBase64(path,true));
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/color_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 res= response.body().string();
        return res;
    }
    /**
     * 获取文件base64编码
     *
     * @param path      文件路径
     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    private static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    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");
    }

}
图像对比度增强
package com.std.www.homework.work2;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class PictureContrastEnhance {
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static String getPicture(String path) throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "image="+getFileContentAsBase64(path,true));
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/contrast_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 res= response.body().string();
        return res;
    }
    /**
     * 获取文件base64编码
     *
     * @param path      文件路径
     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    private static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    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");
    }

}
图像无损放大
 package com.std.www.homework.work2;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class PictureQualityEnhance {
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static String getPicture(String path) throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "image="+getFileContentAsBase64(path,true));
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/image_quality_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 res= response.body().string();
        return res;
    }
    /**
     * 获取文件base64编码
     *
     * @param path      文件路径
     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    private static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    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");
    }

}
图像去雾
package com.std.www.homework.work2;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class PictureDehaze {
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static String getPicture(String path) throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "image="+getFileContentAsBase64(path,true));
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/dehaze?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 res= response.body().string();
        return res;
    }
    /**
     * 获取文件base64编码
     *
     * @param path      文件路径
     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
     * @return base64编码信息,不带文件头
     * @throws IOException IO异常
     */
    private static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    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");
    }

}

功能调用模块

package com.std.www.homework.work2;

import java.io.IOException;

public class Picture {

    public static String pictureClear(String path) throws IOException {
        return PictureClear.getPicture(path);
    }
    public static String pictureAnime(String path) throws IOException {
        return PictureAnime.getPicture(path);
    }
    public static String pictureColourize(String path) throws IOException {
        return PictureColourize.getPicture(path);
    }
    public static String pictureColorEnhance(String path) throws IOException {
        return PictureColorEnhance.getPicture(path);
    }
    public static String pictureContrastEnhance(String path) throws IOException {
        return PictureContrastEnhance.getPicture(path);
    }
    public static String pictureQualityEnhance(String path) throws IOException {
        return PictureQualityEnhance.getPicture(path);
    }
    public static String pictureDehaze(String path) throws IOException {
        return PictureDehaze.getPicture(path);
    }
}

GUI界面模块

package com.std.www.homework.work2;

import org.json.JSONObject;
import org.pushingpixels.substance.api.skin.SubstanceGeminiLookAndFeel;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;

public class PictureGUI {
    private static String imgPath;
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("图像转换器");
        frame.setSize(1200, 900);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Font font = new Font("宋体", Font.BOLD, 25);
        JButton selectButton=new JButton("选中图片");
        selectButton.setFont(font);
        selectButton.setBounds(20,50,250,50);
        JButton jButton1=new JButton("图像清晰度增强");
        jButton1.setFont(font);
        jButton1.setBounds(300,50,250,50);
        JButton jButton2=new JButton("人像动漫化");
        jButton2.setFont(font);
        jButton2.setBounds(580,50,250,50);
        JButton jButton3=new JButton("黑白图像上色");
        jButton3.setFont(font);
        jButton3.setBounds(860,50,250,50);
        JButton jButton4=new JButton("图像色彩增强");
        jButton4.setFont(font);
        jButton4.setBounds(20,150,250,50);
        JButton jButton5=new JButton("图像对比度增强");
        jButton5.setFont(font);
        jButton5.setBounds(300,150,250,50);
        JButton jButton6=new JButton("图像无损放大");
        jButton6.setFont(font);
        jButton6.setBounds(580,150,250,50);
        JButton jButton7=new JButton("图像去雾");
        jButton7.setFont(font);
        jButton7.setBounds(860,150,250,50);

        JPanel sourcePanel = createStyledPanel(100, 300, 400, 400, "原图片");
        JPanel resPanel = createStyledPanel(700, 300, 400, 400, "结果图片");
        selectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setCurrentDirectory(new File("D:\\JavaProject\\JuniorProject\\Project1\\src\\main\\data\\Img"));
                int result = fileChooser.showOpenDialog(null);

                if (result == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    imgPath=selectedFile.getAbsolutePath();
                    displayImage(selectedFile, sourcePanel);
                }
            }
        });
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(imgPath==null){
                        JOptionPane.showMessageDialog(frame, "请选择图片");
                        return;
                    }
                    String res=Picture.pictureClear(imgPath);
                    JSONObject jsonObject = new JSONObject(res);
                    String base64Image =jsonObject.getString("image");
                    byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                    Path outputPath = Paths.get("Project1/src/main/data/Img/清晰度增强后的图片.png");
                    Files.write(outputPath, imageBytes, StandardOpenOption.CREATE);
                    JOptionPane.showMessageDialog(frame, "图像保存成功,文件路径:" + outputPath);
                    displayImage(new File(outputPath.toUri()),resPanel);

                }catch (Exception ep){
                    ep.printStackTrace();
                }
            }
        });
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(imgPath==null){
                        JOptionPane.showMessageDialog(frame, "请选择图片");
                        return;
                    }
                    String res=Picture.pictureAnime(imgPath);
                    JSONObject jsonObject = new JSONObject(res);
                    String base64Image =jsonObject.getString("image");
                    byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                    Path outputPath = Paths.get("Project1/src/main/data/Img/动漫化后的图片.png");
                    Files.write(outputPath, imageBytes, StandardOpenOption.CREATE);
                    JOptionPane.showMessageDialog(frame, "图像保存成功,文件路径:" + outputPath);
                    displayImage(new File(outputPath.toUri()),resPanel);

                }catch (Exception ep){
                    ep.printStackTrace();
                }
            }
        });
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(imgPath==null){
                        JOptionPane.showMessageDialog(frame, "请选择图片");
                        return;
                    }
                    String res=Picture.pictureColourize(imgPath);
                    JSONObject jsonObject = new JSONObject(res);
                    String base64Image =jsonObject.getString("image");
                    byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                    Path outputPath = Paths.get("Project1/src/main/data/Img/上色后的图片.png");
                    Files.write(outputPath, imageBytes, StandardOpenOption.CREATE);
                    JOptionPane.showMessageDialog(frame, "图像保存成功,文件路径:" + outputPath);
                    displayImage(new File(outputPath.toUri()),resPanel);

                }catch (Exception ep){
                    ep.printStackTrace();
                }
            }
        });

        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(imgPath==null){
                        JOptionPane.showMessageDialog(frame, "请选择图片");
                        return;
                    }
                    String res=Picture.pictureColorEnhance(imgPath);
                    JSONObject jsonObject = new JSONObject(res);
                    String base64Image =jsonObject.getString("image");
                    byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                    Path outputPath = Paths.get("Project1/src/main/data/Img/色彩增强后的图片.png");
                    Files.write(outputPath, imageBytes, StandardOpenOption.CREATE);
                    JOptionPane.showMessageDialog(frame, "图像保存成功,文件路径:" + outputPath);
                    displayImage(new File(outputPath.toUri()),resPanel);

                }catch (Exception ep){
                    ep.printStackTrace();
                }
            }
        });

        jButton5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(imgPath==null){
                        JOptionPane.showMessageDialog(frame, "请选择图片");
                        return;
                    }
                    String res=Picture.pictureContrastEnhance(imgPath);
                    JSONObject jsonObject = new JSONObject(res);
                    String base64Image =jsonObject.getString("image");
                    byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                    Path outputPath = Paths.get("Project1/src/main/data/Img/对比度增强后的图片.png");
                    Files.write(outputPath, imageBytes, StandardOpenOption.CREATE);
                    JOptionPane.showMessageDialog(frame, "图像保存成功,文件路径:" + outputPath);
                    displayImage(new File(outputPath.toUri()),resPanel);

                }catch (Exception ep){
                    ep.printStackTrace();
                }
            }
        });

        jButton6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(imgPath==null){
                        JOptionPane.showMessageDialog(frame, "请选择图片");
                        return;
                    }
                    String res=Picture.pictureQualityEnhance(imgPath);
                    JSONObject jsonObject = new JSONObject(res);
                    String base64Image =jsonObject.getString("image");
                    byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                    Path outputPath = Paths.get("Project1/src/main/data/Img/放大后的图片.png");
                    Files.write(outputPath, imageBytes, StandardOpenOption.CREATE);
                    JOptionPane.showMessageDialog(frame, "图像保存成功,文件路径:" + outputPath);
                    displayImage(new File(outputPath.toUri()),resPanel);

                }catch (Exception ep){
                    ep.printStackTrace();
                }
            }
        });

        jButton7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(imgPath==null){
                        JOptionPane.showMessageDialog(frame, "请选择图片");
                        return;
                    }
                    String res=Picture.pictureDehaze(imgPath);
                    JSONObject jsonObject = new JSONObject(res);
                    String base64Image =jsonObject.getString("image");
                    byte[] imageBytes = Base64.getDecoder().decode(base64Image);
                    Path outputPath = Paths.get("Project1/src/main/data/Img/去雾后的图片.png");
                    Files.write(outputPath, imageBytes, StandardOpenOption.CREATE);
                    JOptionPane.showMessageDialog(frame, "图像保存成功,文件路径:" + outputPath);
                    displayImage(new File(outputPath.toUri()),resPanel);

                }catch (Exception ep){
                    ep.printStackTrace();
                }
            }
        });



        frame.add(selectButton);
        frame.add(jButton1);
        frame.add(jButton2);
        frame.add(jButton3);
        frame.add(jButton4);
        frame.add(jButton5);
        frame.add(jButton6);
        frame.add(jButton7);
        frame.add(sourcePanel);
        frame.add(resPanel);
        frame.setVisible(true);

    }

    private static JPanel createStyledPanel(int x, int y, int width, int height, String title) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBounds(x, y, width, height);
        panel.setBorder(new LineBorder(Color.BLUE));
        panel.setBackground(Color.LIGHT_GRAY);

        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("宋体", Font.BOLD, 20));
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }
    private static void displayImage(File imageFile, JPanel panel) {
        ImageIcon icon = new ImageIcon(imageFile.getAbsolutePath());
        Image image = icon.getImage();
        Image scaledImage = image.getScaledInstance(panel.getWidth(), panel.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon scaledIcon = new ImageIcon(scaledImage);
        JLabel label = new JLabel(scaledIcon);
        panel.removeAll();
        panel.add(label);
        panel.revalidate();
        panel.repaint();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(new SubstanceGeminiLookAndFeel());
                createAndShowGUI();
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
        });
        //        javax.swing.SwingUtilities.invokeLater(new Runnable() {
//            public void run() {
//                createAndShowGUI();
//            }
//        });
    }
}