我在编码方面的经验很少,对于我的大学项目原型(prototype),我需要对图像(处理中的 PImage)进行编码以使用我的手移动(Leap Motion Controller 输入)。
目前,我陷入了如何使用手的位置移动图像的困境,但弹出错误说图像只能使用 float 作为输入,因为手的位置是使用 PVector 计算的类(class)。我尝试使用 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();
}
}
最佳答案
Dietrich 的答案是正确的 (+1)。
有一些注意事项。 您收到的错误(预计参数如下:“image(PImage, float, float)”)是由于 variable shadowing
您可能尝试在 for 循环内渲染 hand
图像,但是,该循环使用一个具有相同名称 (hand
) 的局部变量,在此case 是 LeapMotion Hand
实例,而不是 PImage
。
for 循环变量(Hand
实例)正在隐藏全局变量(PImage
变量)。
发生错误的原因是它尝试渲染 LeapMotion Hand
而不是您的 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);
}
}
执行此操作的方法有很多,这取决于什么对您的项目的用户体验有意义。
关于java - 初学者编码器 : Java (Processing) and Leap Motion Controller Tutorials?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64238897/