导入依赖
<dependencies>
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.16.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
public class Sample {
//设置APPID/AK/SK
public static final String APP_ID = "xxxx";
public static final String API_KEY = "xxxx";
public static final String SECRET_KEY = "xxxx";
public static void main(String[] args) throws IOException {
// 初始化一个AipImageProcess
AipImageProcess client = new AipImageProcess(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
HashMap<String, String> options = new HashMap<>();
//可选参数
//anime或者anime_mask。前者生成二次元动漫图,后者生成戴口罩的二次元动漫人像
options.put("type", "anime");
//在type参数填入anime_mask时生效,1~8之间的整数,用于指定所使用的口罩的编码。
// type参数没有填入anime_mask,或mask_id 为空时,生成不戴口罩的二次元动漫图。
// options.putIfAbsent()
// 调用接口
String path = "E:\\图片\\a.jpg";
JSONObject jsonObject = client.selfieAnime(path, options);
// System.out.println(jsonObject);
ObjectMapper mapper = new ObjectMapper();
user user = mapper.readValue(jsonObject.toString(), user.class);
// System.out.println(user);
byte[] decode = Base64Utils.decode(user.getImage());
//生成jpeg图片
String imagePath = "E:\\图片\\a3.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imagePath);
out.write(decode);
out.close();
}