声控鼠标器


利用sphinx4写了个java的声控鼠标器,可以用声音控制鼠标的活动,代码贴下:
  ControlGui.java-----
   

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ControlGui implements ActionListener,Runnable{

static JFrame jf;
static JButton b;
JPanel p1,p2;
JPanel pl1,pl2,pl3;
static JLabel label,l1,l2,l3;

public ControlGui(){
jf=new JFrame("声控鼠标器");
b=new JButton("启动声控");
p1=new JPanel();
p2=new JPanel();
pl1=new JPanel();
pl2=new JPanel();
pl3=new JPanel();
label=new JLabel("");
l1=new JLabel("左-left(a),右-right(d),上-up(w)");
l2=new JLabel(",下-down(s)小括号内的字母a(d,w,");
l3=new JLabel("s) 是鼠标向左(右,上,下)1个单位");

jf.setLayout(new GridLayout(5,1));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1.add(b);
p2.add(label);
         pl1.add(l1);
         pl2.add(l2);
         pl3.add(l3);
   
jf.add(pl1);
jf.add(pl2);
jf.add(pl3);
jf.add(p1);
jf.add(p2);
jf.setSize(200,200);
jf.setVisible(true);
b.addActionListener(this);

}

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getActionCommand().equals("启动声控")){

new Thread(this).start();
b.setText("停止声控");
jf.repaint();

}
else{

SoundService.flag=true;
MouseSevice.move=false;
b.setText("启动声控");
jf.repaint();
}

}

public static void main(String[] args){

new ControlGui();


}

public void run(){

SoundService s=new SoundService();
s.listenerRun();


}
}
---------------SoundService.java-------------------------------


import java.io.File;
import java.io.IOException;
import java.net.URL;

import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
import edu.cmu.sphinx.util.props.PropertyException;

public class SoundService {

static boolean flag;

public void listenerRun(){

try {
        URL url;
      
           url = SoundService.class.getResource("soundservice.config.xml");
      

      //  System.out.println("Loading...");
            ControlGui.label.setText("初始化............");
            ControlGui.jf.repaint();
        ConfigurationManager cm = new ConfigurationManager(url);

    Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
    Microphone microphone = (Microphone) cm.lookup("microphone");


        /* allocate the resource necessary for the recognizer */
        recognizer.allocate();

        /* the microphone will keep recording until the program exits */
    if (microphone.startRecording()) {

//System.out.println
  //  ("Say commnd:left right wait click....................");
    ControlGui.label.setText("say commnd to control....");
        ControlGui.jf.repaint();
       
while (true&&!flag) {

               /*
                 * This method will return when the end of speech
                 * is reached. Note that the endpointer will determine
                 * the end of speech.
                 */
    Result result = recognizer.recognize();
   
    if (result != null) {
String resultText = result.getBestFinalResultNoFiller();
MouseSevice.move=false;
new Thread(new MouseThread(resultText)).start();

ControlGui.label.setText("You said: " + resultText);
        ControlGui.jf.repaint();
// System.out.println("You said: " + resultText + "\n");
    } else {
    ControlGui.label.setText("I can't hear what you said.");
        ControlGui.jf.repaint();

    }
}
    } else {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
    }
    } catch (Exception e) {
    ControlGui.label.setText("初始化失败..."+e.getMessage());
    ControlGui.b.setText("启动声控");
        ControlGui.jf.repaint();
    e.printStackTrace();
       
    }

}
}
------------------MouseThread.java----------------------

public class MouseThread implements Runnable{

String resultText;
MouseSevice serv=new MouseSevice();
public MouseThread(String cmd){

this.resultText=cmd;

}


public void run(){
       if(resultText.equals("left")){

serv.leftMove();
}else if(resultText.equals("right")){
serv.rightMove();

}else if(resultText.equals("wait")){
serv.waitMove();
}
else if(resultText.equals("click")){

serv.clickMouse();
}
else if(resultText.equals("up")){
serv.upMove();

}else if(resultText.equals("down")){
serv.downMove();
}
else if(resultText.equals("a")){
serv.L();
}
else if(resultText.equals("d")){
serv.R();
}
else if(resultText.equals("w")){
serv.U();
}
else if(resultText.equals("s")){
serv.D();
}

}


}
----------------------MouseSevice.java-----------------------


import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class MouseSevice {

static boolean move=true;
Robot ro;
MousePointInfo p;
public MouseSevice(){

try {
ro=new Robot();
p=new MousePointInfo();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void leftMove(){
move=true;
try {
while(move){
ro.mouseMove(p.getMousePosX()-10,p.getMousePosY());
new Thread().sleep(130);

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

   public void rightMove(){
move=true;
   try {
while(move){
ro.mouseMove(p.getMousePosX()+10,p.getMousePosY());
new Thread().sleep(130);

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
  
   public void waitMove(){
  
   move=false;
  
   }
  
   public void clickMouse(){
   try {

ro.mousePress(InputEvent.BUTTON1_MASK);
ro.mouseRelease(InputEvent.BUTTON1_MASK);
ro.mousePress(InputEvent.BUTTON1_MASK);
ro.mouseRelease(InputEvent.BUTTON1_MASK);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  
   }
  
   public void upMove(){
move=true;
   try {
while(move){
ro.mouseMove(p.getMousePosX(),p.getMousePosY()-10);
new Thread().sleep(130);

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  
   }
  
   public void downMove(){
  
move=true;
   try {
while(move){
ro.mouseMove(p.getMousePosX(),p.getMousePosY()+10);
new Thread().sleep(130);

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}






  
   }

  
   public void L(){
   ro.mouseMove(p.getMousePosX()-10,p.getMousePosY());

}
  
   public void R(){

   ro.mouseMove(p.getMousePosX()+10,p.getMousePosY());
}
   public void U(){

   ro.mouseMove(p.getMousePosX(),p.getMousePosY()-10);
}
   public void D(){
   ro.mouseMove(p.getMousePosX(),p.getMousePosY()+10);

}

}
-----------------------soundservice.config.xml---------------------
<?xml version="1.0" encoding="UTF-8"?>

<!--
   Sphinx-4 Configuration file
-->

<!-- ******************************************************** -->
<!--  an4 configuration file                             -->
<!-- ******************************************************** -->

<config>       
   
    <!-- ******************************************************** -->
    <!-- frequently tuned properties                              -->
    <!-- ******************************************************** -->

    <property name="logLevel" value="WARNING"/>
   
    <property name="absoluteBeamWidth"  value="-1"/>
    <property name="relativeBeamWidth"  value="1E-80"/>
    <property name="wordInsertionProbability" value="1E-36"/>
    <property name="languageWeight"     value="8"/>
   
    <property name="frontend" value="epFrontEnd"/>
    <property name="recognizer" value="recognizer"/>
    <property name="showCreations" value="false"/>
   
   
    <!-- ******************************************************** -->
    <!-- word recognizer configuration                            -->
    <!-- ******************************************************** -->
   
    <component name="recognizer" type="edu.cmu.sphinx.recognizer.Recognizer">
        <property name="decoder" value="decoder"/>
        <propertylist name="monitors">
            <item>accuracyTracker </item>
            <item>speedTracker </item>
            <item>memoryTracker </item>
        </propertylist>
    </component>
   
    <!-- ******************************************************** -->
    <!-- The Decoder   configuration                              -->
    <!-- ******************************************************** -->
   
    <component name="decoder" type="edu.cmu.sphinx.decoder.Decoder">
        <property name="searchManager" value="searchManager"/>
    </component>
   
    <component name="searchManager"
        type="edu.cmu.sphinx.decoder.search.SimpleBreadthFirstSearchManager">
        <property name="logMath" value="logMath"/>
        <property name="linguist" value="flatLinguist"/>
        <property name="pruner" value="trivialPruner"/>
        <property name="scorer" value="threadedScorer"/>
        <property name="activeListFactory" value="activeList"/>
    </component>
   
   
    <component name="activeList"
             type="edu.cmu.sphinx.decoder.search.PartitionActiveListFactory">
        <property name="logMath" value="logMath"/>
        <property name="absoluteBeamWidth" value="${absoluteBeamWidth}"/>
        <property name="relativeBeamWidth" value="${relativeBeamWidth}"/>
    </component>
   
    <component name="trivialPruner"
                type="edu.cmu.sphinx.decoder.pruner.SimplePruner"/>
   
    <component name="threadedScorer"
                type="edu.cmu.sphinx.decoder.scorer.ThreadedAcousticScorer">
        <property name="frontend" value="${frontend}"/>
        <property name="isCpuRelative" value="true"/>
        <property name="numThreads" value="0"/>
        <property name="minScoreablesPerThread" value="10"/>
        <property name="scoreablesKeepFeature" value="true"/>
    </component>
   
    <!-- ******************************************************** -->
    <!-- The linguist  configuration                              -->
    <!-- ******************************************************** -->
   
    <component name="flatLinguist"
                type="edu.cmu.sphinx.linguist.flat.FlatLinguist">
        <property name="logMath" value="logMath"/>
        <property name="grammar" value="jsgfGrammar"/>
        <property name="acousticModel" value="wsj"/>
        <property name="wordInsertionProbability"
                value="${wordInsertionProbability}"/>
        <property name="languageWeight" value="${languageWeight}"/>
        <property name="unitManager" value="unitManager"/>
    </component>
       
   
    <!-- ******************************************************** -->
    <!-- The Grammar  configuration                               -->
    <!-- ******************************************************** -->
           
    <component name="jsgfGrammar" type="edu.cmu.sphinx.jsapi.JSGFGrammar">
        <property name="dictionary" value="dictionary"/>
        <property name="grammarLocation"
             value="resource:/SoundService!/"/>
        <property name="grammarName" value="mouse"/>
<property name="logMath" value="logMath"/>
    </component>
   
       
    <!-- ******************************************************** -->
    <!-- The Dictionary configuration                            -->
    <!-- ******************************************************** -->
   
    <component name="dictionary"
        type="edu.cmu.sphinx.linguist.dictionary.FastDictionary">
        <property name="dictionaryPath"
value="resource:/edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.Model!/edu/cmu/sphinx/model/acoustic/WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz/dict/mouse.dict"/>
        <property name="fillerPath"
value="resource:/edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.Model!/edu/cmu/sphinx/model/acoustic/WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz/dict/fillerdict"/>
      
        <property name="addSilEndingPronunciation" value="false"/>
        <property name="allowMissingWords" value="false"/>
        <property name="unitManager" value="unitManager"/>
    </component>

    <!-- ******************************************************** -->
    <!-- The acoustic model configuration                         -->
    <!-- ******************************************************** -->
    <component name="wsj"
      type="edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.Model">
        <property name="loader" value="wsjLoader"/>
        <property name="unitManager" value="unitManager"/>
    </component>

    <component name="wsjLoader"
               type="edu.cmu.sphinx.model.acoustic.WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.ModelLoader">
        <property name="logMath" value="logMath"/>
        <property name="unitManager" value="unitManager"/>
    </component>
   
   
    <!-- ******************************************************** -->
    <!-- The unit manager configuration                           -->
    <!-- ******************************************************** -->

    <component name="unitManager"
        type="edu.cmu.sphinx.linguist.acoustic.UnitManager"/>

    <!-- ******************************************************** -->
    <!-- The frontend configuration                               -->
    <!-- ******************************************************** -->
   
    <component name="frontEnd" type="edu.cmu.sphinx.frontend.FrontEnd">
        <propertylist name="pipeline">
            <item>microphone </item>
            <item>premphasizer </item>
            <item>windower </item>
            <item>fft </item>
            <item>melFilterBank </item>
            <item>dct </item>
            <item>liveCMN </item>
            <item>featureExtraction </item>
        </propertylist>
    </component>
   
    <!-- ******************************************************** -->
    <!-- The live frontend configuration                          -->
    <!-- ******************************************************** -->
    <component name="epFrontEnd" type="edu.cmu.sphinx.frontend.FrontEnd">
        <propertylist name="pipeline">
            <item>microphone </item>
            <item>speechClassifier </item>
            <item>speechMarker </item>
            <item>nonSpeechDataFilter </item>
            <item>premphasizer </item>
            <item>windower </item>
            <item>fft </item>
            <item>melFilterBank </item>
            <item>dct </item>
            <item>liveCMN </item>
            <item>featureExtraction </item>
        </propertylist>
    </component>

    <!-- ******************************************************** -->
    <!-- The frontend pipelines                                   -->
    <!-- ******************************************************** -->
   
    <component name="speechClassifier"
               type="edu.cmu.sphinx.frontend.endpoint.SpeechClassifier">
        <property name="threshold" value="13"/>
    </component>
   
    <component name="nonSpeechDataFilter"
               type="edu.cmu.sphinx.frontend.endpoint.NonSpeechDataFilter"/>
   
    <component name="speechMarker"
               type="edu.cmu.sphinx.frontend.endpoint.SpeechMarker" >
        <property name="speechTrailer" value="50"/>
    </component>
   
   
    <component name="premphasizer"
               type="edu.cmu.sphinx.frontend.filter.Preemphasizer"/>
   
    <component name="windower"
               type="edu.cmu.sphinx.frontend.window.RaisedCosineWindower">
    </component>
   
    <component name="fft"
            type="edu.cmu.sphinx.frontend.transform.DiscreteFourierTransform">
    </component>
   
    <component name="melFilterBank"
        type="edu.cmu.sphinx.frontend.frequencywarp.MelFrequencyFilterBank">
    </component>
   
    <component name="dct"
            type="edu.cmu.sphinx.frontend.transform.DiscreteCosineTransform"/>
   
    <component name="liveCMN"
               type="edu.cmu.sphinx.frontend.feature.LiveCMN"/>
       
    <component name="featureExtraction"
               type="edu.cmu.sphinx.frontend.feature.DeltasFeatureExtractor"/>
      
    <component name="microphone"
               type="edu.cmu.sphinx.frontend.util.Microphone">
        <property name="closeBetweenUtterances" value="false"/>
    </component>

   
    <!-- ******************************************************* -->
    <!--  monitors                                               -->
    <!-- ******************************************************* -->
   
    <component name="accuracyTracker"
                type="edu.cmu.sphinx.instrumentation.AccuracyTracker">
        <property name="recognizer" value="${recognizer}"/>
        <property name="showAlignedResults" value="false"/>
        <property name="showRawResults" value="false"/>
    </component>
   
    <component name="memoryTracker"
                type="edu.cmu.sphinx.instrumentation.MemoryTracker">
        <property name="recognizer" value="${recognizer}"/>
<property name="showSummary" value="false"/>
<property name="showDetails" value="false"/>
    </component>
   
    <component name="speedTracker"
                type="edu.cmu.sphinx.instrumentation.SpeedTracker">
        <property name="recognizer" value="${recognizer}"/>
        <property name="frontend" value="${frontend}"/>
<property name="showSummary" value="true"/>
<property name="showDetails" value="false"/>
    </component>
   
   
    <!-- ******************************************************* -->
    <!--  Miscellaneous components                               -->
    <!-- ******************************************************* -->
   
    <component name="logMath" type="edu.cmu.sphinx.util.LogMath">
        <property name="logBase" value="1.0001"/>
        <property name="useAddTable" value="true"/>
    </component>
   
</config>

--------------------mouse.gram---------------------------------
grammar mouse;

public <mouse> =(LEFT|RIGHT|WAIT|CLICK|DOWN|UP|A|S|W|D);
------------------------------------------------------------------
因为要在程序不被激活的状态下也能控制鼠标所以需要用到JNI
-----------MousePointInfo.java--------------------
public class MousePointInfo{
    static{
        System.loadLibrary("MouseInfo");
   
    }
    public native static int getMousePosX();
    public native static int getMousePosY();
 
    public static void main(String[] args)throws Exception{
        // TODO Auto-generated method stub
        MousePointInfo mouseInfo = new MousePointInfo();
        System.out.println("x:"+mouseInfo.getMousePosX()+"y:"+mouseInfo.getMousePosY());
    }
}

javac MousePointInfo.java
javah MousePointInfo
得到MousePointInfo.h
利用VC工具编译一个MouseInfo.dll
-------------------------------------------------------------
程序用到sphinx4的一些文件所以超过了10M传不上来........................
感兴趣的可以告诉我发给你....

你可能感兴趣的:(java,thread,swing,qq,vc++)