Detecting hand gestures on Samsung Galaxy.

The Gesture SDK on Samsung Galaxy phones supports an interesting feature of detecting palm movements in front of the screen. The issue however is that they’re only supported on Galaxy S5, Note3 and Note4 devices and since December 2014, Samsung provides no further updates for the SDK for new Samsung mobile devices. But it’s still a really interesting feature to work with in a research project and if you have access to one of the above mentioned phones.

Here’s a simple code as to how you can detect the direction of palm movement.

Detecting if the feature is supported and enabled on the device :

Sgesture sg;
sg = new Sgesture();

try {
	sg.initialize(context);
} catch (SsdkUnsupportedException se) {
	Log.e(TAG, "Gestures not supported");
	return;
}

if (!sg.isFeatureEnabled(Sgesture.TYPE_HAND_PRIMITIVE)) {
	Log.e(TAG, "Gestures not enabled");
	return;
} else {
	Log.i(TAG, "Gestures are enabled");
}    

If all this works well, you’re halfway there.

Detecting movements :

SgestureHand sghand;
SgestureHand.ChangeListener listener;

if (sghand == null) {
	sghand = new SgestureHand(getMainLooper(), sg);
}

if (listener == null) {
	listener = new SgestureHand.ChangeListener() {
	    @Override
		public void onChanged(SgestureHand.Info info) {
			int angle = info.getAngle();
			Direction direction;
			WindowManager windowService = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
			int rotation = windowService.getDefaultDisplay().getRotation();
			switch (rotation) {
				case Surface.ROTATION_0:
					direction = Direction.computeDirectionFrom(angle);
					break;
				case Surface.ROTATION_90:
					direction = Direction.computeDirectionFrom(angle + 90);
					break;
				case Surface.ROTATION_180:
					direction = Direction.computeDirectionFrom(angle + 180);
					break;
				default:
					direction = Direction.computeDirectionFrom(angle + 270);
					break;
			}
			Log.i(TAG, "Direction : " + direction.toString());
		}
	};

	sghand.start(Sgesture.TYPE_HAND_PRIMITIVE, listener);
}

By default, the onChanged() method only receives an angle. The angle of the hand at the end point of the gesture.

To be able to convert it to a direction we use a Direction Enum. It converts the raw angle into the the direction of palm movement.

public enum Direction {
    LEFT(225, 315), TOP(135, 225), RIGHT(45, 135), BOTTOM(315, 45);

    private final int mLowerLimit;
    private final int mUpperLimit;

    Direction(int lowerLimit, int upperLimit) {
        mLowerLimit = lowerLimit;
        mUpperLimit = upperLimit;
    }

    private boolean isAngleWithinRange(int angle) {
        if (mLowerLimit <= mUpperLimit) {
            return (mLowerLimit < angle && angle <= mUpperLimit);
        } else {
            return (mLowerLimit < angle || angle <= mUpperLimit);
        }
    }

    public static Direction computeDirectionFrom(int angle) {
        final int normalizedAngle = normalizeAngle(angle);

        if (LEFT.isAngleWithinRange(normalizedAngle)) {
            return LEFT;
        } else if (RIGHT.isAngleWithinRange(normalizedAngle)) {
            return RIGHT;
        } else if (BOTTOM.isAngleWithinRange(normalizedAngle)) {
            return BOTTOM;
        } else {
            return TOP;
        }
    }

    private static int normalizeAngle(int angle) {
        int normalizedAngle = angle % 360;
        normalizedAngle = (normalizedAngle + 360) % 360;
        return normalizedAngle;
    }
}

The sensors seem to be pretty accurate. It does miss a couple of events if you move the palm really quick, or if the palm is a little too far from the sensor but overall it’s really easy to work with.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s