基于java的百度语音识别示例
来源:互联网 发布:eia数据汇总 编辑:程序博客网 时间:2024/06/01 09:56
SDK以及示例代码下载地址: http://yuyin.baidu.com/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"); }}
2 0
- 基于java的百度语音识别示例
- 基于java的百度语音识别示例
- 【百度语音识别】JavaAPI方式语音识别示例 MP3转PCM文件Java实现
- Android 开发基于百度语音识别技术的小程序
- android之基于百度语音合讯飞语音识别的语音交互
- 基于科大讯飞的语音识别
- 科大讯飞和百度语音平台语音识别Java调用记录
- 百度语音识别的文档链接
- 调用百度语音SDK,简单的语音识别控件
- 百度语音API的Python语音识别实践
- 百度语音识别
- node百度语音识别
- iOS-百度语音识别
- Android 语音识别示例
- 基于TMS320VC5509A的语音识别与控制系统
- 基于matlab的语音识别系统设计
- 基于DBN的语音识别技术分析
- 基于DBN的语音识别技术分析
- android开发---2.eclipse与ndk adb等工具的使用
- java并发备忘
- android开发---3.linux 下使用 javah生成头文件
- ORACLE 11g远程连接数据库
- android开发---4.android开发环境搭建
- 基于java的百度语音识别示例
- 在ubuntu 平台上c / c++使用整理
- android开发---5.android常见错误及解决
- Android的ScrollView使用
- android开发---6在源码目录与ndk-build下编译jni与apk(附demo)
- HTML5+CSS3基础
- Canvas基本矩形的
- 将图片和Base64为互相转换
- android开发---7.常用控件的使用