本文主要是介绍通过GestureOverlayView手势库来识别手写的字,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MainActivity 文件
package qianfeng.gesture;import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;import java.io.File;
import java.util.ArrayList;public class Main4Activity extends AppCompatActivity {private static final String TAG = "Main4Activity";private GestureOverlayView mGestureOverlay;private ImageView mImage1,mImage2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main4);mGestureOverlay = (GestureOverlayView) findViewById(R.id.overlay);mImage1 = (ImageView) findViewById(R.id.images1);mImage2 = (ImageView) findViewById(R.id.images2);mGestureOverlay.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {@Overridepublic void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {//将手势转换成图片显示Bitmap bitmap = gesture.toBitmap(100,100,10, Color.BLUE);mImage1.setImageBitmap(bitmap);GestureLibrary libraries = GestureLibraries.fromFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/getsure");//加载手势库if(libraries.load()){Log.e(TAG,"load");}else{Log.e(TAG,"load error");}//返回的一堆 相近的手势ArrayList<Prediction> recognize = libraries.recognize(gesture);//判断相似度if(recognize.size()>0){for (Prediction prediction : recognize){Log.e(TAG,prediction.score+"");//且当 相似度大于10if(prediction.score>10.0f){//手势库通过名称 获取该名称下的所有手势ArrayList<Gesture> gestures = libraries.getGestures(prediction.name);//由于我们的手势 使用的唯一名称 所以只需要判断大于0 获取第0个 就可以if(gestures.size()>0){Gesture gesture_temp = gestures.get(0);Bitmap bitmap1 = gesture_temp.toBitmap(100, 100, 10, Color.RED);mImage2.setImageBitmap(bitmap1);}}}}//添加手势到库libraries.addGesture(String.valueOf(System.currentTimeMillis()),gesture);//保存手势libraries.save();}});}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context="com.qf.gestureoverlayview.MainActivity"><LinearLayout
android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"><ImageView
android:background="#400f"android:id="@+id/img1"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent" /><ImageView
android:id="@+id/img2"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"/></LinearLayout><android.gesture.GestureOverlayView
android:background="#000"android:id="@+id/gest"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gestureStrokeType="multiple"android:gestureStrokeLengthThreshold="2"></android.gesture.GestureOverlayView>
</LinearLayout>
这篇关于通过GestureOverlayView手势库来识别手写的字的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!