使用AI大模型生成动漫人像

在线体验:点击【图像处理】即可使用

public static final String SELFIE_ANIME = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime";
private static final String TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token";
private static final String AK = "需自行申请";
private static final String SK = "需自行申请";

/**
     * 人像动漫化
     *
     * @param base64 图片base64编码
     * @return 处理后的图片base64编码
     */
private static String selfieAnime(String base64) {
        String token = BaiduTokenUtil.getToken(AK, SK);
        String url = String.format("%s?access_token=%s", SELFIE_ANIME, token);
        FormBody formBody = new FormBody.Builder()
                .add("image", base64)
                .build();
        String result = HttpUtil.postForm(url, formBody);
        Assert.notNull(result, "请求超时,请重试");
        Map map = JSONUtil.toBean(result, Map.class);
        Object image = map.get("image");
        return image == null ? null : image.toString();
    }
 
 public static String getToken(String ak, String sk) {
        String token = redisUtil.get(Constants.BAIDU_TOKEN);
        if (token == null) {
            token = loadToken(appid, ak, sk);
        }
        return token;
    }

public static String loadToken(String ak, String sk) {
        String url = String.format("%s?client_id=%s&client_secret=%s&grant_type=client_credentials", TOKEN_URL, ak, sk);
        String result = HttpUtil.post(url, null);
        Assert.notNull(result, "调用失败");
        Map<String, String> data = (Map<String, String>) JSONUtil.parse(result);
        String token = data.get("access_token");
        redisUtil.set(Constants.BAIDU_TOKEN, token, 60 * 60 * 24);
        return token;
    }

你可能感兴趣的:(java,AI,AI编程)