亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實(shí)現(xiàn)的百度語音識別功能示例

 更新時(shí)間:2018年08月23日 10:26:05   作者:eclipse_c  
這篇文章主要介紹了Java實(shí)現(xiàn)的百度語音識別功能,較為簡明扼要的分析了Java調(diào)用百度語音接口相關(guān)操作步驟,并給出了具體的語音識別用法代碼示例,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)的百度語音識別功能。分享給大家供大家參考,具體如下:

SDK以及示例代碼下載地址: http://yuyin.baidu.com/sdk

最近一直在搞java,就選擇了java工程。將代碼拷過去。同時(shí)復(fù)制文件“test.pcm”到工程目錄下。就基本上可以了。

注:test.pcm是語音文件,可以用audacity軟件打開,選擇 文件->導(dǎo)入->裸數(shù)據(jù)。 設(shè)置采樣率為8000Hz。點(diǎn)擊播放就能聽見聲音了。

這個(gè)時(shí)候程序跑起來還有問題,需要將apiKey 以及secretKey填寫上。這兩個(gè)值是你申請應(yīng)用對應(yīng)的分配好的。

cuid填本機(jī)mac地址就可以了,這個(gè)值我試過好像無所謂沒啥要求。

程序能跑起來,并且按照正常返回識別的語音結(jié)果。但是返回結(jié)果的編碼為GBK,所以漢字顯示為亂碼,需要對其進(jìn)行一次轉(zhuǎn)碼。轉(zhuǎn)碼的代碼是我自己加上去的。

下面貼代碼:

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"; // 百度語音提供技術(shù)支持
  //put your own params here
  // 下面3個(gè)值要填寫自己申請的app對應(yīng)的值
  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編碼轉(zhuǎn)為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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • SpringCloud Gateway跨域配置代碼實(shí)例

    SpringCloud Gateway跨域配置代碼實(shí)例

    這篇文章主要介紹了SpringCloud Gateway跨域配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • spring @profile注解的使用方法

    spring @profile注解的使用方法

    本篇文章主要介紹了spring @profile注解的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • 一文理清什么是BIO以及如何使用

    一文理清什么是BIO以及如何使用

    這篇文章主要介紹了什么是BIO以及如何使用,BIO英文全名是blockingIO,也叫做阻塞IO,是最容易理解、最容易實(shí)現(xiàn)的IO工作方式,本文就來通過一些簡單的示例為大家講講BIO吧,需要的朋友可以參考下
    2023-10-10
  • MyBatis-Plus非表字段的三種處理方法小結(jié)

    MyBatis-Plus非表字段的三種處理方法小結(jié)

    這篇文章主要介紹了MyBatis-Plus非表字段的三種處理方法小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 快速搭建Spring Boot+MyBatis的項(xiàng)目IDEA(附源碼下載)

    快速搭建Spring Boot+MyBatis的項(xiàng)目IDEA(附源碼下載)

    這篇文章主要介紹了快速搭建Spring Boot+MyBatis的項(xiàng)目IDEA(附源碼下載),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • PowerJob的MapProcessor工作流程源碼解讀

    PowerJob的MapProcessor工作流程源碼解讀

    這篇文章主要為大家介紹了PowerJob的MapProcessor工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Java并發(fā)編程之StampedLock鎖介紹

    Java并發(fā)編程之StampedLock鎖介紹

    這篇文章主要介紹了Java并發(fā)編程之StampedLock鎖,StampedLock是并發(fā)包里面JDK8版本新增的一個(gè)鎖,下文更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • 例題詳解Java?dfs與記憶化搜索和分治遞歸算法的使用

    例題詳解Java?dfs與記憶化搜索和分治遞歸算法的使用

    遞歸指函數(shù)調(diào)用自身。常用的遞歸算法有dfs(深度優(yōu)先搜索)、記憶化搜索和分治,接下來將用幾個(gè)算法題來帶你熟練掌握它
    2022-04-04
  • 初識MyBatis及基本配置和執(zhí)行

    初識MyBatis及基本配置和執(zhí)行

    這篇文章主要介紹了初識MyBatis的基本知識,文中給大家提到了mybatis基本配置和執(zhí)行過程,需要的朋友可以參考下
    2017-11-11
  • Java遠(yuǎn)程共享目錄的操作代碼

    Java遠(yuǎn)程共享目錄的操作代碼

    這篇文章主要介紹了java操作遠(yuǎn)程共享目錄的實(shí)現(xiàn)代碼,非常不粗,具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-08-08

最新評論