一 依赖

<!--阿里云图片处理开始-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ivpd</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.9</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-imageseg</artifactId>
            <version>1.1.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.60</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <!--阿里云图片处理结束-->
        <!--阿里OSS开始-->
        <!-- 阿里云OSS -->
        <!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.13.0</version>
        </dependency>
        <!--阿里OSS结束-->

二上传文件

public class AliOssUtils {

    private static String endpoint = "*****";
    private static String accessKeyId = "*******";
    private static String accessKeySecret = "**********";
    private static String bucketName = "*******";
    private static String host = "***";
    private static String hostAli = "***********";

    /**
     * 上传文件,以IO流方式
     *
     * @param inputStream 输入流
     * @param objectName  唯一objectName(在oss中的文件名字)
     */
    public Map upload(InputStream inputStream, String objectName, String type) {
        Map<String, String> map = new HashMap<>();
        if (!StringUtils.isNullOrEmpty(type)) {
            host = hostAli;
        }
        try {
            ObjectMetadata objectMetadata = new ObjectMetadata();
            //使得图片可以预览而非直接下载
            objectMetadata.setContentType("image/jpg");
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。
            ossClient.putObject(bucketName, objectName, inputStream,objectMetadata);
            // 关闭OSSClient。
            ossClient.shutdown();
            map.put("url", host + objectName);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonException("上传失败!");
        }
        return map;
    }
}

三抠图

      抠头部

/**
     * https://vision.aliyun.com/experience/detail?spm=a211p3.14020179.J_7524944390.99.66cd4850dnfKkk&tagName=imageseg&children=SegmentHead
     *
     * @param headUrl
     * @return 抠头
     */
    public AliPicHead aliHeadPic(String headUrl) {

        AliPicHead aliPicHead = new AliPicHead();

        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        SegmentHeadRequest request = new SegmentHeadRequest();
        request.setSysRegionId("cn-shanghai");
        request.setImageURL(headUrl);
        SegmentHeadResponse response = new SegmentHeadResponse();

        try {
            response = client.getAcsResponse(request);
            String pic = aliPic(response, null);
            aliPicHead.setUrl(pic);
            aliPicHead.setSegmentHeadResponse(response);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            aliPicHead.setError(e.getErrMsg());
        }
        return aliPicHead;
    }

    抠身体

/**
     * https://vision.aliyun.com/experience/detail?spm=a211p3.14020179.J_7524944390.21.66cd4850Yd12V2&tagName=imageseg&children=SegmentBody
     *
     * @param bodyUrl
     * @return 抠身体
     */
    public AliPicBody aliBodyPic(String bodyUrl) {

        AliPicBody aliPicBody = new AliPicBody();

        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        SegmentBodyRequest request = new SegmentBodyRequest();
        request.setSysRegionId("cn-shanghai");
        request.setImageURL(bodyUrl);
        SegmentBodyResponse response = new SegmentBodyResponse();
        try {
            response = client.getAcsResponse(request);
            String aliPic = aliPic(null, response);
            aliPicBody.setUrl(aliPic);
            aliPicBody.setSegmentBodyResponse(response);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            aliPicBody.setError(e.getErrMsg());
        }
        return aliPicBody;
    }

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐