/// <summary> ///阅读的身体框架 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void bodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) { bool dataReceived = false; //获取一帧身体数据 using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame()) { if (bodyFrame != null) { if (this.bodies == null) { this.bodies = new Body[bodyFrame.BodyCount]; } //第一次GetAndRefreshBodyData被调用时,Kinect将在数组中分配每个主体。 //只要这些物体不被处理,并且在数组中不被设置为空, //这些物体将被重新使用。 bodyFrame.GetAndRefreshBodyData(this.bodies); dataReceived = true; } } if (!dataReceived) { alreadyTrackedPos = false; return; } foreach (Body body in this.bodies) { //第一个被跟踪的身体,注意下面有一个休息。 if (body.IsTracked) { // 得到各种骨骼位置 CameraSpacePoint handLeft = body.Joints[JointType.HandLeft].Position; CameraSpacePoint handRight = body.Joints[JointType.HandRight].Position; CameraSpacePoint spineBase = body.Joints[JointType.SpineBase].Position; // 如果右手向前推进 if (handRight.Z - spineBase.Z < -0.15f) { //用这个计算的手x。我们不使用右肩作为参考, //因为肩膀的右肩通常是在升力的后面,而这个位置是推断和不稳定的。 //因为脊椎底部在右手的左边,我们加上0.05f,使它更接近右边。 float x = handRight.X - spineBase.X + 0.05f; //用这个来计算。它的脊柱底部比右方要低,我们加上0.51f来让它成为higer, //值0.51是通过多次测试来计算的,你可以把它设置为你喜欢的另一个。 float y = spineBase.Y - handRight.Y + 0.51f; // 得到当前光标位置 Point curPos = MouseControl.GetCursorPosition(); //使用的平滑度应该是0-0.95f。我们的算法是:oldPos + (newPos - oldPos) * smoothValue float smoothing = 1 - cursorSmoothing; // 设置光标位置 MouseControl.SetCursorPos((int)(curPos.X + (x * mouseSensitivity * screenWidth - curPos.X) * smoothing), (int)(curPos.Y + ((y + 0.25f) * mouseSensitivity * screenHeight - curPos.Y) * smoothing)); alreadyTrackedPos = true; //控制动作 if (doClick && useGripGesture) {//用握拳动作进行完成点击事件 ///////////////////////添加缩放功能///////////////////////////// bool isZoom = (System.Math.Abs(handRight.Z - body.Joints[JointType.ShoulderRight].Position.Z) > 0.5); if (body.HandRightState == HandState.Open && isZoom) { MouseControl.MouseAmplification(); System.Threading.Thread.Sleep(200); } else if (body.HandRightState == HandState.Closed && isZoom) { MouseControl.MouseNarrow(); System.Threading.Thread.Sleep(200); } //////////////////////////////////////////////////// if (body.HandRightState == HandState.Closed && !isZoom) { if (!wasRightGrip) { MouseControl.MouseLeftDown(); wasRightGrip = true; } } else if (body.HandRightState == HandState.Open && !isZoom) { if (wasRightGrip) { MouseControl.MouseLeftUp(); wasRightGrip = false; } } } } //////////////////实现双手识别/////////////////////////////////////////////// //else if (handLeft.Z - spineBase.Z < -0.15f) // if left hand lift forward //{ // float x = handLeft.X - spineBase.X + 0.3f; // float y = spineBase.Y - handLeft.Y + 0.51f; // Point curPos = MouseControl.GetCursorPosition(); // float smoothing = 1 - cursorSmoothing; // MouseControl.SetCursorPos((int)(curPos.X + (x * mouseSensitivity * screenWidth - curPos.X) * smoothing), (int)(curPos.Y + ((y + 0.25f) * mouseSensitivity * screenHeight - curPos.Y) * smoothing)); // alreadyTrackedPos = true; // if (doClick && useGripGesture) // { // if (body.HandLeftState == HandState.Closed) // { // if (!wasLeftGrip) // { // MouseControl.MouseLeftDown(); // wasLeftGrip = true; // } // } // else if (body.HandLeftState == HandState.Open) // { // if (wasLeftGrip) // { // MouseControl.MouseLeftUp(); // wasLeftGrip = false; // } // } // } //} else { wasLeftGrip = true; wasRightGrip = true; alreadyTrackedPos = false; } // 第一个被跟踪的身体 break; } } }