ASL语言项目

时间:2013-11-26 16:06:46

标签: java android xml eclipse

你好,我正在做手语项目。我的项目理念是将语音识别转换为文本,然后将文本转换为ASL语言。我已经完成了两者的编码,但我不知道如何链接两者。任何人都可以帮助我。谢谢

这是我的语音识别编码

java代码

package com.authorwjf.talk2me;

import java.util.ArrayList;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.speech.RecognizerIntent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    protected static final int REQUEST_OK = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
         Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
         i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
         try {
             startActivityForResult(i, REQUEST_OK);
         } catch (Exception e) {
             Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
         }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_OK  && resultCode==RESULT_OK) {
            ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            ((TextView)findViewById(R.id.text1)).setText(thingsYouSaid.get(0));
        }
    }

}

xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="132dp"
        android:text="..." />

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:layout_marginTop="37dp"
        android:src="@android:drawable/ic_btn_speak_now" />

</RelativeLayout>

现在这是ASL语言的编码

java编码

package com.example3.aslalphabet;



import android.app.Activity;

import android.app.Service;

import android.os.Bundle;

import android.text.Editable;

import android.view.Menu;

import android.view.View;

import android.view.inputmethod.InputMethodManager;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;


import android.widget.TextView;

public class MainActivity extends Activity {

// View Objects

    Button translate;

    EditText enterText;

    TextView displayText;

    ImageView aslImages;

    MainActivity thisClass;

    // Variables for Translation and Display

    int phraseIndex = 0; // Keep track of the array indexes
    String letters;     // Message to be Translated     
    String display;     // Will hold the letters already displayed and show them

    // Array Libraries For Characters and Image References 
    char letterIndex [] = { 'a', 'b', 'c', 'd',
                            'e', 'f', 'g', 'h',
                            'i', 'j', 'k', 'l',
                            'm', 'n', 'o', 'p',
                            'q', 'r', 's', 't',
                            'u', 'v', 'w', 'x',
                            'y', 'z', ' '};

    int aslPics [] ={R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
                     R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h,
                     R.drawable.i, R.drawable.j, R.drawable.k, R.drawable.l,
                     R.drawable.m, R.drawable.n, R.drawable.o, R.drawable.p,
                     R.drawable.q, R.drawable.r, R.drawable.s, R.drawable.t,
                     R.drawable.u, R.drawable.v, R.drawable.w, R.drawable.x,
                     R.drawable.y, R.drawable.z, R.drawable.space};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        thisClass=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Attach objects to view objects

        translate =(Button) findViewById(R.id.buttonTranslate);
        translate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                setString(v);
                translateLetter();
            }
        });
        enterText = (EditText) findViewById(R.id.textInput);
        displayText=(TextView) findViewById(R.id.displayText);
        aslImages=(ImageView) findViewById(R.id.aslViewer);
        aslImages.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                translateLetter();
            }
        });

        //Select all the Text in the enterText Field
        enterText.setSelectAllOnFocus(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    //setString Function
    //Acts when the Translate Button is pressed
    public void setString(View v){
        //Set the text of displayText to'--- '"
        displayText.setText("----");

        // Close Keyboard on lost focus
        InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

        //Reset the Phrase Index
        phraseIndex=0;
        display="";

        //Get the Input Text
        Editable input = enterText.getText();

        //Convert to a string
        String phrase =input.toString();
        letters=phrase.toLowerCase();

    }

    //Displays Image when ImageView Touched.
    // Also displays text of letters already translated
    public void translateLetter(){


        //Checks if letters string is null-displays message
        //If the phrase has not been converted to a string
        if (letters==null){
             displayText.setText("Press the Translate Button ");

        }

        //Checks if letters string is null-will not display
        //ASL letters until translate Button is pressed
        if (letters!=null){
            // Fetch the current Character in the phrase
            char currentLetter = letters.charAt(phraseIndex);

            // add the letter to the display string
            display+=currentLetter;

            //Search for the corresponding ASL image by Index
            for (int i=0; i<letterIndex.length; i++)
            {
                if (letterIndex[i]== currentLetter){
                    //Display the image
                    aslImages.setImageResource(aslPics[i]);

                } //end if
            }// end for
            // Set the text to display the  letters  translated
            displayText.setText(display);

            //Advance to the Next Letter in the Phrase
            phraseIndex++;

            //Check to see if you reach the end of the phrase
            if (phraseIndex > letters.length()-1) {
                // Reset back to the first character
                phraseIndex =0;
                display="";
            } //end if 
            }// end Function translateLetter()
    }// end if
}//End Class Main

现在这是xml文件

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TableRow 
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/aslViewer"
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:layout_span="2"
            android:onClick="translateletter"
            android:contentDescription="@string/hand_images"
            android:src="@drawable/a" />

    </TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/displayText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:text="@string/T_text"/>
    </TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/buttonTranslate"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
       android:onClick="setString"
       android:text="@string/Translate"/>

    <EditText
        android:id="@+id/textInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/Enter_Text" >

    </EditText>

</TableRow>
 <ImageButton
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       android:layout_below="@+id/text1"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:layout_marginTop="37dp"
        android:src="@android:drawable/ic_btn_speak_now" />

</TableLayout>

0 个答案:

没有答案
相关问题