12.6每日总结

今天进行了软件构造的实验二,

实验二:百度图像增强与特效SDK实验(2023.12.6日完成)

    任务一:下载配置百度图像增强与特效的Java相关库及环境(占10%)。

    任务二:了解百度图像增强与特效相关功能并进行总结(占20%)。

    任务三:完成图像增强GUI相关功能代码并测试调用,要求上传自己的模糊照片进行图像增强(占30%)。

    任务四:完成图像特效GUI相关功能代码并测试调用,要求上传自己的照片进行图像特效(占30%)。

    实验总结:(占10%)

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
package Vfx;
 
import okhttp3.*;
import org.json.JSONObject;
 
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
 
public class Enhance {
 
    public static final String API_KEY = "azh8NUHqQ6xfYxsycMj5064k";
    public static final String SECRET_KEY = "RCrBgDCGT0gA0H0XuHHf8POXX5ExAi4G";
 
    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
    static String accessToken;
 
    public static void main(String[] args) {
        JFrame frame = new JFrame("Image Processing");
        frame.setSize(800, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JLabel imageLabel = new JLabel();
        imageLabel.setBounds(0, 100, 300, 300);
 
        JLabel enhancedImageLabel = new JLabel();
        enhancedImageLabel.setBounds(350, 100, 300, 300);
 
        JButton addButton = new JButton("添加图片");
        JButton enhanceButton = new JButton("图像清晰度增强");
 
        addButton.setBounds(50, 50, 150, 30);
        enhanceButton.setBounds(400, 50, 150, 30);
 
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "jpeg", "png", "gif");
                fileChooser.setFileFilter(filter);
 
                int result = fileChooser.showOpenDialog(null);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    String imagePath = selectedFile.getAbsolutePath();
 
                    // 显示选择的图片并缩放大小
                    ImageIcon imageIcon = new ImageIcon(imagePath);
                    Image originalImage = imageIcon.getImage();
                    int originalWidth = originalImage.getWidth(null);
                    int originalHeight = originalImage.getHeight(null);
 
                    int newWidth = 300;
                    int newHeight = (int) (originalHeight * ((double) newWidth / originalWidth));
 
                    Image scaledImage = originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
                    ImageIcon scaledImageIcon = new ImageIcon(scaledImage);
                    imageLabel.setIcon(scaledImageIcon);
                }
            }
        });
 
 
        enhanceButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 图像清晰度增强按钮点击事件处理逻辑
                try {
                    if (accessToken == null) {
                        accessToken = getAccessToken();
                    }
                    byte[] enhancedImageData = enhanceImage(accessToken);
                    // 显示增强后的图片并进行缩放
                    ImageIcon enhancedImageIcon = new ImageIcon(enhancedImageData);
                    Image enhancedImage = enhancedImageIcon.getImage();
                    int enhancedWidth = enhancedImage.getWidth(null);
                    int enhancedHeight = enhancedImage.getHeight(null);
 
                    int newWidth = 300;
                    int newHeight = (int) (enhancedHeight * ((double) newWidth / enhancedWidth));
 
                    Image scaledEnhancedImage = enhancedImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
                    ImageIcon scaledEnhancedImageIcon = new ImageIcon(scaledEnhancedImage);
                    enhancedImageLabel.setIcon(scaledEnhancedImageIcon);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
 
        });
 
 
        frame.add(imageLabel);
        frame.add(enhancedImageLabel);
        frame.add(addButton);
        frame.add(enhanceButton);
        frame.setLayout(null);
        frame.setVisible(true);
    }
 
    /**
     * 调用百度API对图像进行清晰度增强
     *
     * @param accessToken 鉴权签名(Access Token)
     * @return 增强后的图片数据
     * @throws IOException IO异常
     */
    static byte[] enhanceImage(String accessToken) throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance?access_token=" + accessToken)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
 
        byte[] enhancedImageData = response.body().bytes();
        response.close();
 
        return enhancedImageData;
    }
 
    /**
     * 从用户的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");
    }
}

  写了一部分代码

posted @   漏网鲨鱼  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示