准備工作:
下載jar包,鏈接:https://pan.baidu.com/s/1qXPgCzy 密碼:xigv
解壓jar包,將jacob.jar復制到工程目錄,右鍵該文件→Build Path→Add to...
將jacob-1.17-M2-x86.dll添加到JDK的bin目錄和Windows的system32目錄(64位系統添加jacob-1.17-M2-x64.dll)
代碼實現如下:
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Test04 { public static void main(String[] args) { ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice"); try { // 音量 0-100 sap.setProperty("Volume", new Variant(100)); // 語音朗讀速度 -10 到 +10 sap.setProperty("Rate", new Variant(-2)); // 獲取執行對象 Dispatch sapo = sap.getObject(); // 執行朗讀 Dispatch.call(sapo, "Speak", new Variant("你好,很高興見到你。")); // 關閉執行對象 sapo.safeRelease(); } catch (Exception e) { e.printStackTrace(); } finally { // 關閉應用程序連接 sap.safeRelease(); } } }
我們可以朗讀文件內容,代碼實現如下:
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Test05 { public static void main(String[] args) throws IOException { ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice"); //輸入文件 File srcFile = new File("E:/tmp/testvoice.txt"); //使用包裝字符流讀取文件 BufferedReader br = new BufferedReader(new FileReader(srcFile)); String content = br.readLine(); try { // 音量 0-100 sap.setProperty("Volume", new Variant(100)); // 語音朗讀速度 -10 到 +10 sap.setProperty("Rate", new Variant(-2)); // 獲取執行對象 Dispatch sapo = sap.getObject(); // 執行朗讀 while (content != null) { Dispatch.call(sapo, "Speak", new Variant(content)); content = br.readLine(); } // 關閉執行對象 sapo.safeRelease(); } catch (Exception e) { e.printStackTrace(); } finally { br.close(); // 關閉應用程序連接 sap.safeRelease(); } } }
出現的問題:
關於java使用jacob.jar調用word的配置問題
最近用到了jacob.jar來轉換word文件,出現一些問題都是關於配置的,先將一些配置說明一下,以供大家參考。
文件下載地址,
https://pan.baidu.com/s/1Fd5d3dvJ8IGeqxuU-vcN3Q
jacob-1.17-M2-x64.dll放到C:\Windows\System32下面(64位)
jacob-1.17-M2-x86.dll 放到C:\Windows\SysWOW64下面(32位)
將jacob.dll拷貝至%JAVA_HOME%\jre\bin目錄。
再把jacob.jar拷入lib下
重啟完成
https://blog.csdn.net/qq_40864915/article/details/86573079
https://blog.csdn.net/qq_34137397/article/details/77814851
1.下載jacob.ja
2.32位操作系統下載:jacob-1.17-M2-x32.dll,64位操作系統下載:jacob-1.17-M2-x64.dll
3.將jacob.jar考到項目中進行構建路徑。
4.將jacob-1.17-M2-x32.dll或者jacob-1.17-M2-x64.dll,考到系統盤:\Windows\System32\下面。
5.將jacob-1.17-M2-x32.dll或者jacob-1.17-M2-x64.dll,考到JDK安裝目錄的bin下面。
然后寫個test方法測試一下即可,測試代碼如下:
/** * @Title: Voice.java * @Package org.util * @Description: TODO該方法的主要作用: * @author A18ccms A18ccms_gmail_com * @date 2017-7-3 下午9:03:45 * @version V1.0 */ package cn.bdqn.bdqn.utils; import org.junit.Test; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; /** * * 項目名稱:avoice * 類名稱:Voice * 類描述: 語音播報工具類 * 創建人:Mu Xiongxiong * 創建時間:2017-7-3 下午9:03:45 * 修改人:Mu Xiongxiong * 修改時間:2017-7-3 下午9:03:45 * 修改備注: * @version * */ public class Voice { /** * * @Title: strat * @Description: 該方法的主要作用:朗讀 * @param @param content * @param @param type 設定文件 0:開始,1停止 * @return 返回類型:void * @throws */ public void strat(String content, int type) { // ?? 這個Sapi.SpVoice是需要安裝什么東西嗎,感覺平白無故就來了 ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice"); // Dispatch是做什么的? Dispatch sapo = sap.getObject(); if (type == 0) { try { // 音量 0-100 sap.setProperty("Volume", new Variant(100)); // 語音朗讀速度 -10 到 +10 sap.setProperty("Rate", new Variant(1.3)); Variant defalutVoice = sap.getProperty("Voice"); Dispatch dispdefaultVoice = defalutVoice.toDispatch(); Variant allVoices = Dispatch.call(sapo, "GetVoices"); Dispatch dispVoices = allVoices.toDispatch(); Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch(); ActiveXComponent voiceActivex = new ActiveXComponent( dispdefaultVoice); ActiveXComponent setvoiceActivex = new ActiveXComponent( setvoice); Variant item = Dispatch.call(setvoiceActivex, "GetDescription"); // 執行朗讀 Dispatch.call(sapo, "Speak", new Variant(content)); } catch (Exception e) { e.printStackTrace(); } finally { sapo.safeRelease(); sap.safeRelease(); } } else { // 停止 try { Dispatch.call(sapo, "Speak", new Variant(content), new Variant( 2)); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } /** * * @Title: test * @Description: 該方法的主要作用:執行朗讀內容 * @param 設定文件 * @return 返回類型:void * @throws */ @Test public void test() { strat("語音朗讀的內容", 0); } }
