简介
使用阿里云人物动漫化功能制作一款属于自己的专属头像(该功能收费)
功能描述
人物动漫化能力可以将一张人物图像进行转换处理,生成二次元卡通形象,并返回动漫化后的结果图像。效果示例如下。
原图:
日漫风结果图:
3D特效结果图:
手绘风结果图:
铅笔画结果图:
艺术特效结果图:
详细内容请看官方文档
官方文档
开通服务
所需依赖
<properties>
<fastjson.version>1.2.9</fastjson.version>
<aliyun.ocr.version>1.0.4</aliyun.ocr.version>
<aliyun.facebody.version>2.0.0</aliyun.facebody.version>
<aliyun.imagerecog.version>1.0.5</aliyun.imagerecog.version>
<aliyun.imageseg.version>1.0.0</aliyun.imageseg.version>
<aliyun.imageenhan.version>1.0.3</aliyun.imageenhan.version>
<aliyun.goodstech.version>0.0.3</aliyun.goodstech.version>
<aliyun.objectdet.version>2.0.9</aliyun.objectdet.version>
<aliyun.imgsearch.version>1.0.0</aliyun.imgsearch.version>
<aliyun.videorecog.version>1.0.0</aliyun.videorecog.version>
<aliyun.videoenhan.version>1.0.2</aliyun.videoenhan.version>
<aliyun.videoseg.version>1.0.1</aliyun.videoseg.version>
<aliyun-java-sdk-facebody.version>1.2.27</aliyun-java-sdk-facebody.version>
</properties>
<dependencies>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!--阿里云人物动漫化-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ocr20191230</artifactId>
<version>${aliyun.ocr.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>facebody20191230</artifactId>
<version>${aliyun.facebody.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imagerecog20190930</artifactId>
<version>${aliyun.imagerecog.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imageseg20191230</artifactId>
<version>${aliyun.imageseg.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imageenhan20190930</artifactId>
<version>${aliyun.imageenhan.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>goodstech20191230</artifactId>
<version>${aliyun.goodstech.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>objectdet20191230</artifactId>
<version>${aliyun.objectdet.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imgsearch20200320</artifactId>
<version>${aliyun.imgsearch.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>videorecog20200320</artifactId>
<version>${aliyun.videorecog.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>videoseg20200320</artifactId>
<version>${aliyun.videoseg.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>videoenhan20200320</artifactId>
<version>${aliyun.videoenhan.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-facebody</artifactId>
<version>${aliyun-java-sdk-facebody.version}</version>
</dependency>
</dependencies>
具体代码
import com.aliyun.facebody20191230.Client;
import com.aliyun.facebody20191230.models.GenerateHumanAnimeStyleAdvanceRequest;
import com.aliyun.facebody20191230.models.GenerateHumanAnimeStyleResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.facebody.model.v20191230.GenerateHumanAnimeStyleRequest;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
/**
* @Author ChenNong
* @Date 2021/12/7
* @Description 头像工具类 by aliyun
*/
public class AvatarUtils {
private static final String accessKeyId = "阿里云accessKeyId ";
private static final String accessKeySecret = "阿里云accessKeySecret ";
/**
* 日漫风
*/
public static final String anime = "anime";
/**
* 3D特效
*/
public static final String threeD = "3d";
/**
* 手绘风
*/
public static final String handdrawn = "handdrawn";
/**
* 铅笔画
*/
public static final String sketch = "sketch";
/**
* 艺术特效
*/
public static final String artstyle = "artstyle";
/**
* 本地文件上传生成头像
* @param style 生成的图片风格
* @param filePath 图片文件位置
* @return
* @throws Exception
*/
public static String buildAvatarByFile(String style, String filePath) throws Exception {
String imageUrl = "";
try {
Config config = new Config();
//你的accessKeyId
config.accessKeyId = accessKeyId;
//你的accessKeyId
config.accessKeySecret = accessKeySecret;
//你的accessKeySecret
config.type = "access_key";
config.regionId = "cn-shanghai";
// config.endpointType="internal"; //默认通过公网访问OSS,如需通过内网请打开这一行
Client client = new Client(config);
RuntimeOptions runtimeOptions = new RuntimeOptions();
GenerateHumanAnimeStyleAdvanceRequest request = new GenerateHumanAnimeStyleAdvanceRequest();
request.setAlgoType(style);
request.imageURLObject = new FileInputStream(new File(filePath));
GenerateHumanAnimeStyleResponse response = client.generateHumanAnimeStyleAdvance(request, runtimeOptions);
// System.out.println("头像生成=" + JsonUtils.toJson(response));
return getImageUrl(response);
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
return imageUrl;
}
/**
* 图片url生成头像
* @param algoType 生成的图片风格
* @param url 图片url
* @return
* @throws Exception
*/
public static String buildAvatarByUrl(String algoType, String url) throws Exception {
String imageUrl = "";
try {
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
/** use STS Token
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // The region ID
"<your-access-key-id>", // The AccessKey ID of the RAM account
"<your-access-key-secret>", // The AccessKey Secret of the RAM account
"<your-sts-token>"); // STS Token
**/
IAcsClient client = new DefaultAcsClient(profile);
GenerateHumanAnimeStyleRequest request = new GenerateHumanAnimeStyleRequest();
request.setAlgoType(algoType);
request.setImageURL(url);
com.aliyuncs.facebody.model.v20191230.GenerateHumanAnimeStyleResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
return getImageUrl(response);
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
return imageUrl;
}
/**
* 获取返回结果中的imageUrl
*
* @param response
* @return
*/
private static String getImageUrl(Object response) {
String responseJson = JsonUtils.toJson(response);
Map<String, Object> responseMap = JsonUtils.toMap(responseJson);
String bodyJson = JsonUtils.toJson(responseMap.get("body"));
Map<String, Object> bodyMap = JsonUtils.toMap(bodyJson);
String dataJson = JsonUtils.toJson(bodyMap.get("data"));
Map<String, Object> imageUrlMap = JsonUtils.toMap(dataJson);
return String.valueOf(imageUrlMap.get("imageURL"));
}
}