我在Processing中编写了以下草图,它扫描图像,并将每个黑色像素的坐标存储到两个 ArrayLists - xList 和 yList 中。它目前会打印出坐标,但现在我需要将这些 ArrayLists 发送到 Arduino,并让 Arduino 将其存储在自己的两个数组(或一个 2D 数组)中。我遇到的问题是无法告诉 Arduino xList 中的数据何时结束,yList 中的数据何时开始,因为 ArrayLists 中没有预定义数量的元素。
import javax.swing.*;
PImage img;
//Threshold determines what is considered a "black" pixel
//based on a 0-255 grayscale.
int threshold = 50;
void setup() {
// File opener
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
if (file.getName().endsWith("jpg")) {
img = loadImage(file.getPath());
if (img != null) {
size(img.width,img.height);
image(img,0,0);
}
} else {
println("Please select a .jpg image file.");
}
}
//I added noLoop() here, because the program would output
//the coordinates continuously until it was stopped.
noLoop();
}
void draw() {
//Here it scans through each pixel of the image.
ArrayList xList = new ArrayList();
ArrayList yList = new ArrayList();
for (int ix = 0; ix < img.width; ix++) {
for (int iy = 0; iy < img.height; iy++) {
//If the brightness of the pixel at (ix,iy) is less than
//the threshold
if (brightness(get(ix,iy)) < threshold) {
//Add ix, and iy to their respective ArrayList.
xList.add(ix);
yList.add(iy);
}
}
}
//Print out each coordinate
//For this statement, I use xList.size() as the limiting
//condition, because xList and yList should be the same size.
for (int i=0; i<xList.size(); i++) {
println("X:" + xList.get(i) + " Y:" + yList.get(i));
}
}