Java实现的百度语音识别功能示例
程序员文章站
2024-02-25 20:35:21
本文实例讲述了java实现的百度语音识别功能。分享给大家供大家参考,具体如下:
sdk以及示例代码下载地址:
最近一直在搞java,就选择了java工程。将代码拷过去...
本文实例讲述了java实现的百度语音识别功能。分享给大家供大家参考,具体如下:
sdk以及示例代码下载地址:
最近一直在搞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; 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相关内容感兴趣的读者可查看本站专题:《java面向对象程序设计入门与进阶教程》、《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。