登录发现更多内容
首页
资讯
分类
目录
索引
酷站
发帖
充值
用户名
Email
自动登录
找回密码
密码
登录
立即注册
只需一步,快速开始
立即登录
立即注册
其他登录
QQ
微信
懒人官网
门户
Portal
导读
Guide
视频直播网校
VIP会员
广播
Follow
钱包
合鑫云真机
群组
Group
任务大厅
动态
Space
淘帖
Collection
日志
Blog
相册
Album
分享
Share
记录
Doing
排行榜
Ranklist
问AI
提问
搜索
搜索
热搜:
活动
交友
discuz
本版
文章
帖子
群组
用户
我的广告
好友
收藏
道具
勋章
任务
淘帖
动态
日志
相册
分享
记录
留言板
广播
群组
门户
导读
排行榜
设置
我的收藏
退出
首页
问答专区
›
有人把这个程序改成懒人的脚本么
发布主题
返回列表
有人把这个程序改成懒人的脚本么
0
回复
2450
查看
[ 复制链接 ]
wgxxdr
当前离线
积分
176
wgxxdr
7
主题
14
回帖
176
积分
注册会员
注册会员, 积分 176, 距离下一级还需 24 积分
注册会员, 积分 176, 距离下一级还需 24 积分
积分
176
收听TA
发消息
2022-3-28 02:43:42
显示全部楼层
阅读模式
package test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.ParseException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Encoder;
public class InvoiceOcr {
//接口地址
private static String apiURL = "http://xxxx:8866/predict/chinese_ocr_db_crnn_server";
//private Log logger = LogFactory.getLog(this.getClass());
private HttpClient httpClient = null;
private HttpPost method = null;
private long startTime = 0L;
private long endTime = 0L;
private int status = 0;
/**
* 接口地址
*
* @param url
*/
public InvoiceOcr(String url) {
if (url != null) {
this.apiURL = url;
}
if (apiURL != null) {
httpClient = new DefaultHttpClient();
method = new HttpPost(apiURL);
}
}
/**
* 调用 API
*
* @param parameters
* @return
*/
public String post(String parameters) {
String body = null;
if (method != null & parameters != null && !"".equals(parameters.trim())) {
try {
// 建立一个NameValuePair数组,用于存储欲传送的参数
method.addHeader("Content-type","application/json");
method.setHeader("Accept", "application/json");
method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8")));
startTime = System.currentTimeMillis();
HttpResponse response = httpClient.execute(method);
endTime = System.currentTimeMillis();
int statusCode = response.getStatusLine().getStatusCode();
//logger.info("statusCode:" + statusCode);
//logger.info("调用API 花费时间(单位:毫秒):" + (endTime - startTime));
if (statusCode != HttpStatus.SC_OK) {
//logger.error("Method failed:" + response.getStatusLine());
status = 1;
}
body = EntityUtils.toString(response.getEntity(),"utf-8");
} catch (IOException e) {
// 网络错误
status = 3;
System.out.println(e);
} finally {
//logger.info("调用接口状态:" + status);
}
}
return body;
}
public static void main(String[] args) throws ParseException {
InvoiceOcr ac = new InvoiceOcr(apiURL);
JSONArray arry = new JSONArray();
JSONObject j = new JSONObject();
arry.add(imageToBase64("E:\\DESK_LIVE\\20200907\\first\\image2.png"));
j.put("images",arry);
String result = ac.post(j.toJSONString());
System.out.println(result);
}
public static String imageToBase64(String path) {
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(path);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
/**
* 0.成功 1.执行方法失败 2.协议错误 3.网络错误
*
* @return the status
*/
public int getStatus() {
return status;
}
/**
* @param status
* the status to set
*/
public void setStatus(int status) {
this.status = status;
}
/**
* @return the startTime
*/
public long getStartTime() {
return startTime;
}
/**
* @return the endTime
*/
public long getEndTime() {
return endTime;
}
}
复制代码
回复
使用道具
举报
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖并转播
回帖后跳转到最后一页
浏览过的版块
源码分享
懒人BUG&建议专区
首页
分类
目录
索引
我的