本文實例講述了java實現的百度語音識別功能。分享給大家供大家參考,具體如下:
sdk以及示例代碼下載地址: http://yuyin.baidu.com/sdk
最近一直在搞java,就選擇了java工程。將代碼拷過去。同時復制文件“test.pcm”到工程目錄下。就基本上可以了。
注:test.pcm是語音文件,可以用audacity軟件打開,選擇 文件->導入->裸數據。 設置采樣率為8000hz。點擊播放就能聽見聲音了。
這個時候程序跑起來還有問題,需要將apikey 以及secretkey填寫上。這兩個值是你申請應用對應的分配好的。
cuid填本機mac地址就可以了,這個值我試過好像無所謂沒啥要求。
程序能跑起來,并且按照正常返回識別的語音結果。但是返回結果的編碼為gbk,所以漢字顯示為亂碼,需要對其進行一次轉碼。轉碼的代碼是我自己加上去的。
下面貼代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
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; public class sample { private static final string serverurl = "http://vop.baidu.com/server_api" ; private static string token = "" ; private static final string testfilename = "test.pcm" ; // 百度語音提供技術支持 //put your own params here // 下面3個值要填寫自己申請的app對應的值 private static final string apikey = "" ; private static final string secretkey = "" ; private static final string cuid = "" ; public static void main(string[] args) throws exception { gettoken(); method1(); method2(); } private static void 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" ); } private static void 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); } private static void 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))); } private static 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(); } private static byte [] loadfile(file file) throws ioexception { inputstream is = new fileinputstream(file); long length = file.length(); byte [] bytes = new byte [( int ) length]; int offset = 0 ; int numread = 0 ; while (offset < bytes.length && (numread = is.read(bytes, offset, bytes.length - offset)) >= 0 ) { offset += numread; } if (offset < bytes.length) { is.close(); throw new ioexception( "could not completely read file " + file.getname()); } is.close(); return bytes; } // gbk編碼轉為utf-8 private static string getutf8string(string s) throws unsupportedencodingexception { stringbuffer sb = new stringbuffer(); sb.append(s); string xmlstring = "" ; string xmlutf8 = "" ; xmlstring = new string(sb.tostring().getbytes( "gbk" )); xmlutf8 = urlencoder.encode(xmlstring , "gbk" ); return urldecoder.decode(xmlutf8, "utf-8" ); } } |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/eclipse_c/article/details/51803886