java - 初学者编码器: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();
}
}
解决方案
迪特里希的回答是正确的 (+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);
}
}
有很多方法可以做到这一点,这取决于对您的项目的用户体验有意义的方式。
推荐阅读
- kotlin - Kotlin/Native 中的 ${type}Var 是什么?
- java - 从独立客户端查找远程 EJB 时出现 AssertionError“上下文可能不为空”
- java - 在 Flyway 中使用 Gradle 集成与 Spring Boot 集成的优缺点是什么?
- c++ - C ++中的循环算法
- reactjs - 将表单发布到 reactJS 应用程序并获取表单数据
- c# - 使用 MS Edge 驱动程序进行 C# Selenium 测试
- python - 计算数据框列中的位数
- hyperledger-fabric - Hyperledger Fabric:注册检查失败:Idemix 注册信息不存在
- oracle - 错误没有可用的主数据块
- r - 从分组点创建不重叠的多边形