I have the following sketch written in Processing, which scans an image, and stores the coordinates of each black pixel into two ArrayLists - xList, and yList. It currently then prints out the coordinates, but now I need to send these ArrayLists to an Arduino, and have the Arduino store it in its own two arrays (or one 2D array). The problem I'm running into is not being able to tell the Arduino when the data from xList ends, and the data from yList begins, as there isn't a predefined number of elements in the 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));
}
}