12.6每日总结
今天进行了软件构造的实验二,
实验二:百度图像增强与特效SDK实验(2023.12.6日完成)
任务一:下载配置百度图像增强与特效的Java相关库及环境(占10%)。
任务二:了解百度图像增强与特效相关功能并进行总结(占20%)。
任务三:完成图像增强GUI相关功能代码并测试调用,要求上传自己的模糊照片进行图像增强(占30%)。
任务四:完成图像特效GUI相关功能代码并测试调用,要求上传自己的照片进行图像特效(占30%)。
实验总结:(占10%)
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");
}
}
写了一部分代码
浙公网安备 33010602011771号