手势识别以及语音识别demo

1、使用手势
1)、设置手势组件
<android.gesture.GestureOverlayView
android:id="@+id/gestures" 
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:gestureStrokeType="multiple" />


2)、设置监听器
public class MainActivity 


extends Activity 


implements OnGesturePerformedListener


//配置手势库
private GestureLibrary gestureLibrary;


3)、装载手势文件 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
      //指定手势资源文件的位置
      //gestureLibrary = GestureLibraries.fromFile("/sdcard/gestures");
        
      //从raw资源中装载手势资源
gestureLibrary = 
GestureLibraries
.fromRawResource(this, R.raw.gestures);


if (gestureLibrary.load())
{
setTitle("手势文件装载成功(输出文本).");
GestureOverlayView gestureOverlayView = 
(GestureOverlayView) findViewById(R.id.gestures);

//设置OnGesturePerformedListener事件,该事件方法在绘制完手势,
//并进行识别后调用。
gestureOverlayView.addOnGesturePerformedListener(this);
}
else
{
setTitle("手势文件装载失败.");
}
    }


4)、处理手势事件
//存放所有手势
private GestureLibrary gestureLibrary;






@Override
public void onGesturePerformed
(GestureOverlayView overlay, Gesture gesture) {

// TODO Auto-generated method stub

//获得可能匹配的手势列表
                                  //recognize:识别
                                  //Prediction:预测
ArrayList<Prediction> predictions = 
gestureLibrary.recognize(gesture);


//有可能匹配的手势
if (predictions.size() > 0)
{


StringBuilder sb = new StringBuilder();
int n = 0;

//开始扫描所有可能匹配的手势
for (int i = 0; i < predictions.size(); i++)
{
Prediction prediction = predictions.get(i);

//根据相似度,只列出score字段值大于1的匹配手势。
if (prediction.score > 1.0)
{
sb.append("score:" + prediction.score + "  name:"
+ prediction.name + "\n");
n++;
}
}

sb.insert(0,n + "个相匹配的手势.\n");
Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
}
}


2、运用手势驱动其它应用程序
1)、布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.gesture.GestureOverlayView
android:id="@+id/gestures" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gestureStrokeType="multiple" />
</LinearLayout>


2)、装载手势
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);


setContentView(R.layout.main);
// gestureLibrary = GestureLibraries.fromFile("/sdcard/gestures");
gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (gestureLibrary.load())
{
setTitle("手势文件装载成功(识别动作).");
GestureOverlayView gestureOverlayView = 
(GestureOverlayView) findViewById(R.id.gestures);


gestureOverlayView.addOnGesturePerformedListener(this);
}
else
{
setTitle("手势文件装载失败.");
}
}


3)、使用手势启动其它程序
private GestureLibrary gestureLibrary;


@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)
{
ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture);


if (predictions.size() > 0)
{


int n = 0;
for (int i = 0; i < predictions.size(); i++)
{
Prediction prediction = predictions.get(i);
if (prediction.score > 1.0)
{
Intent intent = null;
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();

//拨打电话
//必须有权限:<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
if ("action_call".equals(prediction.name))
{
intent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:12345678"));

//显示通话记录
else if ("action_call_button".equals(prediction.name))
{
intent = new Intent(Intent.ACTION_CALL_BUTTON);


}
//自动输入电话号码
else if ("action_dial".equals(prediction.name))
{
intent = new Intent(Intent.ACTION_DIAL, Uri
.parse("tel:12345678"));


}
if (intent != null)
startActivity(intent);
n++;
break;
}
}


if (n == 0)
Toast.makeText
          (this, "没有符合要求的手势.", Toast.LENGTH_SHORT).show();
}


3、语音识别
1)、布局
<Button android:id="@+id/button"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="说话"
    />
    <TextView android:id="@+id/textview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="@string/text"
    />


2)、设定朗读文本
 <string name="text">Android 2.2 is a minor platform release deployable
to Android-powered handsets starting in January 2010. This release
includes new API changes and bug fixes. For information on changes,
see the Framework API section.\n


For developers, the Android 2.1 platform is available as a downloadable
component for the Android SDK. The downloadable platform includes a
fully compliant Android library and system image, as well as a set of
emulator skins, sample applications, and more. The downloadable
platform includes no external libraries.\n


To get started developing or testing against the Android 2.1 platform,
use the Android SDK and AVD Manager tool to download the platform into
your Android SDK. For more information, see Adding SDK Components.
</string>




3)、设置监听器
/**
 * 方便输入信息还不够,如果让手机根据文本读出输入的内容岂不是更人性化吗?
 * 在Andorid1.6中提出了TTS(Text To Speech)技术可以完成该工作
 * 
 *  TTS技术的核心是android.speech.tts.TextToSpeech类.要想使用TTS技术
 *  朗读文本,需要两步:
 *  1、初始化TTS,主要指定TTS朗读的文本的语言。
 *  2、使用speak方法指定要朗读的文本。
 *  
 *  目前TTS只支持以英语为首的几种欧美语言,中文、日文等亚洲语系暂时不支持。
 * @author Administrator
 *
 */


public class MainActivity extends Activity


 implements TextToSpeech.OnInitListener,
OnClickListener


private TextToSpeech tts;
private TextView textView;


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
Button button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textview);
button.setOnClickListener(this);


}


4)、处理事件
@Override
public void onClick(View view)
{


//第二步:
//参数1:指定朗读文本
//参数2:刷新队列中保存的字符
tts.speak(textView.getText().toString(), TextToSpeech.QUEUE_FLUSH,
null);


}


//第一步:初始化TTS,主要指定TTS朗读的文本的语言。
@Override
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
//指定美式英语
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED)
{


Toast.makeText(this, "Language is not available.",
Toast.LENGTH_SHORT).show();
}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值