基於java的百度語音識別示例
阿新 • • 發佈:2019-02-15
最近一直在搞java,就選擇了java工程。將程式碼拷過去。同時複製檔案“test.pcm”到工程目錄下。就基本上可以了。
注:test.pcm是語音檔案,可以用audacity軟體開啟,選擇 檔案->匯入->裸資料。 設定取樣率為8000Hz。點選播放就能聽見聲音了。
這個時候程式跑起來還有問題,需要將apiKey 以及secretKey填寫上。這兩個值是你申請應用對應的分配好的。
cuid填本機mac地址就可以了,這個值我試過好像無所謂沒啥要求。
程式能跑起來,並且按照正常返回識別的語音結果。但是返回結果的編碼為GBK,所以漢字顯示為亂碼,需要對其進行一次轉碼。轉碼的程式碼是我自己加上去的。
下面貼程式碼:
- package com.baidu.speech.serviceapi;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
- import javax.xml.bind.DatatypeConverter;
- import org.json.JSONObject;
- publicclass Sample {
- privatestaticfinal String serverURL = "http://vop.baidu.com/server_api";
- privatestatic
- privatestaticfinal String testFileName = "test.pcm"; // 百度語音提供技術支援
- //put your own params here
- // 下面3個值要填寫自己申請的app對應的值
- privatestaticfinal String apiKey = "";
- privatestaticfinal String secretKey = "";
- privatestaticfinal String cuid = "";
- publicstaticvoid main(String[] args) throws Exception {
- getToken();
- method1();
- method2();
- }
- privatestaticvoid getToken() throws Exception {
- String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" +
- "&client_id=" + apiKey + "&client_secret=" + secretKey;
- HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL).openConnection();
- token = new JSONObject(printResponse(conn)).getString("access_token");
- }
- privatestaticvoid method1() throws Exception {
- File pcmFile = new File(testFileName);
- HttpURLConnection conn = (HttpURLConnection) new URL(serverURL).openConnection();
- // construct params
- JSONObject params = new JSONObject();
- params.put("format", "pcm");
- params.put("rate", 8000);
- params.put("channel", "1");
- params.put("token", token);
- params.put("lan", "zh");
- params.put("cuid", cuid);
- params.put("len", pcmFile.length());
- params.put("speech", DatatypeConverter.printBase64Binary(loadFile(pcmFile)));
- // add request header
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- // send request
- DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
- wr.writeBytes(params.toString());
- wr.flush();
- wr.close();
- printResponse(conn);
- }
- privatestaticvoid method2() throws Exception {
- File pcmFile = new File(testFileName);
- HttpURLConnection conn = (HttpURLConnection) new URL(serverURL
- + "?cuid=" + cuid + "&token=" + token).openConnection();
- // add request header
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "audio/pcm; rate=8000");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- // send request
- DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
- wr.write(loadFile(pcmFile));
- wr.flush();
- wr.close();
- System.out.println(getUtf8String(printResponse(conn)));
- }
- privatestatic String printResponse(HttpURLConnection conn) throws Exception {
- if (conn.getResponseCode() != 200) {
- // request error
- System.out.println("conn.getResponseCode() = " + conn.getResponseCode());
- return"";
- }
- InputStream is = conn.getInputStream();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- String line;
- StringBuffer response = new StringBuffer();
- while ((line = rd.readLine()) != null) {
- response.append(line);
- response.append('\r');
- }
- rd.close();
- System.out.println(new JSONObject(response.toString()).toString(4));
- return response.toString();
- }
- privatestaticbyte[] loadFile(File file) throws IOException {
- InputStream is = new FileInputStream(file);
- long length = file.length();
- byte[] bytes = newbyte[(int) length];
- int offset = 0;