PhotoSer.java 4.51 KB
package com.example.demo;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.imagesearch.model.v20180120.AddItemRequest;
import com.aliyuncs.imagesearch.model.v20180120.AddItemResponse;
import com.aliyuncs.imagesearch.model.v20180120.SearchItemRequest;
import com.aliyuncs.imagesearch.model.v20180120.SearchItemResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by jack on 2018/3/14.
 */
@Service
@ConfigurationProperties("sdk")
@Data
public class PhotoSer {
    private static final Logger LOGGER = LoggerFactory.getLogger(PhotoSer.class);
    IClientProfile profile;
    IAcsClient client;
    String ak;
    String aks;
    String instanceId;
    String instanceName;

    public PhotoSer() {

    }

    @PostConstruct
    public void init() {
        try {
            DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "ImageSearch", "imagesearch.cn-shanghai.aliyuncs.com");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        profile = DefaultProfile.getProfile("cn-shanghai", ak, aks);
        client = new DefaultAcsClient(profile);


    }


    public void loadPhoto(int id, List<String> filePaths) throws ClientException {
        AddItemRequest request = new AddItemRequest();
        request.setInstanceName(instanceName);
        request.setCatId("0");
        request.setItemId(String.valueOf(id));
        request.setCustContent("{\"key\":\"value\"}");
        for (String filePath : filePaths) {
            String fileName = FilenameUtils.getName(filePath);
            byte[] fileContent = getBytes(filePath);
            // 以字节流的方式读取图片内容, key 为图片名称,value 为图片内容
            if (null == fileContent) continue;
            request.addPicture(fileName, fileContent);
        }

        if (!request.buildPostContent()) {
            LOGGER.error("build post content failed.");
            return;
        }
        AddItemResponse response = null;
        response = client.getAcsResponse(request);
        LOGGER.info("success to load {}, resp {} ", id, response.getSuccess());

    }

    private byte[] getBytes(String photoPath) {
        File file = new File(photoPath);
        try {
            byte[] content = FileUtils.readFileToByteArray(file);
            return content;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public SearchItemResponse searchPhoto(String filePath) {
        SearchItemRequest request = new SearchItemRequest();
        request.setInstanceName(instanceName);
        request.setNum(5);
        request.setStart(0);
        byte[] bytes = getBytes(filePath);
        request.setSearchPicture(bytes);
        if (!request.buildPostContent()) {
            System.out.println("build post content failed.");
            return null;
        }
        SearchItemResponse response = null;
        try {
            response = client.getAcsResponse(request);
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }


    public SearchItemResponse searchPhoto(String filePath, int num) {
        SearchItemRequest request = new SearchItemRequest();
        request.setInstanceName(instanceName);
        request.setNum(num);
        request.setStart(0);

        ImageUtils.zoomInImage(filePath, "/tmp/text.jpg");

        byte[] bytes = getBytes("/tmp/text.jpg");

        request.setSearchPicture(bytes);
        if (!request.buildPostContent()) {
            System.out.println("build post content failed.");
            return null;
        }
        SearchItemResponse response = null;
        try {
            response = client.getAcsResponse(request);
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }


}