All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qingxun.javasdkapi.clients.ImageOcrOpenApiClient Maven / Gradle / Ivy

Go to download

openapi SDK for Java Copyright © 2023 杭州轻寻科技 All rights reserved. 版权所有 (C)杭州轻寻科技 http://open.qingxun.com

The newest version!
package com.qingxun.javasdkapi.clients;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qingxun.javasdkapi.clients.interfaces.CommonOpenApiClient;
import com.qingxun.javasdkapi.clients.interfaces.IImageOcrOpenApiClient;
import com.qingxun.javasdkapi.common.ApiCode;
import com.qingxun.javasdkapi.common.UrlConstansts;
import com.qingxun.javasdkapi.request.OcrUploadTransRequest;
import com.qingxun.javasdkapi.response.ApiResponse;
import com.qingxun.javasdkapi.response.OcrImageUploadResponse;

import com.qingxun.javasdkapi.utils.HttpClientUtil;
import com.qingxun.javasdkapi.utils.IHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static com.qingxun.javasdkapi.common.JsonUtil.objectMapper;

/**
 * OCR(图像识别)
 */
public class ImageOcrOpenApiClient extends CommonOpenApiClient implements IImageOcrOpenApiClient {




    private Logger log = LoggerFactory.getLogger(ImageOcrOpenApiClient.class);

    private IHttpUtil httpUtil = HttpClientUtil.DEFAULT;

    public ImageOcrOpenApiClient(String appId, String privateKey) {
        super(appId, privateKey);
    }
    public ImageOcrOpenApiClient(String appId, String privateKey,IHttpUtil iHttpUtil) {
        super(appId, privateKey);
        this.httpUtil = iHttpUtil;
    }

    @Override
    public OcrImageUploadResponse excute(OcrUploadTransRequest ocrUploadTransRequest) {
        String urlResult = "";
        if (ocrUploadTransRequest.getFile() !=null ){
            urlResult = excuteFile(ocrUploadTransRequest);
        }else{
            urlResult = excuteBytes(ocrUploadTransRequest);
        }
        OcrImageUploadResponse uploadResponse = new OcrImageUploadResponse();
        try {
            ApiResponse> baseResponse = objectMapper.readValue(urlResult, ApiResponse.class);
            uploadResponse.setApiCode(baseResponse.getCode(), baseResponse.getMsg());
            ApiCode apiCode = ApiCode.getByCode(baseResponse.getCode());
            if (apiCode == ApiCode.OK && null != baseResponse.getData()) {
                if (baseResponse.getData().containsKey("words_result_num")) {
                    Object wordResultNum = baseResponse.getData().get("words_result_num");
                    if (wordResultNum != null) {
                        uploadResponse.setWordsResultNum((Integer) wordResultNum);
                    }
                }
                if (baseResponse.getData().containsKey("words_result")) {
                    Object wordRestult = baseResponse.getData().get("words_result");
                    if (wordRestult != null) {
                        uploadResponse.setWordsResult((List) wordRestult);
                    }
                }
            }
            return uploadResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        uploadResponse.setApiCode(ApiCode.SYSTEM_ERROR.getCode(), ApiCode.SYSTEM_ERROR.getMessage());
        return uploadResponse;

    }

    private String excuteFile(OcrUploadTransRequest ocrUploadTransRequest) {
        TreeMap treeMap = ocrUploadTransRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        treeMap.put("file", ocrUploadTransRequest.getFile());
        String urlResult = httpUtil.post(UrlConstansts.OCR_UPLOAD_URL,treeMap,ocrUploadTransRequest.getFile());
        treeMap.remove("file");
        try {
            log.info("url: {}\n参数:{}\n结果{}", UrlConstansts.OCR_UPLOAD_URL, objectMapper.writeValueAsString(treeMap), urlResult);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return urlResult;
    }

    private String excuteBytes(OcrUploadTransRequest ocrUploadTransRequest){
        TreeMap treeMap = ocrUploadTransRequest.transToMap();
        treeMap = generateCommonParameter(treeMap);
        String urlResult="";
        try {
            urlResult = httpUtil.post(UrlConstansts.OCR_UPLOAD_URL,treeMap,ocrUploadTransRequest.getFileBytes(),ocrUploadTransRequest.getFileName());
            log.info("url: {}\n参数:{}\n结果{}", UrlConstansts.OCR_UPLOAD_URL, objectMapper.writeValueAsString(treeMap), urlResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlResult;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy