全部产品

Java SDK

本文介绍淘宝联盟服务Java SDK的使用方法及示例。

接口列表

接口名称

接口说明

SearchByPic

用于向淘宝联盟版在线调用,根据图片查询。

SearchByUrl

用户向淘宝联盟版在线调用,根据图片Url查询

准备工作

  • 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见

    创建AccessKey

  • 将SDK添加到项目中引入ImageSearch SDK依赖,通过Maven二方库依赖的方式将ImageSearch的SDK加入到自己的项目中。

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>imagesearch20210501</artifactId>
  <version>1.0.1</version>
</dependency>

SearchByPic接口

  • 代码示例

package com.example.demo1.imagesearch20210501;

import com.alibaba.fastjson.JSON;
import com.aliyun.imagesearch20210501.Client;
import com.aliyun.imagesearch20210501.models.SearchByPicAdvanceRequest;
import com.aliyun.imagesearch20210501.models.SearchByPicResponse;
import com.aliyun.imagesearch20210501.models.SearchByPicResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;


public class SearchByPic {
    private static final String PIC_NAME = "C:\\xxx/9.jpg";

    private static final String PIC_URL = "https://ossname.oss-cn-shanghai.aliyuncs.com/m.jpg";

    public static void main(String[] args) throws Exception {
        Config authConfig = new Config();

        authConfig.accessKeyId = "xxxxxx";
        authConfig.accessKeySecret = "xxxxxxx";

        authConfig.endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
        authConfig.regionId = "cn-shanghai";
        Client client = new Client(authConfig);
        SearchByPicAdvanceRequest request = new SearchByPicAdvanceRequest();

        InputStream inputStream = new FileInputStream(PIC_NAME);

        request.picContentObject = inputStream;
        // 需要返回的字段list。不同的字段用逗号分割。默认ClickUrl,ItemName,Pic,Price,PromotionPrice字段必返回
        request.fields = "CouponStartTime,CouponEndTime";
        // 图片类目
        //request.categoryId = 88888888;
        // 是否需要进行主体识别。默认true
        //request.crop = false;
        // 图片的主体区域,格式为x1,x2,y1,y2
        //request.region = "518,1524,398,1632";

        // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
        request.start = 0;

        // 选填,返回结果的数目。取值范围:1-20。默认值:10。
        request.num = 2;

        // 渠道ID。用于淘宝联盟中的渠道区分。
        //request.relationId = 1145;

        // 2 创建RuntimeObject实例并设置运行参数
        RuntimeOptions runtimeOptions = new RuntimeOptions();
        runtimeOptions.autoretry = true;
        try {
            SearchByPicResponse response = client.searchByPicAdvance(request, runtimeOptions);
            System.out.println("requestID:" + response.getBody().getRequestId());
            System.out.println("code:" + response.getBody().getCode());
            System.out.println("message:" + response.getBody().getMessage());
            List<SearchByPicResponseBody.SearchByPicResponseBodyDataAuctions> auctions = response.getBody().getData().getAuctions();
            for (SearchByPicResponseBody.SearchByPicResponseBodyDataAuctions auction : auctions) {
                System.out.println("---------------");
                System.out.println(auction.getRankScore());
                SearchByPicResponseBody.SearchByPicResponseBodyDataAuctionsResult result = auction.getResult();
                System.out.println("result:" + result.toMap());
            }

            SearchByPicResponseBody.SearchByPicResponseBodyPicInfo picInfo = response.getBody().getPicInfo();
            System.out.println("---------------");
            SearchByPicResponseBody.SearchByPicResponseBodyPicInfoMainRegion mainRegion = picInfo.getMainRegion();
            for (SearchByPicResponseBody.SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId categoryId : mainRegion.getMultiCategoryId()) {
                // 主体区域预测 类目
                System.out.println("mainRegion categoryId: " + categoryId.getCategoryId());
                // 主体区域预测 类目分值打分
                System.out.println("mainRegion score: " + categoryId.getScore());
            }
            System.out.println("---------------");
            System.out.println("mainRegion region: " + mainRegion.getRegion());
            System.out.println("---------------");
            List<SearchByPicResponseBody.SearchByPicResponseBodyPicInfoMultiRegion> multiCategoryIds = picInfo.getMultiRegion();
            if (null != multiCategoryIds) {
                for (SearchByPicResponseBody.SearchByPicResponseBodyPicInfoMultiRegion c : multiCategoryIds) {
                    // 多主体识别 区域
                    System.out.println("multi region: " + c.getRegion());
                }
            }

        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getData());
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {

  • 结果示例

requestID:69988741-981B-4EB2-9BD0-A72F5046FB34
code:0
message:null
---------------
0.8301
result:{CouponAmount=null, ClickUrl=//s.click.taobao.com/t?e=xxxxxx, CouponShareUrl=null, Pic=//img.alicdn.com/i1/yyyyyy4.jpg, ItemId=null, PromotionPrice=0.12, Provcity=null, CouponEndTime=null, SellerNickName=null, CouponTotalCount=null, Price=0.12, MonthSellCount=null, CouponStartTime=null, ItemName=PPR接头配件, SellerId=null, CategoryName=null, CouponStartFee=null, CommissionRate=null, CouponSaleTextInfo=null, UserType=null, CouponRemainCount=null, LevelOneCategoryName=null}
---------------
0.8316
result:{CouponAmount=null, ClickUrl=//s.click.taobao.com/t?e=xxxxxD, CouponShareUrl=null, Pic=//img.alicdn.com/i1/yyyyyy.jpg, ItemId=null, PromotionPrice=9.9, Provcity=null, CouponEndTime=null, SellerNickName=null, CouponTotalCount=null, Price=9.9, MonthSellCount=null, CouponStartTime=null, ItemName= pvc免粘胶, SellerId=null, CategoryName=null, CouponStartFee=null, CommissionRate=null, CouponSaleTextInfo=null, UserType=null, CouponRemainCount=null, LevelOneCategoryName=null}
---------------
mainRegion categoryId: 88888888
mainRegion score: 0.7559
mainRegion categoryId: 20
mainRegion score: 0.05169
mainRegion categoryId: 8
mainRegion score: 0.04217
mainRegion categoryId: 9
mainRegion score: 0.03845
---------------
mainRegion region: 55,456,65,415
---------------
multi CategoryId: 55,456,65,415

SearchByUrl接口

  • 代码示例

package com.example.demo1.imagesearch20210501;

import com.alibaba.fastjson.JSON;
import com.aliyun.imagesearch20210501.Client;
import com.aliyun.imagesearch20210501.models.SearchByPicAdvanceRequest;
import com.aliyun.imagesearch20210501.models.SearchByPicResponse;
import com.aliyun.imagesearch20210501.models.SearchByPicResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;


public class SearchByUrl {
    private static final String PIC_NAME = "C:\\xxxl/9.jpg";

    private static final String PIC_URL = "https://ossname.oss-cn-shanghai.aliyuncs.com/m.jpg";

    public static void main(String[] args) throws Exception {
        Config authConfig = new Config();

        authConfig.accessKeyId = "xxxxxx";
        authConfig.accessKeySecret = "xxxxxx";

        authConfig.endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
        authConfig.regionId = "cn-shanghai";
        Client client = new Client(authConfig);
        SearchByUrlRequest request = new SearchByUrlRequest();

        request.picUrl = PIC_URL;
        // 需要返回的字段list。不同的字段用逗号分割。默认ClickUrl,ItemName,Pic,Price,PromotionPrice字段必返回
        request.fields = "Provcity,UserType,CouponTotalCount,ItemId";
        // 图片类目
        //request.categoryId = 88888888;
        // 是否需要进行主体识别。默认true
        //request.crop = false;
        // 图片的主体区域,格式为x1,x2,y1,y2
        //request.region = "518,1524,398,1632";
        // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
        request.start = 0;
        // 选填,返回结果的数目。取值范围:1-20。默认值:10。
        request.num = 2;
        // 渠道ID。用于淘宝联盟中的渠道区分。
        //request.relationId = 1145;
        try {
            SearchByUrlResponse response = client.searchByUrl(request);
            System.out.println("requestID:" + response.getBody().getRequestId());
            System.out.println("code:" + response.getBody().getCode());
            System.out.println("message:" + response.getBody().getMessage());
            List<SearchByUrlResponseBody.SearchByUrlResponseBodyDataAuctions> auctions = response.getBody().getData().getAuctions();
            for (SearchByUrlResponseBody.SearchByUrlResponseBodyDataAuctions auction : auctions) {
                System.out.println("---------------");
                System.out.println(auction.getRankScore());
                SearchByUrlResponseBody.SearchByUrlResponseBodyDataAuctionsResult result = auction.getResult();
                System.out.println("result:" + result.toMap());
            }

            SearchByUrlResponseBody.SearchByUrlResponseBodyPicInfo picInfo = response.getBody().getPicInfo();
            System.out.println("---------------");
            SearchByUrlResponseBody.SearchByUrlResponseBodyPicInfoMainRegion mainRegion = picInfo.getMainRegion();
            List<SearchByUrlResponseBody.SearchByUrlResponseBodyPicInfoMultiRegion> multiRegion = picInfo.getMultiRegion();
            for (SearchByUrlResponseBody.SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId categoryId : mainRegion.getMultiCategoryId()) {
                // 主体区域预测 类目
                System.out.println("mainRegion categoryId: " + categoryId.getCategoryId());
                // 主体区域预测 类目分值打分
                System.out.println("mainRegion score: " + categoryId.getScore());
            }System.out.println("---------------");
            System.out.println("mainRegion region: " + mainRegion.getRegion());
            System.out.println("---------------");
            if (null != mainRegion) {
                for (SearchByUrlResponseBody.SearchByUrlResponseBodyPicInfoMultiRegion c : multiRegion) {
                    // 多主体识别 区域
                    System.out.println("multi region: " + c.getRegion());
                }
            }

        } catch (TeaException e) {
            System.out.println(e.getCode());
            System.out.println(e.getData());
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 结果示例

requestID:69988741-981B-4EB2-9BD0-A72F5046FB34
code:0
message:null
---------------
0.8301
result:{CouponAmount=null, ClickUrl=//s.click.taobao.com/t?e=xxxxxx, CouponShareUrl=null, Pic=//img.alicdn.com/i1/yyyy.jpg, ItemId=null, PromotionPrice=0.12, Provcity=null, CouponEndTime=null, SellerNickName=null, CouponTotalCount=null, Price=0.12, MonthSellCount=null, CouponStartTime=null, ItemName=PPR接头配件, SellerId=null, CategoryName=null, CouponStartFee=null, CommissionRate=null, CouponSaleTextInfo=null, UserType=null, CouponRemainCount=null, LevelOneCategoryName=null}
---------------
0.8316
result:{CouponAmount=null, ClickUrl=//s.click.taobao.com/t?e=xxxxxD, CouponShareUrl=null, Pic=//img.alicdn.com/i1/yyyyyy.jpg, ItemId=null, PromotionPrice=9.9, Provcity=null, CouponEndTime=null, SellerNickName=null, CouponTotalCount=null, Price=9.9, MonthSellCount=null, CouponStartTime=null, ItemName= pvc免粘胶, SellerId=null, CategoryName=null, CouponStartFee=null, CommissionRate=null, CouponSaleTextInfo=null, UserType=null, CouponRemainCount=null, LevelOneCategoryName=null}
---------------
mainRegion categoryId: 88888888
mainRegion score: 0.7559
mainRegion categoryId: 20
mainRegion score: 0.05169
mainRegion categoryId: 8
mainRegion score: 0.04217
mainRegion categoryId: 9
mainRegion score: 0.03845
---------------
mainRegion region: 55,456,65,415
---------------
multi CategoryId: 55,456,65,415