windows 使用jacob实现文文字语音阅读

加载dll文件,改写LibraryLoader

public final class LibraryLoader {
	/**
	 * Name of system property (currently <tt>jacob.dll.path</tt>) that may
	 * contain an absolute path to the JNI library.
	 */
	public static final String JACOB_DLL_PATH = "jacob.dll.path";

	/**
	 * Name of system property (currently <tt>jacob.dll.name</tt>) that may
	 * contain an alternate name for the JNI library (default is 'jacob').
	 */
	public static final String JACOB_DLL_NAME = "jacob.dll.name";

	/**
	 * Name of system property (currently <tt>jacob.dll.name</tt>) that may
	 * contain an alternate name for the JNI library (default is 'jacob'), 32
	 * bit windows.
	 */
	public static final String JACOB_DLL_NAME_X86 = "jacob.dll.name.x86";

	/**
	 * Name of system property (currently <tt>jacob.dll.name</tt>) that may
	 * contain an alternate name for the JNI library (default is 'jacob'), 64
	 * bit windows.
	 */
	public static final String JACOB_DLL_NAME_X64 = "jacob.dll.name.x64";

	/**
	 * Appended to "jacob" when building DLL name This string must EXACTLY match
	 * the string in the build.xml file
	 */
	public static final String DLL_NAME_MODIFIER_32_BIT = "x86";
	/**
	 * Appended to "jacob" when building DLL name This string must EXACTLY match
	 * the string in the build.xml file
	 */
	public static final String DLL_NAME_MODIFIER_64_BIT = "x64";

	/**
	 * Load the jacob dll either from an absolute path or by a library name,
	 * both of which may be defined in various ways.
	 * 
	 * @throws UnsatisfiedLinkError
	 *             if the library does not exist.
	 */
	public static void loadJacobLibrary() {
		// In some cases, a library that uses Jacob won't be able to set system
		// properties
		// prior to Jacob being loaded. The resource bundle provides an
		// alternate way to
		// override DLL name or path that will be loaded with Jacob regardless
		// of other
		// initialization order.
		ResourceBundle resources = null;
		Set<String> keys = new HashSet<String>();
		try {
			resources = ResourceBundle.getBundle(LibraryLoader.class.getName(),
					Locale.getDefault(), LibraryLoader.class.getClassLoader());
			for (Enumeration<String> i = resources.getKeys(); i
					.hasMoreElements();) {
				String key = i.nextElement();
				keys.add(key);
			}
		} catch (MissingResourceException e) {
			// Do nothing. Expected.
		}

		// First, check for a defined PATH. System property overrides resource
		// bundle.
		String path = System.getProperty(JACOB_DLL_PATH);
		if (path == null && resources != null && keys.contains(JACOB_DLL_PATH)) {
			path = (String) resources.getObject(JACOB_DLL_PATH);
		}

		if (path != null) {
			JacobObject.debug("Loading library " + path
					+ " using System.loadLibrary ");
			System.load(path);
		} else {
			// Path was not defined, so use the OS mechanism for loading
			// libraries.
			// Check for a defined NAME. System property overrides resource
			// bundle.
			/*String name = null;

			if (System.getProperty(JACOB_DLL_NAME) != null) {
				name = System.getProperty(JACOB_DLL_NAME);
			} else if (System.getProperty(JACOB_DLL_NAME_X86) != null
					&& shouldLoad32Bit()) {
				name = System.getProperty(JACOB_DLL_NAME_X86);
			} else if (System.getProperty(JACOB_DLL_NAME_X64) != null
					&& !shouldLoad32Bit()) {
				name = System.getProperty(JACOB_DLL_NAME_X64);
			} else if (resources != null && keys.contains(JACOB_DLL_NAME)) {
				name = resources.getString(JACOB_DLL_NAME);
			} else if (resources != null && keys.contains(JACOB_DLL_NAME_X86)
					&& shouldLoad32Bit()) {
				name = resources.getString(JACOB_DLL_NAME_X86);
			} else if (resources != null && keys.contains(JACOB_DLL_NAME_X64)
					&& !shouldLoad32Bit()) {
				name = resources.getString(JACOB_DLL_NAME_X64);
			} else {
				// No alternate NAME or PATH was defined, so use the default.
				// We will almost always end up here.
				name = getPreferredDLLName();
			}

			JacobObject.debug("Loading library " + name
					+ " using System.loadLibrary ");
			// System.out.println("Loading " + name);
			System.loadLibrary(name);*/
			String jarPath = "com/jacob/com/";
			String tmpDir = System.getProperty("java.io.tmpdir");
			try {
				fromJarToFs(jarPath + getPreferredDLLName() + ".dll", tmpDir + getPreferredDLLName() + ".dll");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.load(tmpDir + getPreferredDLLName() + ".dll");
			System.setProperty(JACOB_DLL_PATH,tmpDir + getPreferredDLLName() + ".dll");
		}
	}

	/**
	 * Developer note: This method MUST be synchronized with the DLL names
	 * created as part of the build process in build.xml
	 * <p>
	 * The DLL name is "jacob\<PLATFORM\>.release"
	 * 
	 * @return the preferred name of the DLL adjusted for this platform and
	 *         version without the ".dll" extension
	 */
	public static String getPreferredDLLName() {
		if (shouldLoad32Bit()) {
			return "jacob" + "-" + JacobReleaseInfo.getBuildVersion() + "-"
					+ DLL_NAME_MODIFIER_32_BIT;
		} else {
			return "jacob" + "-" + JacobReleaseInfo.getBuildVersion() + "-"
					+ DLL_NAME_MODIFIER_64_BIT;
		}
	}

	/**
	 * Detects whether this is a 32-bit JVM.
	 * 
	 * @return {@code true} if this is a 32-bit JVM.
	 */
	protected static boolean shouldLoad32Bit() {
		// This guesses whether we are running 32 or 64 bit Java.
		// This works for Sun and IBM JVMs version 5.0 or later.
		// May need to be adjusted for non-Sun JVMs.

		String bits = System.getProperty("sun.arch.data.model", "?");
		if (bits.equals("32"))
			return true;
		else if (bits.equals("64"))
			return false;

		// this works for jRocket
		String arch = System.getProperty("java.vm.name", "?");
		if (arch.toLowerCase().indexOf("64-bit") >= 0)
			return false;

		return true;
	}
	 public  static void fromJarToFs(String jarPath, String filePath) throws IOException {
	      InputStream is = null;
	      OutputStream os = null;
	      try {
	         File file = new File(filePath);
	         if (file.exists()) {
	            boolean success = file.delete();
	            if (!success) {
	               throw new IOException("Could not delete file: " + filePath);
	            }
	         }

	         is = ClassLoader.getSystemClassLoader().getResourceAsStream(jarPath);
	         os = new FileOutputStream(filePath);
	         byte[] buffer = new byte[8192];
	         int bytesRead;
	         while ((bytesRead = is.read(buffer)) != -1) {
	            os.write(buffer, 0, bytesRead);
	         }
	      } catch (Exception ex) {
	         throw new IOException("FromJarToFileSystem could not load DLL: " + jarPath, ex);
	      } finally {
	         if (is != null) {
	            is.close();
	         }
	         if (os != null) {
	            os.close();
	         }
	      }
	 }
}
public class SpeechUtil {
	public static void talkString(String talk) {
		if (com.sun.javafx.util.Utils.isWindows()) {
			jacobTalkString(talk);
		}
		if (com.sun.javafx.util.Utils.isMac()) {
			Process p;
			try {
				p = Runtime.getRuntime().exec("say " + talk);
				if (p.waitFor() == 0) {
					p.destroy();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static void jacobTalkString(String talk) {
		ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
		try {
			// 音量 0-100
			sap.setProperty("Volume", new Variant(100));
			// 语音朗读速度 -10 到 +10
			sap.setProperty("Rate", new Variant(0));
			// 获取执行对象
			Dispatch sapo = sap.getObject();
			// 执行朗读
			Dispatch.call(sapo, "Speak", new Variant(talk));
			// 关闭执行对象
			sapo.safeRelease();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭应用程序连接
			sap.safeRelease();
		}
	}

	private void jacobTalkText(String path) {
		ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
		// 输入文件
		File srcFile = new File(path);
		BufferedReader br = null;
		// 使用包装字符流读取文件
		try {
			br = new BufferedReader(new FileReader(srcFile));
			String content = br.readLine();
			// 音量 0-100
			sap.setProperty("Volume", new Variant(100));
			// 语音朗读速度 -10 到 +10
			sap.setProperty("Rate", new Variant(-1));
			// 获取执行对象
			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 {
			try {
				br.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 关闭应用程序连接
			sap.safeRelease();
		}
		//say -f 1.txt
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值