package com.skynet.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
import com.skynet.pojo.AddoilMark;
/**
* @ClassName: IdentificationTicketUtil
* @Description: 百度识别图片中文字的类
* @author lizhenjie
* @date 2019年1月24日 上午10:57:38
* @version 1.0
*/
public class IdentificationTicketUtil {
//设置APPID/AK/SK
public static final String APP_ID = "11722411";
public static final String API_KEY = "GBzpKtWlDTigpuRP1NQHLlmm";
public static final String SECRET_KEY = "lWNw6sGGjomZUXtNNED7a6XdO3cztWmY";
public static void main(String[] args) throws Exception{
// String url = "https://user-file.obs.cn-north-1.myhwclouds.com/shop-heatmap/20190122/d46f814716f8a052cf2f30092c5212b520190122172827.jpg";
// saveHeaderPic(url);
}
public static AddoilMark saveHeaderPic(String url) throws Exception {
AddoilMark addoilMark = new AddoilMark();
try {
URL file = new URL(url);
InputStream is = file.openStream();
byte[] readInputStream = null;
readInputStream = readInputStream(is);
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
Map<String,Object> map = new HashMap<>();
JSONObject res = client.basicGeneral(readInputStream, new HashMap<String, String>());
String str = res.toString(2);
int one = getCharacterPosition(str, "元", 1);
int pat = getCharacterPosition(str, "中", 1);
int two = getCharacterPosition(str, "元", 2);
int three = getCharacterPosition(str, "元", 3);
int four = getCharacterPosition(str, "元", 4);
int L = getCharacterPosition(str, "L", 1);
String stationName = str.substring(pat);
stationName = stationName.substring(0, 7);
addoilMark.setStationName(stationName);
String orderId = str.substring(str.indexOf("订单号")+4, str.indexOf("车牌号"));
orderId = orderId.substring(0,24);
addoilMark.setOrderId(orderId);
String carId = str.substring(str.indexOf("车牌号")+4);
carId = carId.substring(0, 8);
addoilMark.setCarId(carId);
String oilName = str.substring(str.indexOf("油品种类")+5);
oilName = oilName.substring(0,3);
addoilMark.setOilName(oilName);
String oilAmount = str.substring(str.indexOf("加油金额")+5,one);
addoilMark.setOilAmount(oilAmount);
String real = str.substring(str.indexOf("支付金额")+5, two);
addoilMark.setRealAmount(real);
String totalDiscount = str.substring(str.indexOf("优惠金额")+5,three);
addoilMark.setTotalDiscount(totalDiscount);
String totalAmount = str.substring(str.indexOf("实际加油金额")+7,four);
addoilMark.setTotalAmount(totalAmount);
String oilNum = str.substring(str.indexOf("实际加油升数")+7,L+1);
addoilMark.setOilNum(oilNum);
String paymentTypes = str.substring(str.indexOf("付款方式")+5);
paymentTypes = paymentTypes.substring(0, 4);
addoilMark.setPaymentTypes(paymentTypes);
String payee = str.substring(str.indexOf("收款方")+4);
payee = payee.substring(0, 14);
addoilMark.setPayee(payee);
}catch(Exception e){
e.printStackTrace();
}
return addoilMark;
}
/**
* @Title: readInputStream
* @Description: 把字节流转换成字节数组
* @param @param is
* @param @return
* @param @throws IOException 设定文件
* @return byte[] 返回类型
* @throws
*/
private static byte[] readInputStream(InputStream is) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while( (len=is.read(buffer)) != -1 ){
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
is.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
/**
* @Title: getCharacterPosition
* @Description: 获取字符在字符串中第n次出现的位置
* @param @param source
* @param @param target
* @param @param n
* @param @return 设定文件
* @return int 返回类型
* @throws
*/
public static int getCharacterPosition(String source, String target, int n) {
// 这里是获取目标符号的位置
Matcher slashMatcher = Pattern.compile(target).matcher(source);
int mIdx = 0;
while (slashMatcher.find()) {
mIdx++;
// 当目标符号第N次出现的位置
if (mIdx == n) {
break;
}
}
return slashMatcher.start();
}
}