/* Copied from http://processing.org/discourse/yabb2/YaBB.pl?num=1271281000 REQUESTS 1. Add Text field to enter filename to read in 2. Test for JPG and TXT filenames 3. Add camera commands via serial */ /* Then modified by Adam Harvey Jan 08, 2011 */ import processing.serial.*; Serial myPort; int i = 1; String ImageFilename = "Photo_" + year() + "_" + day() + "_" + month() + "-" + hour() + "_" + minute() + "_" + second() + "_" + millis() + ".jpg"; int CharE = 69; int CharO = 79; int CharF = 70; int TotalBytes = 0; Boolean imageLoading = false; Boolean imageReceived = false; long startTime; PImage cameraImg,loadingImg; byte[] Photo = { }; void setup() { size(320,240); //Set and open serial port myPort = new Serial( this, Serial.list()[0], 115200 ); myPort.clear(); loadingImg = loadImage("loading.jpg"); } void draw() { // Import Picture if(imageLoading) { loadPhoto(); // must be polled every time image(loadingImg,0,0); } else if(imageReceived) { image(cameraImg, 0, 0); } } void loadPhoto() { byte[] buffer = new byte[64]; while( myPort.available() > 0 ) { int readBytes = myPort.readBytes(buffer); for( int i = 0; i < readBytes; i++ ) { TotalBytes = TotalBytes + 1; Photo = append( Photo, buffer[i] ); if ( TotalBytes > 2 && Photo[TotalBytes-3] == CharE && Photo[TotalBytes-2] == CharO && Photo[TotalBytes-1] == CharF ) { // Save picture to file if( Photo.length > 10 && !imageReceived ) { print( "Writing to disk " ); print( Photo.length ); println( " bytes ..." ); saveBytes( ImageFilename, Photo ); println( "DONE!" ); println("Time: " + ((millis() - startTime)/1000.0) + "s"); // Open Picture cameraImg = loadImage(ImageFilename); imageReceived = true; imageLoading = false; //Clear the array Photo = null; // clear array //Clear totalbytes TotalBytes = 0; //Flush serial port myPort.clear(); return; } } } } } void initiatePhotoTransfer() { startTime = millis(); imageReceived = false; imageLoading = true; Photo = new byte[0]; TotalBytes = 0; println("send S"); myPort.write('S'); } void captureAndSend() { startTime = millis(); imageReceived = false; imageLoading = true; Photo = new byte[0]; TotalBytes = 0; println("send T"); myPort.write('T'); } void keyPressed() { if( key == 'S' && imageLoading == false) { initiatePhotoTransfer(); } else if(key == 'T') captureAndSend(); }