Created
November 2, 2012 17:05
-
-
Save chrismedrdz/4002766 to your computer and use it in GitHub Desktop.
Reconocimiento de Voz JAVA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.speech.*; | |
import javax.speech.recognition.*; | |
import java.io.FileReader; | |
import java.util.Locale; | |
import java.io.*; | |
public class Programas extends ResultAdapter { | |
static Recognizer oreja; | |
String Programa; | |
public void resultAccepted(ResultEvent e){ | |
try { | |
Result res = (Result)(e.getSource()); | |
ResultToken tokens[] = res.getBestTokens(); | |
String Frase[]= new String[1]; | |
Frase[0]=""; | |
for (int i=0; i < tokens.length; i++){ | |
Programa = tokens[i].getSpokenText(); | |
Frase[0]+=Programa+" "; | |
System.out.print(Programa + " "); | |
} | |
System.out.println(); | |
if(Programa.equals("Fin")){ | |
oreja.deallocate(); | |
Frase[0]="Hasta la proxima!"; | |
System.out.println(Frase[0]); | |
System.exit(0); | |
} | |
else if(Programa.equals("Facebook")) { | |
try { | |
System.out.println("Abriendo Facebook..."); | |
// Se lanza el ejecutable. | |
Process p=Runtime.getRuntime().exec ("cmd.exe /c start Chrome www.facebook.com"); | |
// Se obtiene el stream de salida del programa | |
InputStream is = p.getInputStream(); | |
/* Se prepara un bufferedReader para poder leer la salida más comodamente. */ | |
BufferedReader br = new BufferedReader (new InputStreamReader (is)); | |
// Se lee la primera linea | |
String aux = br.readLine(); | |
while (aux!=null) { | |
System.out.println (aux); | |
aux = br.readLine(); | |
} | |
} catch (Exception es) { | |
es.printStackTrace(); | |
} | |
} | |
else if(Programa.equals("Reproductor")){ | |
try { | |
System.out.println("Abriendo Windows Media Player..."); | |
// Se lanza el ejecutable. | |
Process p=Runtime.getRuntime().exec ("cmd.exe /c start wmplayer"); | |
// Se obtiene el stream de salida del programa | |
InputStream is = p.getInputStream(); | |
/* Se prepara un bufferedReader para poder leer la salida más comodamente. */ | |
BufferedReader br = new BufferedReader (new InputStreamReader (is)); | |
// Se lee la primera linea | |
String aux = br.readLine(); | |
while (aux!=null) { | |
System.out.println (aux); | |
aux = br.readLine(); | |
} | |
} catch (Exception es) { | |
es.printStackTrace(); | |
} | |
} | |
else if(Programa.equals("Chrome")){ | |
try { | |
System.out.println("Abriendo Chrome..."); | |
// Se lanza el ejecutable. | |
Process p=Runtime.getRuntime().exec ("cmd.exe /c start Chrome"); | |
// Se obtiene el stream de salida del programa | |
InputStream is = p.getInputStream(); | |
/* Se prepara un bufferedReader para poder leer la salida más comodamente. */ | |
BufferedReader br = new BufferedReader (new InputStreamReader (is)); | |
// Se lee la primera linea | |
String aux = br.readLine(); | |
while (aux!=null) { | |
System.out.println (aux); | |
aux = br.readLine(); | |
} | |
} catch (Exception es) { | |
es.printStackTrace(); | |
} | |
} | |
else if(Programa.equals("Paint")){ | |
try { | |
System.out.println("Abriendo Paint Brush..."); | |
// Se lanza el ejecutable. | |
Process p=Runtime.getRuntime().exec ("cmd.exe /c start pbrush"); | |
// Se obtiene el stream de salida del programa | |
InputStream is = p.getInputStream(); | |
/* Se prepara un bufferedReader para poder leer la salida más comodamente. */ | |
BufferedReader br = new BufferedReader (new InputStreamReader (is)); | |
// Se lee la primera linea | |
String aux = br.readLine(); | |
while (aux!=null) { | |
System.out.println (aux); | |
aux = br.readLine(); | |
} | |
} catch (Exception es) { | |
es.printStackTrace(); | |
} | |
} | |
else { | |
getPrograma(); | |
oreja.suspend(); | |
oreja.resume(); | |
} | |
}catch(Exception ex) { | |
} | |
} | |
public String getPrograma(){ | |
return Programa; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#JSGF V1.0; | |
grammar sentence; | |
public <sentence> = | |
[<dato0>] | |
[<dato1>] | |
[<dato2>] | |
[<dato3>] | |
[<dato4>]; | |
<dato0>=Chrome; | |
<dato1>=Paint; | |
<dato2>=Facebook; | |
<dato3>=Reproductor; | |
<dato4>=Fin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import java.io.*; | |
import javax.speech.*; | |
import javax.speech.recognition.*; | |
import java.io.FileReader; | |
import java.util.Locale; | |
public class Reconocedor extends JFrame implements ActionListener{ | |
public JButton btn; | |
public Image fondo; | |
public ImageIcon MicrophoneIcon; | |
static Recognizer oreja; | |
String palabra; | |
public static void main (String[] args){ | |
Reconocedor Reconocedor = new Reconocedor(); | |
Reconocedor.setVisible(true); | |
} | |
public Reconocedor() { | |
setLayout(null); | |
setSize(135,155); | |
setResizable(false); | |
setLocation(900,100); | |
setBackground(Color.black); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
Inicio(); | |
Image microImage = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/micro.png")); | |
MicrophoneIcon = new ImageIcon( microImage ); | |
Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); | |
btn = new JButton(MicrophoneIcon); | |
btn.setBounds(0,0,130,130); | |
btn.setFocusable(false); //El botón no atrae el foco | |
btn.setBorder(null); //Botón sin borde | |
btn.setBorderPainted(false); //Botón sin borde pintado | |
btn.setOpaque(false); //Fondo del bóton transparente | |
btn.setContentAreaFilled(false); //Botón sin relleno | |
btn.setCursor(cursor); //Establece el cursor para el botón | |
btn.setFocusPainted(false); //El foco no esta pintado | |
btn.addActionListener(this); //Añade un ActionListener al botón | |
add(btn); | |
} | |
public void Inicio() { | |
setVisible(false); | |
JOptionPane.showMessageDialog(null, " Prepare su Microfono!! ","Bienvenido",JOptionPane.INFORMATION_MESSAGE); | |
return; | |
} | |
public void iniciarRec() { | |
JOptionPane.showMessageDialog(null, "Pronuncie Aplicacion a Ejecutar"); | |
try{ | |
//Se configura al reconocedor para que entienda el idioma inglés | |
oreja = Central.createRecognizer(new EngineModeDesc(Locale.ROOT)); | |
oreja.allocate(); | |
FileReader grammar1 =new FileReader("Programas.txt"); //ruta donde esta el archivo con las Frases | |
RuleGrammar rg = oreja.loadJSGF(grammar1);//Establece la forma en que debe de estar estructurado el archive grammar | |
rg.setEnabled(true); //accesa al archivo | |
oreja.addResultListener(new Programas()); //Se hace referencia a la clase de escucha del reconocedor | |
for(int i=0;i<=23;i++){ | |
System.out.println(""); | |
} | |
System.out.println("Pronuncia un programa"); | |
oreja.commitChanges(); | |
oreja.requestFocus(); | |
oreja.resume(); | |
} catch (Exception e){ | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
return; | |
} | |
public void salir() { | |
JOptionPane.showMessageDialog(null, "GRACIAS POR PROBAR NUESTRO SISTEMA!!"); | |
System.exit(0); | |
} | |
public void actionPerformed(ActionEvent event) { | |
if ( (event.getSource() == btn)) { | |
//activ = true; | |
iniciarRec(); | |
return; | |
} | |
/*else { | |
salir(); | |
} */ | |
}//Termina ActionPerformed | |
} |
Aqui les dejo el Link para descargar las librerias.
1
Hola a todos saludos, buenas tardes saludos.
soy nuevo y estoy buscando código para una
nueva aplicación.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
El codigo funciona solo que hay que instalar las librerias correspondientes.