zoukankan      html  css  js  c++  java
  • Android 使用手势库和GestureOverlayView验证手势

    android 高级操作 调用是手势库并进行30% 正确率识别

    Android 手势识别 - 这个功能我觉得很强大,你可以用来做安全登录等。
    首先我们先看一下如何使创建我们的手势库,这个很重要,因为我们的手势验证都要根据这个手势库去执行。
    我们这篇专讲如何进行手势验证,不说如何保存手势。

    我们这里需要使用一个开发者工具叫做我们的手势库录入工具Getstures Builder。这个软件可以帮助我们进行我们手势录入工作。

    开发环境

    Android Studio 3.5.3
    使用 Android X
    Android Studio 安装位置默认

    我们看一下如何使用我们的软件

    这个软件在老的android 模拟器中有集成,但是新版的没有了
    ,但是给我们提供了APK 文件,接下来让我们看一下如何安装它!
    我们进入android-sdk 文件夹然后搜索GestureBuilder
    就可以找到我们的APK 文件了。然后你找一台Android 机安装一下,然后我们打开软件添加我们的手势即可。

    软件中如何使用手势识别

    在android 控件中有一个神秘的控件,叫做GestureOverlayView
    按照国际管理,我们看以下源代码 - 布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <android.gesture.GestureOverlayView
            android:id="@+id/gestures"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gestureColor="@color/colorAccent"
            android:gestureStrokeWidth="10"
            android:gestureStrokeType="multiple" />
    </LinearLayout>
    

    这个布局很简单,不做过多的讲解。


    然后我们看一下我们的核心代码

    package com.example.stylequickcontactbadge;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.lifecycle.ViewModelProvider;
    
    import android.Manifest;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.gesture.Gesture;
    import android.gesture.GestureLibraries;
    import android.gesture.GestureLibrary;
    import android.gesture.GestureOverlayView;
    import android.gesture.Prediction;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.AndroidException;
    import android.widget.ArrayAdapter;
    import android.widget.MultiAutoCompleteTextView;
    import android.widget.QuickContactBadge;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    
    public class MainActivity extends Activity {
        private boolean success;
        private GestureLibrary library;
        private GestureOverlayView gestireView;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //找到手势库
            library = GestureLibraries.fromRawResource(this, R.raw.gestures);
            success = library.load();
            gestireView = this.findViewById(R.id.gestures);
    
            gestireView.addOnGesturePerformedListener(new GestureListener());
    
    
        }
    
    //创建手势识别类
        private final class GestureListener implements GestureOverlayView.OnGesturePerformedListener {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                if (success) {
                    ArrayList<Prediction> predictions = library.recognize(gesture);
                    if (!predictions.isEmpty()) {
                        Prediction prediction = predictions.get(0);
                        if (prediction.score > 3) {
                            if ("agree".equals(prediction.name)) {
                                Toast.makeText(MainActivity.this, "this is agree", Toast.LENGTH_SHORT).show();
                            } else if ("5556".equals(prediction.name)) {
                                Toast.makeText(MainActivity.this, "This is 5556 the check", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(MainActivity.this, "Sorry about your gesture! Please try again.", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                }
            }
        }
    
    }
    

    这些就是手势识别的全部的核心代码了。

    然后让我们看一下如何去使用这个控件。

    我们先使创建了一个bool值,来判断手势库初始化是否成功。
    然后创建一个GestureLibrary 让他作为我们手势验证的手势库
    然后创建一个GestureOverlayView 他使作为我们手势验证验证平台


    接着看看我们的源代码,我们先将我们之前录入好的手势文件和我们的手势库绑定。
    然后判断我们的手势库是否初始换完毕。

    然后我们看一下我们的验证环节:
    我们在这里定义了一个GestureListener 他需要使用接口GestureOverlayView.OnGesturePerformedListener这样我们的手势验证的帮助类就完成了。
    如果我们手势库初始换成功了。我们通过ArrayList来添加。然后判断手势是否成功,如果存在我们就进行相似度比对,如果相似度>30% 我们就认定手势验证成功,然后我们在根据名字来判断哪个手势验证成功了。
    还是很简单的啦。
    但是手势录入我还不会....

  • 相关阅读:
    网络拓扑
    OSI 7层模型和TCP/IP 4层模型
    第一范式 第二范式 第三范式 BC范式 第四范式
    医院 信息科
    李纳斯•托瓦兹
    所谓绅士,就是做自己该做之事,而不是想做之事。
    活着
    开头词
    人际题目
    人际关系
  • 原文地址:https://www.cnblogs.com/cao-1/p/12162105.html
Copyright © 2011-2022 走看看