本文实例讲述了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