java调用平台提供的接口进行图片识别
2017-06-22 00:00
621 查看
图片识别平台:https://www.juhe.cn/
调用平台提供的接口进行图片识别
注:将本地图片进行Base64位编码
本类只用到这两个方法:encodeImgageToBase64(),readContentFromPost()。
代码:
package com.teamdev.jxbrowser.chromium.demoJD.yyzz;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.imageio.ImageIO;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import com.teamdev.jxbrowser.chromium.demoJD.yyzz.ImageUtils;
import sun.misc.BASE64Encoder;
public class ImageUtils {
/**
*
将本地图片进行Base64位编码
*
*
@param imgUrl
* 图片的url路径,如xxx.jpg
*
@return
*/
@SuppressWarnings("deprecation")
public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageFile);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return java.net.URLEncoder.encode( encoder.encode(outputStream.toByteArray()));// 返回Base64编码过的字节数组字符串
}
/**
* 删除会话文件
* */
public static void DeleteSession() {
String path="F:\\yzm\\yzm.jpg";
File file =new File(path);
if (file.exists()) {
try {
file.delete();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("faile delete session.txt ");
}
}
}
/**
* 下载图片
* */
public static void runDownLoad(String fileurl) {
// 构造URL
URL url;
try {
url = new URL(fileurl);
// 打开URL连接
URLConnection con = (URLConnection) url.openConnection();
// 得到URL的输入流
InputStream input = con.getInputStream();
// 设置数据缓冲
byte[] bs = new byte[1024 * 2];
// 读取到的数据长度
int len;
// 输出的文件流保存图片至本地
String path1 = "F:\\yzm\\yzm.jpg";
File f = new File(path1);
// f.mkdirs();
OutputStream os = new FileOutputStream(path1);
while ((len = input.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
input.close();
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/**
* 向指定 URL 发送POST方法的请求
*
*
@param url
* 发送请求的 URL
*
@param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
/**
* 解析验证码
* @throws Exception
* */
public static String readContentFromPost(String imgPath) throws IOException {
String str = ImageUtils.encodeImgageToBase64(new File(imgPath));
URL postUrl = new URL("http://op.juhe.cn/vercode/index");
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
String content = "codeType=8003&&image=&base64Str=" + str + "&dtype=&key=bdb9286128139fa1a61d8b7ca84ab171";
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb=new StringBuilder();
while ((line = reader.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
//{"reason":"成功的返回","result":"rahu","error_code":0}
String result=sb.toString().split("result\":\"")[1].split("\",")[0];
reader.close();
connection.disconnect();
return result;
}
//测试
public static void main(String[] args) throws IOException{
System.out.println(readContentFromPost("F:\\yzm\\yzm.jpg"));
}
}
调用平台提供的接口进行图片识别
注:将本地图片进行Base64位编码
本类只用到这两个方法:encodeImgageToBase64(),readContentFromPost()。
代码:
package com.teamdev.jxbrowser.chromium.demoJD.yyzz;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.imageio.ImageIO;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import com.teamdev.jxbrowser.chromium.demoJD.yyzz.ImageUtils;
import sun.misc.BASE64Encoder;
public class ImageUtils {
/**
*
将本地图片进行Base64位编码
*
*
@param imgUrl
* 图片的url路径,如xxx.jpg
*
@return
*/
@SuppressWarnings("deprecation")
public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageFile);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return java.net.URLEncoder.encode( encoder.encode(outputStream.toByteArray()));// 返回Base64编码过的字节数组字符串
}
/**
* 删除会话文件
* */
public static void DeleteSession() {
String path="F:\\yzm\\yzm.jpg";
File file =new File(path);
if (file.exists()) {
try {
file.delete();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("faile delete session.txt ");
}
}
}
/**
* 下载图片
* */
public static void runDownLoad(String fileurl) {
// 构造URL
URL url;
try {
url = new URL(fileurl);
// 打开URL连接
URLConnection con = (URLConnection) url.openConnection();
// 得到URL的输入流
InputStream input = con.getInputStream();
// 设置数据缓冲
byte[] bs = new byte[1024 * 2];
// 读取到的数据长度
int len;
// 输出的文件流保存图片至本地
String path1 = "F:\\yzm\\yzm.jpg";
File f = new File(path1);
// f.mkdirs();
OutputStream os = new FileOutputStream(path1);
while ((len = input.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
input.close();
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/**
* 向指定 URL 发送POST方法的请求
*
*
@param url
* 发送请求的 URL
*
@param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
/**
* 解析验证码
* @throws Exception
* */
public static String readContentFromPost(String imgPath) throws IOException {
String str = ImageUtils.encodeImgageToBase64(new File(imgPath));
URL postUrl = new URL("http://op.juhe.cn/vercode/index");
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
String content = "codeType=8003&&image=&base64Str=" + str + "&dtype=&key=bdb9286128139fa1a61d8b7ca84ab171";
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb=new StringBuilder();
while ((line = reader.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
//{"reason":"成功的返回","result":"rahu","error_code":0}
String result=sb.toString().split("result\":\"")[1].split("\",")[0];
reader.close();
connection.disconnect();
return result;
}
//测试
public static void main(String[] args) throws IOException{
System.out.println(readContentFromPost("F:\\yzm\\yzm.jpg"));
}
}
相关文章推荐
- 用Java调用中国网建提供的SMS短信平台接口实现向移动端发送短信
- 微博分享的时候是获取网络的图片需要在微博平台进行获取接口调用权限
- java调用tensorflow模型进行图片分类识别
- Java调用OCR进行图片识别
- Windows下Java调用OCR进行图片识别
- java调用百度接口 识别图片文字
- Java调用OCR进行图片识别
- 【经验随笔】Java通过代理访问互联网平台提供的WebService接口的一种方法
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- Java Sound API是javaSE平台提供底层的(low-level)处理声音接口。
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- Java代码调用Net接口进行Webservices对接
- JAVA 调用接口或读取某网站源代码进行动态解析
- JAVA利用HttpClient进行HTTPS接口调用
- webservice实际应用之调用外部系统提供的服务端地址,写java的webservice客户端程序并通过接口给外部系统传数据
- java 使用URLConnection 进行HTTP接口调用
- Java开发调用PageOffice提供的Word手写批注接口
- JAVA利用HttpClient进行HTTPS接口调用
- 【图片识别】Java中使用tess4J进行图片文字识别(支持中文)(转)
- Java调用语言技术平台(LTP) 3.4.0进行汉语文本处理经验总结