项目结构
具体代码
BaseConvert.java--将base64编码解码成我们能看懂的东西(功能)
package com.example.testDong;
import java.io.*;
import java.util.Base64;
public class BaseConvert {
/**
* 图片转化成base64字符串
* @param imgPath
* @return
*/
public static String GetImageStr(String imgPath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = imgPath;// 待处理的图片
InputStream in = null;
byte[] data = null;
String encode = null; // 返回Base64编码过的字节数组字符串
// 对字节数组Base64编码
Base64.Encoder encoder = Base64.getEncoder();
try {
// 读取图片字节数组
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
encode = encoder.encodeToString(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return encode;
}
@SuppressWarnings("finally")
public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片
if (imgData == null) // 图像数据为空
return false;
Base64.Decoder decoder = Base64.getDecoder();
// BASE64Decoder decoder = new BASE64Decoder();
OutputStream out = null;
try {
out = new FileOutputStream(imgFilePath);
// Base64解码
byte[] b = decoder.decode(imgData);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
out.write(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
out.flush();
out.close();
return true;
}
}
}
另外的那几个Util文件,就是在百度接口里面下载就行;
Selfie_anime.java(图像人像动漫化)
package com.example.testDong;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.URLEncoder;
/**
* 人像动漫化--成功实现
*/
public class Selfie_anime {
/**
* 重要提示代码中所需工具类
* FileUtil,Base64Util,HttpUtil,GsonUtils请从
* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
* 下载
*/
public static String selfie_anime(String filePath) {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime";
try {
// 本地文件路径
//filePath = "D:\\12140\\Desktops\\Picture\\bbb.jpg";
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = "24.3cc50cc6db1d40bf73abaa3bdf7b7636.2592000.1703722627.282335-43836864";
String result = HttpUtil.post(url, accessToken, param);
//System.out.println(result);
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(result).getAsJsonObject();
String image = jsonObject.get("image").getAsString();
//System.out.println("image: " + image);//图片的base64编码形式
//调用解码工具生成图片
Boolean b=BaseConvert.GenerateImage(image, filePath);
System.out.println("b "+b);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Image_definition_enhance.java(图像清晰度增强)
package com.example.testQing;
import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;
import com.example.testDong.BaseConvert;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.URLEncoder;
/**
* 图像清晰度增强
*/
public class Image_definition_enhance {
/**
* 重要提示代码中所需工具类
* FileUtil,Base64Util,HttpUtil,GsonUtils请从
* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
* 下载
*/
public static String image_definition_enhance(String filePath) {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance";
try {
// 本地文件路径
//String filePath = "D:\\12140\\Desktops\\Picture\\aaa.png";
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = "24.ec4728ef58e3e10f693a973da97035e0.2592000.1703745101.282335-43836864";
String result = HttpUtil.post(url, accessToken, param);
//System.out.println(result);
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(result).getAsJsonObject();
String image = jsonObject.get("image").getAsString();//image是处理完成的图片的base64编码
Boolean b= BaseConvert.GenerateImage(image,filePath);
System.out.println("b "+b);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
TestHei.java(黑白图像上色)
package com.example;
import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;
import com.example.testDong.BaseConvert;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.URLEncoder;
/**
* 黑白图像上色
*/
public class testHei {
/**
* 重要提示代码中所需工具类
* FileUtil,Base64Util,HttpUtil,GsonUtils请从
* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
* 下载
*/
public static String colourize(String filePath) {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize";
try {
// 本地文件路径
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = "24.65b142b62b529c0edf5b9b7198ad9387.2592000.1704382715.282335-43836864";
String result = HttpUtil.post(url, accessToken, param);
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(result).getAsJsonObject();
String image = jsonObject.get("image").getAsString();//image是处理完成的图片的base64编码
Boolean b= BaseConvert.GenerateImage(image,filePath);
System.out.println("b "+b);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
TestSe.java(图像色彩增强)
package com.example;
import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;
import com.example.testDong.BaseConvert;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.URLEncoder;
/**
* 图像色彩增强
*/
public class TestSe {
/**
* 重要提示代码中所需工具类
* FileUtil,Base64Util,HttpUtil,GsonUtils请从
* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
* 下载
*/
public static String colorEnhance(String filePath) {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/color_enhance";
try {
// 本地文件路径
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = "24.71c2327f0cf76bc60393e9163860a99b.2592000.1704383566.282335-43836864";
String result = HttpUtil.post(url, accessToken, param);
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(result).getAsJsonObject();
String image = jsonObject.get("image").getAsString();//image是处理完成的图片的base64编码
Boolean b= BaseConvert.GenerateImage(image,filePath);
System.out.println("b "+b);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
TestZhuan.java(图像风格转变)
package com.example;
import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;
import com.example.testDong.BaseConvert;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.URLEncoder;
/**
* 图像风格转换
*/
public class TestZhuan {
/**
* 重要提示代码中所需工具类
* FileUtil,Base64Util,HttpUtil,GsonUtils请从
* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
* 下载
*/
public static String styleTrans(String filePath) {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans";
try {
// 本地文件路径
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
String param = "option=" + "cartoon" + "&image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = "24.c5cbea995c160aead4696da4f88605b7.2592000.1704383334.282335-43836864";
String result = HttpUtil.post(url, accessToken, param);
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(result).getAsJsonObject();
String image = jsonObject.get("image").getAsString();//image是处理完成的图片的base64编码
Boolean b= BaseConvert.GenerateImage(image,filePath);
System.out.println("b "+b);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
标签:baidu,特效,String,ai,image,import,软件,com,图像增强
From: https://www.cnblogs.com/liuzijin/p/17931102.html