Samples¶
The sample project contains a very simple demo. It starts detection on startup, and displays detection result in text. It also shows camera permission granting, stops/restarts detection when app pauses/resumes
Start detection on app startup/resume¶
// This is called on application start/restart
@Override
protected void onStart() {
super.onStart();
// check camera permission
String[] permissions = new String[]{ "android.permission.CAMERA" };
if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions, 0);
return;
}
// start detection
start();
}
// permission check callback
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// only start detection if camera permission is granted
start();
} else {
textView.setText("Camera permission is required");
}
}
// actual function to start detection
private void start() {
// start detection with default option
GestureFailure result = GestureInterface.Start(null);
if (result != GestureFailure.None) {
// check start error
textView.setText("Start Detection Failed: " + result.toString());
return;
}
textView.setText("Start Detection Success");
// start repeated runnable to fetch results
timerHandler.postDelayed(timerRunnable, 0);
}
Stop Detection on App Stop/Pause¶
@Override
protected void onStop() {
super.onStop();
// stop runnable
timerHandler.removeCallbacks(timerRunnable);
// stop detection when activity becomes invisible
GestureInterface.Stop();
}
Fetching and Using Detection Results¶
Handler timerHandler = new Handler();
// this runnable is invoked every 30 ms to check new results.
Runnable timerRunnable = new Runnable() {
@Override
public void run() {
// update result and get frame index
GestureInterface.UpdateResult();
int index = GestureInterface.GetFrameIndex();
Formatter f = new Formatter();
f.format("Frame Index: %d\n", index);
if (index >= 0) {
// index >= 0 means detection is up and running
GestureResult left = GestureInterface.GetLeft();
// left.valid is true if left hand exists. gesture and point is only meaningful in such case
if (left.valid)
f.format("Left: %-8s\t%s\n", left.gesture, left.point);
else
f.format("Left: None\n");
GestureResult right = GestureInterface.GetRight();
if (right.valid)
f.format("Right: %-8s\t%s\n", right.gesture, right.point);
else
f.format("Right: None\n");
}
textView.setText(f.toString());
// if index < 0, detection is not running (or stopped due to error), so we don't need to run this runnable
if (index >= 0)
timerHandler.postDelayed(this, 30);
}
};