圖片美化增強(qiáng)AI接口調(diào)用手冊(cè)

在調(diào)合合AI平臺(tái)提供的圖片美化增強(qiáng)API接口,API平臺(tái)鏈接:https://ai.ccint.com/doc/api/crop_enhance_image, 因?yàn)橛杏龅揭恍﹩栴}缎除,寫篇博客記錄一下

API文檔提供的說(shuō)明: url中參數(shù)app_key為個(gè)人中心實(shí)例的app_key

請(qǐng)求方式: POST

返回類型: JSON

POST BODY請(qǐng)求字段描述

字段 說(shuō)明
image_data 必填,圖像的base64串
app_secret 必填总寻,個(gè)人中心實(shí)例的app_secret
scan-m 掃描模式器罐, 建議為 1
detail 銳化程度,建議為-1
contrast 對(duì)比度 渐行,建議為 0
bright 增亮 轰坊,建議為 0
enhanceMode 增強(qiáng)模式,1:增亮祟印,2:增強(qiáng)并銳化肴沫,3:黑白,4:灰度

POST BODY蕴忆,接口要求以Post body方式發(fā)送颤芬,因?yàn)橐獋鱞ase64字符串,請(qǐng)求參數(shù)過(guò)長(zhǎng)有400錯(cuò)誤的


    
{
    "image_data": "", // 必填,圖像的base64串
    "app_secret": "" // 必填站蝠,個(gè)人中心實(shí)例的app_secret
  "scan-m": 1, //掃描模式汰具, 建議為 1
  "detail": -1, //銳化程度,建議為-1
  "contrast": 0, //對(duì)比度 菱魔,建議為 0
  "bright": 0, //增亮 留荔,建議為 0
  "enhanceMode": 0 //增強(qiáng)模式,1:增亮澜倦,2:增強(qiáng)并銳化聚蝶,3:黑白,4:灰度
}



提示:POST BODY 為 JSON字符串藻治。

返回字段描述

字段 說(shuō)明
code 返回狀態(tài)碼碘勉。200:正常返回; 500:服務(wù)器內(nèi)部錯(cuò)誤
message 返回對(duì)應(yīng)code的狀態(tài)說(shuō)明
result base64編碼的圖片信息

正常返回示例

{
    "code": 200,
    "message": "success",
    "result": “圖片base64信息”
}

失敗返回示例


    {
    "code":30301,
    "message":"額度已用完,請(qǐng)充值后使用"
}

返回碼說(shuō)明


在這里插入圖片描述

API文檔提供的實(shí)例代碼:

import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static void main(String[] args) throws Exception {
        String url = "https://ocr-api.ccint.com/ocr_service?app_key=%s";
        String appKey = "xxxxxx"; // your app_key
      String appSecret = "xxxxxx"; // your app_secret
      url = String.format(url, appKey);
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            String imgData = imageToBase64("example.jpg");
            String param="{\"app_secret\":\"%s\",\"image_data\":\"%s\"}";
            param=String.format(param,appSecret,imgData);
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST"); // 設(shè)置請(qǐng)求方式
            conn.setRequestProperty("Content-Type", "application/json"); // 設(shè)置發(fā)送數(shù)據(jù)的
            conn.connect();
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.append(param);
            out.flush();
            out.close();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("發(fā)送 POST 請(qǐng)求出現(xiàn)異常栋艳!" + e);
            e.printStackTrace();
        }
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println(result);
    }
    public static String imageToBase64(String path)
    {
        String imgFile = path;
        InputStream in = null;
        byte[] data = null;
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
}

注意要點(diǎn):

  • 寫文件流時(shí)記得outputstream要flush恰聘,才能拿到數(shù)據(jù)
  • 接口返回的json格式的數(shù)據(jù)句各,同時(shí)帶有base64的字符串吸占,所以需要json解析一下,然后調(diào)工具類凿宾,將base64字符串轉(zhuǎn)換為文件矾屯,保存在本地,下面給出調(diào)用的代碼初厚,僅供參考
/**
     * 圖片切邊增強(qiáng)接口調(diào)用
     * @author nicky.ma
     * @date   2019年5月20日下午3:44:27
     * @param scanM   掃描模式件蚕, 建議為 1
     * @param bright    增亮 ,建議為 0
     * @param contrast   對(duì)比度 产禾,建議為 0
     * @param detail     銳化程度排作,建議為-1
     * @param sourcePath  
     * @param destPath
     * detail=0&contrast=0&bright=50  增到最亮
     * @return
     */
    public static void ccintCropEnhanceHttpService(final int scanM,final int bright,final int contrast,
            final int detail,final int enhanceMode,String sourcePath,String destPath) throws Exception{
        logger.info("sourcePath:{}"+sourcePath);
        logger.info("destPath:{}"+destPath);
        
        //base64轉(zhuǎn)換
        final String imgData = imageToBase64(sourcePath);
        
        Map<String,Object> paramsMap=new HashMap<String,Object>(){
        private static final long serialVersionUID=1L;
        {
                put("image_data",imgData);
                put("app_secret",CCINT_APP_SECRET);
                put("scan-m",scanM);
                put("detail",detail);
                put("contrast",contrast);
                put("bright",bright);
                put("enhanceMode",enhanceMode);
        }};
        String param = JSON.toJSONString(paramsMap);
        
//      String param="{\"app_secret\":\"%s\",\"image_data\":\"%s\",\"scan-m\":\"%s\",\"detail\":\"%s\",\"contrast\":\"%s\",\"bright\":\"%s\",\"enhanceMode\":\"%s\"}";
//      param=String.format(param,CCINT_APP_SECRET,imgData,scanM,detail,contrast,bright,enhanceMode);
        
        String url = CCINT_CROP_ENHANCE_URL+"?app_key="+CCINT_APP_KEY;
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setConnectTimeout(20*1000);
            conn.setReadTimeout(20*1000);
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST"); // 設(shè)置請(qǐng)求方式
            //conn.setRequestProperty("transfer-encoding","chunked");
            conn.setRequestProperty("Content-Type", "application/json"); // 設(shè)置發(fā)送數(shù)據(jù)的
            conn.connect();
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.append(param);
            //要記得flush,才能拿到數(shù)據(jù)
            out.flush();
            out.close();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            logger.info("返回Result:{}"+result);
            int code=conn.getResponseCode();
            if(code==200){
                JSONObject obj = JSON.parseObject(result);
//              copyFileByInputStream(conn.getInputStream(),destPath);
                FileBase64Util.decoderBase64File(obj.getString("result"), destPath);
                logger.info("圖片增強(qiáng)后文件大小:{}"+new File(destPath).length()/1024+"KB");
            }
            conn.disconnect();

        } catch (Exception e) {
            logger.error("AI平臺(tái)接口調(diào)用異常:{}"+e);
            e.printStackTrace();
        }finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    private static String imageToBase64(String path)
    {
        String imgFile = path;
        InputStream in = null;
        byte[] data = null;
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
    

base64字符串和文件轉(zhuǎn)換工具類:


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.codec.binary.Base64;

public class FileBase64Util{

     /**
      * 將文件轉(zhuǎn)成base64 字符串
      * @param path文件路徑
      * @return   
      * @throws Exception
      */
     public static String encodeBase64File(String path) throws Exception {
       File file = new File(path);
       FileInputStream inputFile = new FileInputStream(file);
       byte[] buffer = new byte[(int) file.length()];
       inputFile.read(buffer);
       inputFile.close();
       return Base64.encodeBase64String(buffer);
     }

     /**
      * 將base64字符解碼保存文件
      * @param base64String
      * @param targetPath
      * @throws Exception
      */

     public static void decoderBase64File(String base64String, String targetPath)throws Exception {
         byte[] buffer=Base64.decodeBase64(base64String);
         FileOutputStream out = new FileOutputStream(targetPath);
         out.write(buffer);
         out.close();
     }

     /**
      * 將base64字符保存文本文件
      * @param base64Code
      * @param targetPath
      * @throws Exception
      */

     public static void toFile(String base64Code, String targetPath)throws Exception {
        byte[] buffer=base64Code.getBytes();
        FileOutputStream out = new FileOutputStream(targetPath);
        out.write(buffer);
        out.close();
      }

     public static void main(String[] args){
         try{
            String base64String=${base64字符串};
            decoderBase64File(encodeBase64File("d://2018-11-27 14_34_28_reject_dq.pdf"),"D:/2.pdf");
         }catch(Exception e){
            e.printStackTrace();
         }
     }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市亚情,隨后出現(xiàn)的幾起案子妄痪,更是在濱河造成了極大的恐慌,老刑警劉巖楞件,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件衫生,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡土浸,警方通過(guò)查閱死者的電腦和手機(jī)罪针,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)黄伊,“玉大人泪酱,你說(shuō)我怎么就攤上這事。” “怎么了墓阀?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵愈腾,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我岂津,道長(zhǎng)虱黄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任吮成,我火速辦了婚禮橱乱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘粱甫。我一直安慰自己泳叠,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布茶宵。 她就那樣靜靜地躺著危纫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪乌庶。 梳的紋絲不亂的頭發(fā)上种蝶,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音瞒大,去河邊找鬼螃征。 笑死,一個(gè)胖子當(dāng)著我的面吹牛透敌,可吹牛的內(nèi)容都是我干的盯滚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼酗电,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼魄藕!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起撵术,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤背率,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后荷荤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體退渗,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年蕴纳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了会油。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡古毛,死狀恐怖翻翩,靈堂內(nèi)的尸體忽然破棺而出都许,到底是詐尸還是另有隱情,我是刑警寧澤嫂冻,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布胶征,位于F島的核電站,受9級(jí)特大地震影響桨仿,放射性物質(zhì)發(fā)生泄漏睛低。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一服傍、第九天 我趴在偏房一處隱蔽的房頂上張望钱雷。 院中可真熱鬧,春花似錦吹零、人聲如沸罩抗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)套蒂。三九已至,卻和暖如春茫蛹,著一層夾襖步出監(jiān)牢的瞬間操刀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工麻惶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留馍刮,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓窃蹋,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親静稻。 傳聞我的和親對(duì)象是個(gè)殘疾皇子警没,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容