[java]代码库
//实现方式: 按键精灵+讯飞语音识别java SDK
//实现原理: 讯飞语音识别输入法将识别的文字复制到了你的windows剪贴板中,然后按键精灵监听剪贴板,并识别其中的文字,根据文字执行相应的代码
//作用: 理论上脚本能够执行的任务,都可以通过语音来控制,你可以用来进行语音编程,语音操作软件,语音开关机等等
//配置方式: 1.安装讯飞语音输入法,并打开 2.在按键精灵中配置好你要执行的命令 3.说出你要执行的任务
//代码如下:
按键精灵的操作讯飞输入法:
Function 获取x绝对坐标(程序标题)
//获取句柄
Hwnd = Plugin.Window.Find(0, 程序标题)
//获取程序左上角坐标
sRect= Plugin.Window.GetClientRect(Hwnd)
Dim MyArray
MyArray = Split(sRect, "|")
获取x绝对坐标=CLng(MyArray(0))//Clng 字符串类型转换成长整形
End Function
Function 获取y绝对坐标(程序标题)
//获取句柄
Hwnd = Plugin.Window.Find(0, 程序标题)
//获取程序左上角坐标
sRect= Plugin.Window.GetClientRect(Hwnd)
Dim MyArray
MyArray = Split(sRect, "|")
获取y绝对坐标=CLng(MyArray(1))//Clng 字符串类型转换成长整形
End Function
Sub 英语输入
Ux = Lib.我的测试.获取x绝对坐标("讯飞语音悬浮窗")
Uy = Lib.我的测试.获取y绝对坐标("讯飞语音悬浮窗")
//获取程序的绝对坐标
x = Ux + 57
y = Uy + 57
MoveTo x, y
RightClick 1
MoveTo x-127, y-242
Delay 50
MoveTo x-137, y-242
Delay 50
MoveTo x-274, y-242
MoveTo x-274, y-208
LeftClick 1
End Sub
Sub 中文输入
Ux = Lib.讯飞.获取x绝对坐标("讯飞语音悬浮窗")
Uy = Lib.讯飞.获取y绝对坐标("讯飞语音悬浮窗")
//获取程序的绝对坐标
x = Ux + 57
y = Uy + 57
MoveTo x, y
RightClick 1
MoveTo x-127, y-242
Delay 50
MoveTo x-137, y-242
Delay 50
MoveTo x-274, y-242
MoveTo x-274, y-264
LeftClick 1
End Sub
Sub 语音识别入口
While 1
Clipboard = Plugin.Sys.GetCLB()
If Clipboard <> "" Then
Call Lib.语音识别.识别(Clipboard)
Call Plugin.Sys.SetCLB("")
Delay 200
End If
Wend
End Sub
Sub 关闭语音
Ux = Lib.讯飞.获取x绝对坐标("讯飞语音悬浮窗")
Uy = Lib.讯飞.获取y绝对坐标("讯飞语音悬浮窗")
//获取程序的绝对坐标
x = Ux + 57
y = Uy + 57
MoveTo x, y
RightClick 1
MoveTo x - 20, y - 10
Delay 80
MoveTo x - 30, y - 10
LeftClick 1
End Sub
按键精灵配置任务(你可以自行设置):
Sub 识别(文本)
Select Case 文本
Case "打开编译器"
RunApp "F:\eclipse\eclipse.exe"
Case "打开表格"
RunApp "C:\Users\acer\Desktop\贺思源的工作计划.xls"
Case "打开幻灯片"
RunApp "C:\Users\acer\AppData\Local\kingsoft\WPS Office\ksolaunch.exe"
Case "打开工作计划"
RunApp "C:\Users\acer\Desktop\贺思源的工作计划.xls"
Case "英语输入"
Call Lib.讯飞.英语输入()
Case "Chinese"
Call Lib.讯飞.中文输入()
Case "搜索文本"
KeyDown "Ctrl", 1
KeyPress "F", 1
KeyUp "Ctrl", 1
End Select
End Sub
讯飞java程序(并不是完整的项目,一些配置文件在软件包中,请下载完整项目):
package com.iflytek.view;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.iflytek.cloud.speech.RecognizerListener;
import com.iflytek.cloud.speech.RecognizerResult;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechRecognizer;
import com.iflytek.cloud.speech.SpeechUtility;
public class MyFrame extends JFrame
{
private static final long serialVersionUID = 1L;
JPanel panelUp, panelDown;
JButton button_start, button_stop;
private SpeechRecognizer mIat;
public static void main(String[] args)
{
System.load("C:/Windows/System32/msc64.dll");
new MyFrame();
}
public MyFrame()
{
initIfly();
Container con = this.getContentPane();
con.setLayout(new BorderLayout());
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(3);
this.setLayout(new BorderLayout());
setFrame();
this.add(panelDown, BorderLayout.SOUTH);
this.add(panelUp, BorderLayout.NORTH);
setVisible(true);
}
public void initIfly()
{
mIat = SpeechRecognizer.createRecognizer();
SpeechUtility.createUtility("appid=58fed84b");
}
public void setFrame()
{
panelUp = new JPanel();
panelDown = new JPanel();
button_start = new JButton("开始");
button_start.addActionListener(e ->
{
setting();
if (!mIat.isListening()) mIat.startListening(recognizerListener);
else mIat.stopListening();
});
button_stop = new JButton("停止");
button_stop.addActionListener(e ->
{
mIat.stopListening();
iatSpeechInitUI();
});
panelDown.add(button_start);
panelDown.add(button_stop);
}
void setting()// 属性设置
{
final String engType = "cloud";
mIat.setParameter(SpeechConstant.ENGINE_TYPE, "cloud");
mIat.setParameter(SpeechConstant.SAMPLE_RATE, "16000");//
mIat.setParameter(SpeechConstant.NET_TIMEOUT, "20000");
mIat.setParameter(SpeechConstant.KEY_SPEECH_TIMEOUT, "60000");
mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");// 语言en_us(英语)
// zh_cn(中文)
mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
mIat.setParameter(SpeechConstant.DOMAIN, "iat");
mIat.setParameter(SpeechConstant.VAD_BOS, "5000");
mIat.setParameter(SpeechConstant.VAD_EOS, "1800");
mIat.setParameter(SpeechConstant.ASR_NBEST, "1");
mIat.setParameter(SpeechConstant.ASR_WBEST, "1");
mIat.setParameter(SpeechConstant.ASR_PTT, "0");// 标点符号 0(关) 1(开)
mIat.setParameter(SpeechConstant.RESULT_TYPE, "plain"); // 返回数据格式
// plain或者jason
mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, null);
}
private RecognizerListener recognizerListener = new RecognizerListener()
{
@Override
public void onBeginOfSpeech()
{
button_start.setText("听写中...");
button_start.setEnabled(false);
}
@Override
public void onEndOfSpeech()
{}
/**
* 获取听写结果. 获取RecognizerResult类型的识别结果,并对结果进行累加,显示到Area里
*/
@Override
public void onResult(RecognizerResult results, boolean islast)
{
String text = results.getResultString();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();// 获取系统剪切板
StringSelection selection = new StringSelection(text);// 构建String数据类型
clipboard.setContents(selection, selection);// 添加文本到系统剪切板
if (islast)
{
iatSpeechInitUI();
}
}
@Override
public void onVolumeChanged(int volume)
{}
@Override
public void onError(SpeechError error)
{
if (null != error)
{
iatSpeechInitUI();
}
}
@Override
public void onEvent(int eventType, int arg1, int agr2, String msg)
{}
};
public void iatSpeechInitUI()
{
button_start.setEnabled(true);
button_start.setText("开始听写");
}
}
[源代码打包下载]
中级程序员
by: 猴子的救兵 发表于:2017-09-13 16:07:34 顶(0) | 踩(0) 回复
这个有谁实现了语音控制吗
回复评论