I ran Gestures Builder app, created gestures file for slide left/right and wrote this code:
public class MainActivity extends Activity implements OnGesturePerformedListener {
private GestureLibrary mGestureLibrary;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
gestureOverlayView.addView(inflate);
gestureOverlayView.addOnGesturePerformedListener(this);
mGestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (mGestureLibrary == null) {
finish();
}
setContentView(gestureOverlayView);
}
#Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mGestureLibrary.recognize(gesture);
for (Prediction prediction : predictions) {
if (prediction.score > 1.0) {
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
}
}
}
}
gestures are in /raw/, but the app says nothing when I try to test it (it loads gesture successfully, the event onGesturePerformed is called, but the gestures are not recognized). The gestures work perfectly in Gestures Buileder, so where is my mistake?
You need to still check the name of the prediction which should match the name of one of your actions. Test for equality and then perform your logic:
String action = predictions.get(0).name;
if("right".equals(action){
}
You may need to call load() on mGestureLibrary before using it. Not that it's documented at all, but that's what Lars does in this example and it works for me: http://www.vogella.com/articles/AndroidGestures/article.html
In the IDE you should be able to see entries in the GestureStore HashMap (mNamedGestures).
Related
I have implemented handwriting gestures for android character recognition for a single character as shown here.
But i want to read multiple characters at a single time, .i.e Android. I am able to read a single character by the following implementation. But, I need help to recognize a complete word like Android at the same time.
My implementation so far is shown below.
GestureLibrary mLibrary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
}
#Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
String result = predictions.get(0).name;
if ("a".equalsIgnoreCase(result))
{
////Toast.makText(this, "a", Toast.LENGTH_LONG).show();
textView.setText(textView.getText().toString()+"a");
}
}
}
Before putting my question, I want everyone there to know that I have checked and read all the questions and answers on stackoverflow regarding this question.
This is in my layout :
<android.support.v7.app.MediaRouteButton
android:id="#+id/button_fling"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:background="#drawable/mr_ic_media_route_holo_light"
android:layout_height="wrap_content"
android:mediaRouteTypes="user"
android:layout_weight="1"
android:visibility="visible" />
and this in my my activity:
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
mMediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(getString(R.string.app_id)))
.build();
mMediaRouterCallback = new MyMediaRouterCallback();
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.button_fling);
mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
button_fling2 = (ImageView)findViewById(R.id.button_fling2);
}
public class MyMediaRouterCallback extends MediaRouter.Callback {
public int mRouteCount =0;
#Override
public void onRouteAdded(MediaRouter router, RouteInfo route) {
Log.d(TAG, "onRouteAdded");
if (++mRouteCount == 1) {
// Show the button when a device is discovered.
Log.i(TAG,"MediaRoute is visible");
button_fling2.setVisibility(View.VISIBLE);
mMediaRouteButton.setVisibility(View.VISIBLE);
}
}
#Override
public void onRouteRemoved(MediaRouter router, RouteInfo route) {
Log.d(TAG, "onRouteRemoved");
if (--mRouteCount == 0) {
// Hide the button if there are no devices discovered.
Log.i(TAG,"MediaRoute is GONE");
button_fling2.setVisibility(View.GONE);
mMediaRouteButton.setVisibility(View.GONE);
}
}
}
ButtonFling2 is an ImageView I am using to test MyMediaRouterCallback is working or not. It successfully hides/shows the imageView. However for button_fling(which is a mediaRouteButton instance) does not show anything. It's as if it can't find the resources of the MediaRouteButton so it is showing no cast icon... anyone ever fix this or come across this?
I don't get any errors it just simply does not show, but the logs show that it is visible and the ImageView i have for testing shows up.
AFter fiddling around with this thing, I was able to get the media Router button to show up. I am not entirely sure what I did but I did verify the appid and tried to use my whitelisted app id. I reinstalled the support library. THANK YOU ALL FOR WHO HELPED ON THIS!
I am not 100% sure how this happened but I hope that this helps someone figure out or at least get past this. ActionBar Activity and non action bar activities show the media router button now!!! :)
Implementing onRouteChanged() callback instead of onRouteAdded() should fix the issue.
Example code:
// for MediaRouterButtonActivity
#Override
public void onRouteChanged(MediaRouter router, RouteInfo route) {
if (++mRouteCount == 1) {
// Show the button when a device is discovered.
mMediaRouteButton.setVisibility(View.VISIBLE);
}
}
Is it possible to replace a gesture template of template library from a running application?
I am building a handwriting recognizer system in which there are templates of letters in gesture library file.So basically after loading the library inside the code i compare user input gesture like:
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gesturelib.recognize(gesture);
if (predictions.size() > 1) {
for(Prediction prediction: predictions){
//i compare prediction name here and if matches print it in an edittext
}
This should work good until user will give the same pattern like i did during building the library template.But i want to give an user that flexibility to replace an template item with his handwritten pattern when there is a mismatch of prediction.
Because below 2 handwritten gesture samples are different in terms of pattern,but not as a letter.Suppose,my system supports the 1st image pattern,i want when user will give 2nd image pattern the system will ask confirmation from user to replace it with the library pattern of A and then replace it after confirm.So next time the system will recognize user pattern much better.
Any help would be greatly appreciated.
If I understand correctly, you want to replace an already existing gesture with a new one?
So, when the user inputs a gesture that isn't in the library, your app asks the user to select which gesture they wish to replace? From your question, I will assume that when user draws a lowercase a(and if a isn't in the library), user is presented with a list of all available gestures/letters that your app currently supports. And then, the user selects capital A, and now, capital A must be replaced with lowercase a. In the following code, oldGesture is the Gesture corresponding to A. And newGesture is the Gesture just drawn.
The process for that would be: delete the old gesture, add the new one using old gesture's name. To delete a gesture, use GestureLibrary.removeGesture(String, Gesture):
public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
ArrayList<Prediction> predictions = gesturelib.recognize(gesture);
if (predictions.size() > 1) {
for(Prediction prediction: predictions){
if (prediction.score > ...) {
} else {
if (user wants to replace) {
showListWithAllGestures(gesture);
}
}
}
}
}
public void showListWithAllGestures(Gesture newGesture) {
....
....
// User picks a gesture
Gesture oldGesture = userPickedItem.gesture;
String gestureName = userPickedItem.name;
// delete the gesture
gesturelib.removeGesture(gestureName, oldGesture);
gesturelib.save();
// add gesture
gesturelib.addGesture(gestureName, newGesture);
gesturelib.save();
}
To get a list of all available gestures:
// Wrapper to hold a gesture
static class GestureHolder {
String name;
Gesture gesture;
}
Load gestures using GestureLibrary.load():
if (gesturelib.load()) {
for (String name : gesturelib.getGestureEntries()) {
for (Gesture gesture : gesturelib.getGestures(name)) {
final GestureHolder gestureHolder = new GestureHolder();
gestureHolder.gesture = gesture;
gestureHolder.name = name;
// Add `gestureHolder` to a list
}
}
// Return the list that holds GestureHolder objects
}
Edit:
Sorry, but the check I have suggested: if (wants to replace) is being carried out at the wrong place in code.
if (predictions.size() > 1) {
// To check whether a match was found
boolean gotAMatch = false;
for(int i = 0; i < predictions.size() && !gotAMatch; i++){
if (prediction.score > ... ) {
....
....
// Found a match, look no more
gotAMatch = true;
}
}
// If a match wasn't found, ask the user s/he wants to add it
if (!gotAMatch) {
if (user wants to replace) {
showListWithAllGestures(gesture);
}
}
}
I have one screen where the user creates a gesture, then the user presses Next and on the Next screen it asks the user to confirm that gesture. Basically I want the gesture stored as a password. The gesture creation works fine, I have the following lines of code which store the gesture in the device's memory:
GestureLibrary store = GestureLibraries.fromRawResource(this, R.raw.gestures);;
store.addGesture("Gesture Password", mGesture);
store.save();
setResult(RESULT_OK);
I'm still messing around with how to recognize the gesture but for now I'm just trying to figure out how to compare against this stored gesture in memory. I have the following:
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for (Prediction prediction : predictions) {
if (prediction.score > 1.0) {
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
}
}
}
Where:
private GestureLibrary gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
As I understand, on the first screen I create the gesture, it's stored in raw.gestures, then I try accessing it again in the confirm screen and if the confirm gesture is similar enough, the toast should return "Gesture Password". But this doesn't work for some reason, what am I missing? Am I storing and retrieving the gesture incorrectly?
Hi i am following this tutorial
http://www.vogella.de/articles/AndroidGestures/article.html
i want to create an application in which user can add his gesture inmy application and then use it for authentication.i know using this code i can check whether gesture entered by him is correct or not.
package de.vogella.android.gestures;
import java.util.ArrayList;
public class GestureTest extends Activity implements OnGesturePerformedListener {
private GestureLibrary gestureLib;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
gestureOverlayView.addView(inflate);
gestureOverlayView.addOnGesturePerformedListener(this);
gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gestureLib.load()) {
finish();
}
setContentView(gestureOverlayView);
}
#Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for (Prediction prediction : predictions) {
if (prediction.score > 1.0) {
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT)
.show();
}
}
}
}
ok but please help me that how to add in gesture in R.raw.animate file.please suggest any way or link for adding a gesture in android app .
Extracted from here :
Android 1.6 and higher SDK platforms include a new application
pre-installed on the emulator, called Gestures Builder. You can use
this application to create a set of pre-defined gestures for your own
application...
...
As you can see, a gesture is always associated with a name. That name
is very important because it identifies each gesture within your
application. The names do not have to be unique. Actually it can be
very useful to have several gestures with the same name to increase
the precision of the recognition. Every time you add or edit a gesture
in the Gestures Builder, a file is generated on the emulator's SD
card, /sdcard/gestures. This file contains the description of all the
gestures, and you will need to package it inside your application
inside the resources directory, in /res/raw.
Here you have the source code of Gesture Builder
Gesture builder is installed in the emulator , but you can download it from here
And gesture source code examples here
Also, you may need to call gestureLib.load() before using it
Source code of gesture builder:
https://android.googlesource.com/platform/development/+/master/apps/GestureBuilder/