天天看點

百度api文字識别

項目結構

百度api文字識别

pom依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wsk</groupId>
    <artifactId>baidu</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.46</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.12</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>ROOT</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
           

AuthService類

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * @author wll
 * @description 描述
 */
public class AuthService {
	/**
	 * 擷取權限token
	 *
	 * @return 傳回示例: { "access_token":
	 *         "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567",
	 *         "expires_in": 2592000 }
	 */
	public static String getAuth() {
		// 官網擷取的 API Key 更新為你注冊的
		String clientId = "*****";
		// 官網擷取的 Secret Key 更新為你注冊的
		String clientSecret = "*****";
		return getAuth(clientId, clientSecret);
	}

	/**
	 * 擷取API通路token 該token有一定的有效期,需要自行管理,當失效時需重新擷取.
	 *
	 * @param ak
	 *            - 百度雲官網擷取的 API Key
	 * @param sk
	 *            - 百度雲官網擷取的 Securet Key
	 * @return assess_token 示例:
	 *         "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
	 */
	private static String getAuth(String ak, String sk) {
		// 擷取token位址
		String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
		String getAccessTokenUrl = authHost
				// 1. grant_type為固定參數
				+ "grant_type=client_credentials"
				// 2. 官網擷取的 API Key
				+ "&client_id=" + ak
				// 3. 官網擷取的 Secret Key
				+ "&client_secret=" + sk;
		try {
			URL realUrl = new URL(getAccessTokenUrl);
			// 打開和URL之間的連接配接
			HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			// 擷取所有響應頭字段
			Map<String, List<String>> map = connection.getHeaderFields();
			// 周遊所有的響應頭字段
			for (String key : map.keySet()) {
				System.err.println(key + "--->" + map.get(key));
			}
			// 定義 BufferedReader輸入流來讀取URL的響應
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			StringBuilder result = new StringBuilder();
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
			/**
			 * 傳回結果示例
			 */
			System.err.println("result:" + result);
			JSONObject jsonObject = JSONObject.parseObject(result.toString());
			return jsonObject.getString("access_token");
		} catch (Exception e) {
			System.err.printf("擷取token失敗!");
			e.printStackTrace(System.err);
		}
		return null;
	}

	public static void main(String[] args) {
		getAuth();
	}

}
           

BaseImg64

import org.apache.commons.codec.binary.Base64;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @author wll
 * @description 圖檔轉化base64後再UrlEncode結果
 */
public class BaseImg64 {
    /**
     * 将一張本地圖檔轉化成Base64字元串
     *
     * @param imgPath 本地圖檔位址
     * @return 圖檔轉化base64後再UrlEncode結果
     */
    public static String getImageStrFromPath(String imgPath) {
        InputStream in;
        byte[] data = null;
        // 讀取圖檔位元組數組
        try {
            in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 對位元組數組Base64編碼
        Base64 base64 = new Base64();
        // 傳回Base64編碼過再URLEncode的位元組數組字元串
        return URLEncoder.encode(base64.encodeToString(data));
    }
}
           

Check

import org.apache.http.HttpResponse;
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 java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * 圖像文字識别
 *
 * @Author : wll
 * @Date :2019/5/20 10:25
 */

public class Check {
	//營業執照接口
    private static final String POST_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license?access_token=" + AuthService.getAuth();

    /**
     * 識别本地圖檔的文字
     *
     * @param path 本地圖檔位址
     * @return 識别結果,為json格式
     * @throws URISyntaxException URI打開異常
     * @throws IOException        io流異常
     */
    public static String checkFile(String path) throws URISyntaxException, IOException {
        File file = new File(path);
        if (!file.exists()) {
            throw new NullPointerException("圖檔不存在");
        }
        String image = BaseImg64.getImageStrFromPath(path);
        String param = "image=" + image;
        return post(param);
    }

    /**
     * @param url 圖檔url
     * @return 識别結果,為json格式
     */
    public static String checkUrl(String url) throws IOException, URISyntaxException {
        String param = "url=" + url;
        return post(param);
    }

    /**
     * 通過傳遞參數:url和image進行文字識别
     *
     * @param param 區分是url還是image識别
     * @return 識别結果
     * @throws URISyntaxException URI打開異常
     * @throws IOException        IO流異常
     */
    private static String post(String param) throws URISyntaxException, IOException {
        //開始搭建post請求
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost();
        URI url = new URI(POST_URL);
        post.setURI(url);
        //設定請求頭,請求頭必須為application/x-www-form-urlencoded,因為是傳遞一個很長的字元串,不能分段發送
        post.setHeader("Content-Type", "application/x-www-form-urlencoded");
        StringEntity entity = new StringEntity(param);
        post.setEntity(entity);
        HttpResponse response = httpClient.execute(post);
        System.out.println(response.toString());
        if (response.getStatusLine().getStatusCode() == 200) {
            String str;
            try {
                /*讀取伺服器傳回過來的json字元串資料*/
                str = EntityUtils.toString(response.getEntity());
                System.out.println(str);
                return str;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public static void main(String[] args) {
    	//C:\Users\Administrator\Desktop\timg.jpg
        String path = "C:\\Users\\Administrator\\Desktop\\timg.jpg";
        try {
            long now = System.currentTimeMillis();
            checkFile(path);
//            checkUrl(path);
            System.out.println("耗時:" + (System.currentTimeMillis() - now) / 1000 + "s");
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }
}
           

百度智能雲擷取 API Key和Secret Key

繼續閱讀