
Java应用之图片美化增强AI接口调用手册
在调合合AI平台提供的图片美化增强API接口,API平台链接:https://ai.ccint.com/doc/api/crop_enhance_image, 因为有遇到一些问题,写篇博客记录一下
API文档提供的说明: url中参数app_key为个人中心实例的app_key
请求方式: POST
返回类型: JSON
POST BODY请求字段描述
image_data 必填,图像的base64串 app_secret 必填,个人中心实例的app_secret scan-m 扫描模式, 建议为 1 detail 锐化程度,建议为-1 contrast 对比度 ,建议为 0 bright 增亮 ,建议为 0 enhanceMode 增强模式,1:增亮,2:增强并锐化,3:黑白,4:灰度 "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:灰度
{
"code": 200,
"message": "success",
"result": “图片base64信息”
}
失败返回示例
{
"code":30301,
"message":"额度已用完,请充值后使用"
}
返回码说明
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;
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);
注意要点:
写文件流时记得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<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"); // 设置请求方式
//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;
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字符串和文件转换工具类:
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){
String base64String=${base64字符串};
decoderBase64File(encodeBase64File("d://2018-11-27 14_34_28_reject_dq.pdf"),"D:/2.pdf");
}catch(Exception e){
e.printStackTrace();
收藏
回复
删除帖子
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源
去关联
添加资源
相关推荐
-
鸿蒙内核移植
手册
之 鸿蒙介绍
韦东山
• 1.2w浏览
• 1回复
-
java
动态代理实现
接口
调用
jkfox
• 1.1w浏览
• 0回复
-
#2020征文-开发板# 用鸿蒙开发
AI
应用
(一)硬件篇
bluishfish
• 1.7w浏览
• 4回复
-
java
调用
HTTP
接口
(Get请求和Post请求)
huatechinfo
• 1.9w浏览
• 0回复
-
#2020征文-开发板# 用鸿蒙开发
AI
应用
(二)系统篇
bluishfish
• 1.2w浏览
• 4回复
-
#2020征文-开发板# 用鸿蒙开发
AI
应用
(三)软件篇
bluishfish
• 1.5w浏览
• 8回复
-
#2020征文-开发板# 用鸿蒙开发
AI
应用
(四)Helloworld
bluishfish
• 1.1w浏览
• 3回复
-
#2020征文-开发板# 用鸿蒙开发
AI
应用
(六)UI篇
bluishfish
• 1.3w浏览
• 4回复
-
用鸿蒙开发
AI
应用
(七)触摸屏控制LED
bluishfish
• 1.5w浏览
• 4回复
-
网络
图片
_Image渲染网络
图片
BLUESKYHOST
• 1.3w浏览
• 2回复
-
HarmonyOS Sample 之 ServiceAbility 跨设备
接口
调用
Buty9147
• 9346浏览
• 2回复
-
全网最全面的鸿蒙资源查询
手册
——UltimateHarmonyReference
朱伟ISRC
• 1.1w浏览
• 19回复
-
HarmonyOS JS FA
调用
JAVA
PA 机制
中软HOS小鸿
• 1.1w浏览
• 4回复
-
HarmonyOS入门
手册
:《鸿蒙生态-开启万物互联智慧新时代》
开源活动小助手
• 1.5w浏览
• 36回复
-
HarmonyOS Sample 之
AI
能力
应用之
报菜名儿
Buty9147
• 6403浏览
• 2回复
-
JS FA
调用
PA (展示本地相册
图片
)
ruchan
• 7220浏览
• 1回复
-
OpenHarmony
AI
推理任务管理与统一推理
接口
介绍
深开鸿Kaihong
• 9404浏览
• 5回复
-
#夏日挑战赛#OpenHarmony——hilog命令行使
用手册
深开鸿Kaihong
• 1.0w浏览
• 2回复
-
一款吊炸天的
AI
图片
增强
工具!
gnt_xxy
• 2221浏览
• 0回复
帖子
视频
声望
粉丝
关注
最近发布
-
一文读懂开源项目 OpenHarmony
2021-06-08 10:13:32发布
-
华为鸿蒙系统确认适配高通/联发科手机!曝OV魅族有意采用
2021-05-08 10:55:26发布