Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.57 KB | None | 0 0
  1. /******************************************************************************\
  2. * Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved.               *
  3. * Leap Motion proprietary and confidential. Not for distribution.              *
  4. * Use subject to the terms of the Leap Motion SDK Agreement available at       *
  5. * https://developer.leapmotion.com/sdk_agreement, or another agreement         *
  6. * between Leap Motion and you, your company or other organization.             *
  7. \******************************************************************************/
  8.  
  9. // Approved by Alex for use as of 7:00 pm
  10.  
  11. import java.awt.AWTException;
  12. import java.io.IOException;
  13. import java.lang.Math;
  14. import com.leapmotion.leap.*;
  15. import com.leapmotion.leap.Gesture.State;
  16.  
  17.  
  18. class SampleListener extends Listener {
  19.     ProgressBar pb;
  20.     public void onInit(Controller controller) {
  21.         System.out.println("Initialized");
  22.     }
  23.  
  24.     public void onConnect(Controller controller) {
  25.         System.out.println("Connected");
  26.         controller.enableGesture(Gesture.Type.TYPE_SWIPE);
  27.         controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
  28.         controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
  29.         controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
  30.         pb = new ProgressBar();
  31.     }
  32.  
  33.     public void onDisconnect(Controller controller) {
  34.         //Note: not dispatched when running in a debugger.
  35.         System.out.println("Disconnected");
  36.     }
  37.  
  38.     public void onExit(Controller controller) {
  39.         System.out.println("Exited");
  40.     }
  41.  
  42.     public void onFrame(Controller controller) {
  43.         // Get the most recent frame and report some basic information
  44.         Frame frame = controller.frame();
  45. //        System.out.println("Frame id: " + frame.id()
  46. //                         + ", timestamp: " + frame.timestamp()
  47. //                         + ", hands: " + frame.hands().count()
  48. //                         + ", fingers: " + frame.fingers().count()
  49. //                         + ", tools: " + frame.tools().count()
  50. //                         + ", gestures " + frame.gestures().count());
  51.  
  52.         //Get hands
  53.         for(Hand hand : frame.hands()) {
  54.             String handType = hand.isLeft() ? "Left hand" : "Right hand";
  55. //            System.out.println("  " + handType + ", id: " + hand.id()
  56. //                             + ", palm position: " + hand.palmPosition());
  57.  
  58.             // Get the hand's normal vector and direction
  59.             Vector normal = hand.palmNormal();
  60.             Vector direction = hand.direction();
  61.  
  62.             // Calculate the hand's pitch, roll, and yaw angles
  63. //            System.out.println("  pitch: " + Math.toDegrees(direction.pitch()) + " degrees, "
  64. //                             + "roll: " + Math.toDegrees(normal.roll()) + " degrees, "
  65. //                             + "yaw: " + Math.toDegrees(direction.yaw()) + " degrees");
  66.  
  67.             // Get arm bone
  68.             Arm arm = hand.arm();
  69. //            System.out.println("  Arm direction: " + arm.direction()
  70. //                             + ", wrist position: " + arm.wristPosition()
  71. //                             + ", elbow position: " + arm.elbowPosition());
  72.  
  73.             // Get fingers
  74.             for (Finger finger : hand.fingers()) {
  75. //                System.out.println("    " + finger.type() + ", id: " + finger.id()
  76. //                                 + ", length: " + finger.length()
  77. //                                 + "mm, width: " + finger.width() + "mm");
  78.  
  79.                 //Get Bones
  80.                 for(Bone.Type boneType : Bone.Type.values()) {
  81.                     Bone bone = finger.bone(boneType);
  82. //                    System.out.println("      " + bone.type()
  83. //                                     + " bone, start: " + bone.prevJoint()
  84. //                                     + ", end: " + bone.nextJoint()
  85. //                                     + ", direction: " + bone.direction());
  86.                 }
  87.             }
  88.         }
  89.  
  90.         // Get tools
  91.         for(Tool tool : frame.tools()) {
  92. //            System.out.println("  Tool id: " + tool.id()
  93. //                             + ", position: " + tool.tipPosition()
  94. //                             + ", direction: " + tool.direction());
  95.         }
  96.  
  97.         GestureList gestures = frame.gestures();
  98.         for (int i = 0; i < gestures.count(); i++) {
  99.             Gesture gesture = gestures.get(i);
  100.  
  101.             switch (gesture.type()) {
  102.                 case TYPE_CIRCLE:
  103.                     CircleGesture circle = new CircleGesture(gesture);
  104.  
  105.                     // Calculate clock direction using the angle between circle normal and pointable
  106.                     String clockwiseness;
  107.                     if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI/2) {
  108.                         // Clockwise if angle is less than 90 degrees
  109.                         clockwiseness = "clockwise";
  110.                     } else {
  111.                         clockwiseness = "counterclockwise";
  112.                     }
  113.  
  114.                     // Calculate angle swept since last frame
  115.                     double sweptAngle = 0;
  116.                     if (circle.state() != State.STATE_START) {
  117.                         CircleGesture previousUpdate = new CircleGesture(controller.frame(1).gesture(circle.id()));
  118.                         sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * Math.PI;
  119.                     }
  120.                     if(clockwiseness.equals("clockwise")) {
  121.                         // zoom in
  122.                         pb.changeZoom(1);
  123.                             System.out.println("Circle in");
  124.                     }else {
  125.                         // zoom out
  126.                         pb.changeZoom(-1);
  127.                             System.out.println("Circle out");
  128.                     }
  129.                     try {
  130.                         Thread.sleep(1000);
  131.                     } catch (Exception e) {
  132.                         // TODO: handle exception
  133.                     }
  134.                    
  135. //                    System.out.println("  Circle id: " + circle.id()
  136. //                               + ", " + circle.state()
  137. //                               + ", progress: " + circle.progress()
  138. //                               + ", radius: " + circle.radius()
  139. //                               + ", angle: " + Math.toDegrees(sweptAngle)
  140. //                               + ", " + clockwiseness);
  141.                     break;
  142.                 case TYPE_SWIPE:
  143.                     SwipeGesture swipe = new SwipeGesture(gesture);
  144.                     if(swipe.direction().getX()<0) {
  145.                             System.out.println("Swipe left");
  146.                             // brightness down
  147.                             pb.changeBrightness(-1);
  148.                     }else {
  149.                         // brightness up
  150.                         pb.changeBrightness(1);
  151.                             System.out.println("Swipe right");
  152.                     }
  153.                    
  154.                     try {
  155.                         Thread.sleep(1000);
  156.                     } catch (Exception e) {
  157.                         // TODO: handle exception
  158.                     }
  159. //                    System.out.println("  Swipe id: " + swipe.id()
  160. //                               + ", " + swipe.state()
  161. //                               + ", position: " + swipe.position()
  162. //                               + ", direction: " + swipe.direction()
  163. //                               + ", speed: " + swipe.speed());
  164.                     break;
  165.                 case TYPE_SCREEN_TAP:
  166.                     ScreenTapGesture screenTap = new ScreenTapGesture(gesture);
  167.                     System.out.println("Tap");
  168.                     if (pb.isVideo_running()) {
  169.                         try {
  170.                             pb.stop_Video();
  171.                         } catch (AWTException e) {
  172.                             // TODO Auto-generated catch block
  173.                             e.printStackTrace();
  174.                         }
  175.                     } else {
  176.                         try {
  177.                             pb.capture_video();
  178.                         } catch (IOException e) {
  179.                             // TODO Auto-generated catch block
  180.                             e.printStackTrace();
  181.                         } catch (AWTException e) {
  182.                             // TODO Auto-generated catch block
  183.                             e.printStackTrace();
  184.                         }
  185.                     }
  186.                     // STart stop video
  187.                    
  188.                     try {
  189.                         Thread.sleep(1000);
  190.                     } catch (Exception e) {
  191.                         // TODO: handle exception
  192.                     }
  193. //                    System.out.println("  Screen Tap id: " + screenTap.id()
  194. //                               + ", " + screenTap.state()
  195. //                               + ", position: " + screenTap.position()
  196. //                               + ", direction: " + screenTap.direction());
  197.                     break;
  198.                 case TYPE_KEY_TAP:
  199.                     KeyTapGesture keyTap = new KeyTapGesture(gesture);
  200.                     System.out.println("KTap");
  201.                 try {
  202.                     pb.capture_img();
  203.                 } catch (IOException e1) {
  204.                     // TODO Auto-generated catch block
  205.                     e1.printStackTrace();
  206.                 } catch (AWTException e1) {
  207.                     // TODO Auto-generated catch block
  208.                     e1.printStackTrace();
  209.                 }
  210.                     try {
  211.                         Thread.sleep(1000);
  212.                     } catch (Exception e) {
  213.                         // TODO: handle exception
  214.                     }
  215. //                    System.out.println("  Key Tap id: " + keyTap.id()
  216. //                               + ", " + keyTap.state()
  217. //                               + ", position: " + keyTap.position()
  218. //                               + ", direction: " + keyTap.direction());
  219.                     break;
  220.                 default:
  221.                     System.out.println("Unknown gesture type.");
  222.                     break;
  223.             }
  224.         }
  225.  
  226. //        if (!frame.hands().isEmpty() || !gestures.isEmpty()) {
  227. //            System.out.println();
  228. //        }
  229.     }
  230. }
  231.  
  232. class Sample {
  233.     public static void main(String[] args) {
  234.         // Create a sample listener and controller
  235.         SampleListener listener = new SampleListener();
  236.         Controller controller = new Controller();
  237.  
  238.         // Have the sample listener receive events from the controller
  239.         controller.addListener(listener);
  240.  
  241.         // Keep this process running until Enter is pressed
  242.         System.out.println("Press Enter to quit...");
  243.         try {
  244.             System.in.read();
  245.         } catch (IOException e) {
  246.             e.printStackTrace();
  247.         }
  248.  
  249.         // Remove the sample listener when done
  250.         controller.removeListener(listener);
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement