首页 > 解决方案 > 初学者编码器:Java(处理)和 Leap Motion 控制器教程?

问题描述

我的编码经验很少,对于我的大学项目原型,我需要编写一个图像(处理中的 PImage)以使用我的手移动(Leap Motion 控制器输入)。

目前我被困在如何让我的图像使用我的手的位置移动,但弹出错误说图像只能使用浮点数作为输入,因为手的位置是使用 PVector 类计算的。我试图用float[] (name) = handpos.array();它来使它成为一个浮点值,但是我无法从图像所需的位置获取 x 和 y 值。

这是我到目前为止在 for 循环中完成的代码,是我计划添加要显示的图像的地方。

import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50,113,250);

void setup(){
  size(700, 700);
  background(255);
  leap = new LeapMotion(this);
  hand = loadImage("images/hand.png");
  imageMode(CENTER);
}

void draw() {
background (ocean);
int fps = leap.getFrameRate();
  for (Hand hand : leap.getHands ()) {
    Finger index = hand.getIndexFinger();
    PVector hpos = hand.getPosition(); 
    PVector ipos = index.getPosition(); 
}
 
}

标签: javaprocessingleap-motion

解决方案


迪特里希的回答是正确的 (+1)。

有几点需要注意。您得到的错误(期望参数,如:“image(PImage,float,float)”)是由于变量阴影

您可能正在尝试hand在 for 循环内渲染图像,但是,循环使用同名 ( hand) 的局部变量,在本例中是 LeapMotionHand实例,而不是PImage.

for 循环变量(Hand实例)正在遮蔽全局变量(PImage变量)。

发生错误是因为它尝试渲染 LeapMotionHand而不是您的PImage.

您可以简单地重命名局部变量:

import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50, 113, 250);

void setup() {
  size(700, 700);
  background(255);
  leap = new LeapMotion(this);
  hand = loadImage("images/hand.png");
  imageMode(CENTER);
}

void draw() {
  background (ocean);
  int fps = leap.getFrameRate();
  for (Hand leapHand : leap.getHands ()) {
    Finger index = leapHand.getIndexFinger();
    PVector hpos = leapHand.getPosition(); 
    PVector ipos = index.getPosition();
    image(hand, hpos.x, hpos.y);
  }
}

这将每只手渲染多个图像。

如果你想为一只手渲染一个图像,你可以做多件事。

您可以从循环中提取位置,这意味着最近的手将控制图像位置:

import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50, 113, 250);

PVector handPosition = new PVector();

void setup() {
  size(700, 700);
  background(255);
  leap = new LeapMotion(this);
  hand = loadImage("images/hand.png");
  imageMode(CENTER);
}

void draw() {
  background (ocean);
  int fps = leap.getFrameRate();
  // hand inside is the loop is a Leap Hand instance, shadowing the PImage
  for (Hand hand : leap.getHands ()) {
    Finger index = hand.getIndexFinger();
    PVector hpos = hand.getPosition(); 
    PVector ipos = index.getPosition();
    handPosition.set(hand.getPosition());
  }
  // hand here is your PImage
  image(hand, handPosition.x, handPosition.y);
  
}

或者您可以使用第一手,例如:

import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50, 113, 250);

PVector handPosition = new PVector();

void setup() {
  size(700, 700);
  background(255);
  leap = new LeapMotion(this);
  hand = loadImage("images/hand.png");
  imageMode(CENTER);
}

void draw() {
  background (ocean);
  int fps = leap.getFrameRate();
  // try to read first hand data
  Hand firstHand = leap.getHand(0);
  // check if there is actual data (at least one hand is present)
  if(firstHand != null){
    // read hand's position
    PVector handPosition = hand.getPosition();
    // render image to hand's position
    image(hand, handPosition.x, handPosition.y);
  }
  
}

有很多方法可以做到这一点,这取决于对您的项目的用户体验有意义的方式。


推荐阅读