您的位置:首页 > 编程语言 > Java开发

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"));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 图片识别