Java應用圖片美化增強AI接口調用手冊

      網友投稿 747 2022-05-30

      在調合合AI平臺提供的圖片美化增強API接口,API平臺鏈接:https://ai.ccint.com/doc/api/crop_enhance_image, 因為有遇到一些問題,寫篇博客記錄一下

      API文檔提供的說明: url中參數app_key為個人中心實例的app_key

      請求方式: POST

      返回類型: JSON

      POST BODY請求字段描述

      POST BODY,接口要求以Post body方式發送,因為要傳base64字符串,請求參數過長有400錯誤的

      { "image_data": "", // 必填,圖像的base64串 "app_secret": "" // 必填,個人中心實例的app_secret "scan-m": 1, //掃描模式, 建議為 1 "detail": -1, //銳化程度,建議為-1 "contrast": 0, //對比度 ,建議為 0 "bright": 0, //增亮 ,建議為 0 "enhanceMode": 0 //增強模式,1:增亮,2:增強并銳化,3:黑白,4:灰度 }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      提示:POST BODY 為 JSON字符串。

      返回字段描述

      正常返回示例

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

      1

      2

      3

      4

      5

      失敗返回示例

      { "code":30301, "message":"額度已用完,請充值后使用" }

      1

      2

      3

      4

      5

      6

      返回碼說明

      API文檔提供的實例代碼:

      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"); // 設置請求方式 conn.setRequestProperty("Content-Type", "application/json"); // 設置發送數據的 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("發送 POST 請求出現異常!" + 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); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      Java應用之圖片美化增強AI接口調用手冊

      69

      70

      71

      72

      73

      74

      75

      76

      注意要點:

      寫文件流時記得outputstream要flush,才能拿到數據

      接口返回的json格式的數據,同時帶有base64的字符串,所以需要json解析一下,然后調工具類,將base64字符串轉換為文件,保存在本地,下面給出調用的代碼,僅供參考

      /** * 圖片切邊增強接口調用 * @author nicky.ma * @date 2019年5月20日下午3:44:27 * @param scanM 掃描模式, 建議為 1 * @param bright 增亮 ,建議為 0 * @param contrast 對比度 ,建議為 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轉換 final String imgData = imageToBase64(sourcePath); Map paramsMap=new HashMap(){ 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"); // 設置請求方式 //conn.setRequestProperty("transfer-encoding","chunked"); conn.setRequestProperty("Content-Type", "application/json"); // 設置發送數據的 conn.connect(); out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.append(param); //要記得flush,才能拿到數據 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("圖片增強后文件大小:{}"+new File(destPath).length()/1024+"KB"); } conn.disconnect(); } catch (Exception e) { logger.error("AI平臺接口調用異常:{}"+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); }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      82

      83

      84

      85

      86

      87

      88

      89

      90

      91

      92

      93

      94

      95

      96

      97

      98

      99

      100

      101

      102

      103

      104

      105

      106

      107

      108

      109

      110

      111

      112

      113

      base64字符串和文件轉換工具類:

      import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.commons.codec.binary.Base64; public class FileBase64Util{ /** * 將文件轉成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(); } } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      AI Java

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:使用 PyQGIS 和 OSRM 將 GPS 捕捉軌跡應用到道路
      下一篇:記住:永遠不要在 MySQL 中使用 UTF-8
      相關文章
      亚洲色欲色欲综合网站| 久久99国产亚洲高清观看首页| 亚洲午夜精品久久久久久浪潮| 亚洲韩国在线一卡二卡| 亚洲综合日韩中文字幕v在线| 亚洲AV无一区二区三区久久| 亚洲熟伦熟女新五十路熟妇| 婷婷综合缴情亚洲狠狠尤物| 国产亚洲精品美女久久久久| 自拍偷自拍亚洲精品偷一| 在线观看亚洲网站| 亚洲国产av一区二区三区| 亚洲国产黄在线观看| 亚洲男人在线无码视频| 久久影视综合亚洲| 国产成人综合亚洲AV第一页 | 亚洲熟伦熟女新五十路熟妇| 亚洲成aⅴ人片久青草影院| 亚洲精品无码久久毛片| 中文字幕在线亚洲精品 | 亚洲情综合五月天| 亚洲AV永久无码精品成人| 亚洲色图国产精品| 亚洲人成网站在线观看播放青青| 亚洲a级在线观看| 亚洲乱色伦图片区小说| AV激情亚洲男人的天堂国语| 国产亚洲高清在线精品不卡| 亚洲伦乱亚洲h视频| 亚洲日韩精品一区二区三区无码| 亚洲国产精品一区二区成人片国内| 国产A在亚洲线播放| 久久精品亚洲中文字幕无码麻豆| 亚洲精品动漫在线| 亚洲国产日韩视频观看| 亚洲色大成网站www永久网站| 国产精品亚洲综合网站| 亚洲愉拍99热成人精品热久久| 五月天网站亚洲小说| 亚洲理论片在线中文字幕| 亚洲jjzzjjzz在线播放|