实验二:百度图像增强与特效SDK实验
一、实验要求
任务一:下载配置百度图像增强与特效的Java相关库及环境(占10%)。
任务二:了解百度图像增强与特效相关功能并进行总结(占20%)。
任务三:完成图像增强GUI相关功能代码并测试调用,要求上传自己的模糊照片进行图像增强(占30%)。
任务四:完成图像特效GUI相关功能代码并测试调用,要求上传自己的照片进行图像特效(占30%)。
实验总结:(占10%)
二、实验步骤
任务一:
任务二:
百度图像增强与特效是百度提供的一项人工智能服务,它可以通过应用各种图像处理算法对图像进行增强和添加特效,以改善图像的质量和视觉效果。以下两种是我用到百度图像增强与特效功能的总结:
- 图像增强:百度图像增强服务提供了多种图像增强技术,包括亮度增强、对比度增强、锐化等。这些技术可以使图像更清晰、更鲜明,并提高图像的可视性。
- 图片动漫化:图片动漫化是一种将真实照片或图像转换成类似动漫风格的艺术效果的过程。这种效果通常包括对图像进行线条加粗、颜色处理和光影效果等,使图像看起来像是从动漫或漫画中提取出来的。
任务三:
MainGUI.java
package baidu.com;
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.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
public class MainGUI extends JFrame {
private JButton button;
private JTextArea textArea;
private JLabel imageLabel;
public MainGUI() {
setTitle("图片处理GUI");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建按钮和文本区域
button = new JButton("处理图片");
textArea = new JTextArea();
textArea.setEditable(false);
// 创建图片标签
imageLabel = new JLabel();
imageLabel.setPreferredSize(new Dimension(300, 300));
// 添加组件到窗口布局中
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(button, BorderLayout.NORTH);
container.add(textArea, BorderLayout.CENTER);
container.add(imageLabel, BorderLayout.WEST);
// 添加按钮点击事件处理程序
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
// 调用Sample类方法获取图片Base64编码
String base64Image = Sample.getFileContentAsBase64("D:\\3.jpg", true);
textArea.setText("Base64编码:" + base64Image);
// 处理图片并显示
processAndDisplayImage(base64Image);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
private void processAndDisplayImage(String base64Image) throws IOException {
// 请求处理图片
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "type=anime&mask_id=1&image=" + base64Image);
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 = Sample.HTTP_CLIENT.newCall(request).execute();
// 获取处理后的图片Base64编码
String processedImageBase64 = new JSONObject(response.body().string()).getString("image");
// 显示处理后的图片
showImage(processedImageBase64);
}
private void showImage(String base64Image) {
// 解码Base64编码为字节数组
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
// 创建图像对象
ImageIcon imageIcon = new ImageIcon(imageBytes);
Image image = imageIcon.getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT);
imageIcon = new ImageIcon(image);
// 更新图片标签的显示
imageLabel.setIcon(imageIcon);
}
private String getAccessToken() throws IOException {
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + Sample.API_KEY
+ "&client_secret=" + Sample.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 = Sample.HTTP_CLIENT.newCall(request).execute();
return new JSONObject(response.body().string()).getString("access_token");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainGUI mainGUI = new MainGUI();
mainGUI.setVisible(true);
}
});
}
}
Sample.java
package baidu.com;
import okhttp3.*;
import org.json.JSONObject;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.net.URLEncoder;
class Sample {
public static final String API_KEY = "0pMg2OjMvVjVW0uSq3QxSc6l";
public static final String SECRET_KEY = "k63PimOyzKMLU9yVPDx7jxbWb8dIPT0j";
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public static void main(String []args) throws IOException{
// booleanture = true;
String filepath ="D:\\2.jpg";
String name= getFileContentAsBase64(filepath,true);
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
// image 可以通过 getFileContentAsBase64("C:\fakepath\1.png.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true
RequestBody body = RequestBody.create(mediaType, "type=anime&mask_id=1&image="+name);
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();
System.out.println(response.body().string());
}
/**
* 获取文件base64编码
*
* @param path 文件路径
* @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
* @return base64编码信息,不带文件头
* @throws IOException IO异常
*/
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");
}
}
任务四:
MainGUI.Java
package baidu.com;
import okhttp3.*;
import org.json.JSONObject;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.net.URLEncoder;
class Sample {
public static final String API_KEY = "0pMg2OjMvVjVW0uSq3QxSc6l";
public static final String SECRET_KEY = "k63PimOyzKMLU9yVPDx7jxbWb8dIPT0j";
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public static void main(String []args) throws IOException{
// booleanture = true;
String filepath ="D:\\2.jpg";
String name= getFileContentAsBase64(filepath,true);
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
// image 可以通过 getFileContentAsBase64("C:\fakepath\1.png.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true
RequestBody body = RequestBody.create(mediaType, "type=anime&mask_id=1&image="+name);
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();
System.out.println(response.body().string());
}
/**
* 获取文件base64编码
*
* @param path 文件路径
* @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
* @return base64编码信息,不带文件头
* @throws IOException IO异常
*/
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");
}
}
Sample.java
package baidu.com;
import okhttp3.*;
import org.json.JSONObject;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.net.URLEncoder;
class Sample {
public static final String API_KEY = "0pMg2OjMvVjVW0uSq3QxSc6l";
public static final String SECRET_KEY = "k63PimOyzKMLU9yVPDx7jxbWb8dIPT0j";
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public static void main(String []args) throws IOException{
// booleanture = true;
String filepath ="D:\\2.jpg";
String name= getFileContentAsBase64(filepath,true);
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
// image 可以通过 getFileContentAsBase64("C:\fakepath\1.png.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true
RequestBody body = RequestBody.create(mediaType, "type=anime&mask_id=1&image="+name);
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();
System.out.println(response.body().string());
}
/**
* 获取文件base64编码
*
* @param path 文件路径
* @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
* @return base64编码信息,不带文件头
* @throws IOException IO异常
*/
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");
}
}
标签:body,12,java,String,每日,application,new,import,打卡 From: https://www.cnblogs.com/yunbianshangdadun/p/17880680.html