通过GestureOverlayView手势库来识别手写的字

来源:互联网 发布:光束灯编程 512 编辑:程序博客网 时间:2024/05/22 15:31

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;    @Override    protected 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() {            @Override            public 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+"");                        //且当 相似度大于10                        if(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>
0 0
原创粉丝点击