我是Java的新手程序员。我目前正在进行基于网络摄像头的自动化。
项目详情: Webcam在基于Linux的计算机上运行。这个网络摄像头将通过TCP / IP向我发送图片。我得到一个组件的图片,并将其显示在窗口的一半。我需要在窗口的另一半打开一个相同组件的autocad文件。我将旋转3d文件,使其与照片视图匹配。然后,我需要在照片上追踪一些点并将这些点发送回Linux机器。
流程: 我正在使用Processing IDE建立连接并获取文件。下载文件后,它将显示在窗口的一半上。然后,mouseClicked事件将记录图像上的点并将其发送回Linux机器。
到目前为止完成的工作: 已建立到telnet会话的TCP / IP连接以进行模拟。 图像显示在屏幕上。 实现mouseClicked事件,并将点击的点返回到telnet会话。
这是我的代码。
import controlP5.*;
import processing.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
ControlP5 controlP5;
ControlFont font;
Textarea myTextarea;
Textfield portTextfield;
Textfield bufSending;
String path;
PImage img;
float posX1;
float posY1;
float posX2;
float posY2;
float posX3;
float posY3;
float posX4;
float posY4;
int clicks=0;
int flag = 0;
String finall;
int portnumber = 10002;
Server myServer;
boolean myServerRunning = false;
String buf="";
controlP5.Button but1,but2,but3,but4,but5;
void setup() {
size(displayWidth, displayHeight);
smooth();
frameRate(30);
background(0);
controlP5 = new ControlP5(this);
portTextfield = controlP5.addTextfield("port",100,10,100,20);
portTextfield.setText(str(portnumber));
but1 = controlP5.addButton("OpenSocket",1,220,10,60,20);
but2 = controlP5.addButton("CloseSocket",2,290,10,60,20);
bufSending = controlP5.addTextfield("buf",100,50,100,20);
bufSending.setFocus(true);
but3 = controlP5.addButton("SendBuffer",3,220,50,60,20);
but4 = controlP5.addButton("Clear",4,290,50,60,20);
but5 = controlP5.addButton("GetImage",5,360,50,60,20);
}
void draw() {
}
void mouseClicked() {
if (flag == 1) {
clicks++;
if (clicks == 2) {
ellipse(mouseX,mouseY,5,5);
posX1 = mouseX;
posY1 = mouseY;
println("click1\n");
}
if (clicks == 3) {
ellipse(mouseX,mouseY,5,5);
posX2 = mouseX;
posY2 = mouseY;
println("click2\n");
}
if (clicks == 4) {
ellipse(mouseX,mouseY,5,5);
posX3 = mouseX;
posY3 = mouseY;
println("click3\n");
}
if (clicks == 5) {
ellipse(mouseX,mouseY,5,5);
posX4 = mouseX;
posY4 = mouseY;
println("click4\n");
}
if (clicks == 6) {
finall = posX1+" "+posY1+" "+posX2+" "+posY2+" "+posX3+" "+posY3+" "+posX4+" "+posY4;
bufSending.setText(finall);
myServer.write(bufSending.getText());
flag = 0;
}
}
}
public void OpenSocket(int theValue) {
if (!myServerRunning) {
but1.setColorBackground(color(51,51,51));
but2.setColorBackground(color(0,54,82));
myServer = new Server(this, portnumber);
myServerRunning = true;
}
}
public void CloseSocket(int theValue) {
if (myServerRunning) {
but2.setColorBackground(color(51,51,51));
but1.setColorBackground(color(0,54,82));
myServerRunning = false;
myServer.stop();
}
}
public void SendBuffer(int theValue) {
if (myServerRunning) {
myServer.write(bufSending.getText());
//println(bufSending.getText());
}
}
public void Clear(int theValue) {
bufSending.setText("");
myTextarea.setText("");
buf="";
}
public void GetImage(int theValue) {
if (myServerRunning) {
but5.setColorBackground(color(51,51,51));
delay(100);
but5.setColorBackground(color(0,54,82));
loadFile(new Frame(), "open your favorite file", "/Users/myName/Desktop/", "");
}
}
public void loadFile (Frame f, String title, String defDir, String fileType) {
FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
fd.setFile(fileType);
fd.setDirectory(defDir);
fd.setLocation(50, 50);
fd.show();
path = fd.getDirectory()+fd.getFile();
img = loadImage(path);
image(img,100,100,img.width/2, img.height/2);
flag = 1;
}
我已经尝试过很多其他想法,比如JFrames,但对我来说并不适用。这是脚本。
package processJava;
import processing.core.*;
import controlP5.*;
import processing.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CircleSketch extends PApplet {
ControlP5 controlP5;
ControlFont font;
Textarea myTextarea;
Textfield portTextfield;
Textfield bufSending;
String path;
PImage img;
int portnumber = 10002;
Server myServer;
boolean myServerRunning = false;
String buf="";
controlP5.Button but1,but2,but3,but4,but5;
public void setup() {
size(displayWidth, displayHeight);
smooth();
frameRate(30);
background(0);
controlP5 = new ControlP5(this);
portTextfield = controlP5.addTextfield("port",100,40,100,20);
portTextfield.setText(str(portnumber));
but1 = controlP5.addButton("OpenSocket",1,220,40,60,20);
but2 = controlP5.addButton("CloseSocket",2,290,40,60,20);
bufSending = controlP5.addTextfield("buf",100,100,100,20);
bufSending.setFocus(true);
but3 = controlP5.addButton("SendBuffer",3,220,100,60,20);
but4 = controlP5.addButton("Clear",4,290,100,60,20);
but5 = controlP5.addButton("GetImage",5,360,100,60,20);
}
public void draw() {
}
public void OpenSocket(int theValue) {
if (!myServerRunning) {
but1.setColorBackground(color(51,51,51));
but2.setColorBackground(color(0,54,82));
myServer = new Server(this, portnumber);
myServerRunning = true;
}
}
public void CloseSocket(int theValue) {
if (myServerRunning) {
but2.setColorBackground(color(51,51,51));
but1.setColorBackground(color(0,54,82));
myServerRunning = false;
myServer.stop();
}
}
public void SendBuffer(int theValue) {
if (myServerRunning) {
myServer.write(bufSending.getText());
//println(bufSending.getText());
}
}
public void Clear(int theValue) {
bufSending.setText("");
myTextarea.setText("");
buf="";
// startRunning();
}
public void GetImage(int theValue) {
if (myServerRunning) {
but5.setColorBackground(color(51,51,51));
delay(100);
but5.setColorBackground(color(0,54,82));
loadFile(new Frame(), "open your favorite file", "/Users/myName/Desktop/", "");
}
}
/*
void keyReleased(){
if(key == 'o') println( loadFile(new Frame(), "open your favorite file", "/Users/myName/Desktop/", "") );
if(key == 's') println( saveFile(new Frame(), "save your great work", "", "") );
}
*/
public void loadFile (Frame f, String title, String defDir, String fileType) {
FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
fd.setFile(fileType);
fd.setDirectory(defDir);
fd.setLocation(50, 50);
fd.show();
path = fd.getDirectory()+fd.getFile();
// return path;
img = loadImage(path);
image(img,100,100,img.width/2, img.height/2);
}
}
我能够达到拆分窗口布局,但我不确定如何将基于处理的小程序放在左框架上,将AutoCAD.exe放在右框架中。 任何帮助都非常感谢。