上传文件至阿里云OSS,以及调用抠图api

本文介绍如何使用阿里云OSS进行文件上传,并利用阿里云的图像处理服务实现图片的抠图功能,包括头部和身体的抠取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 依赖

<!--阿里云图片处理开始-->
        <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;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值