Hello,
I use the following code to send an image from my C328 camera to Processing.
I get the following error: nullpointerexeption
I use the following Processing code:
Code:
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;
byte[] Photo = {
};
void setup()
{
//Set and open serial port
println( Serial.list() );
myPort = new Serial( this, Serial.list()[1], 9600 );
myPort.clear();
}
void draw()
{
// Import Picture
byte[] buffer = new byte[64];
while( myPort.available() > 0 )
{
int readBytes = myPort.readBytes(buffer);
//print( "Read " );
//print( readBytes );
//println( " bytes ..." );
for( int i = 0; i < readBytes; i++ )
{
TotalBytes = TotalBytes + 1;
//print( "Read " );
//print( TotalBytes );
//println( " bytes in total ..." );
Photo = append( Photo, buffer[i] );
print( "PhotoArray contains: " );
print(Photo.length);
println( " Elements..." );
if ( TotalBytes > 2 && Photo[TotalBytes-3] == CharE && Photo[TotalBytes-2] == CharO && Photo[TotalBytes-1] == CharF ){
// Save picture to file
if( Photo.length > 0 ) {
print( "Writing to disk " );
print( Photo.length );
println( " bytes ..." );
saveBytes( ImageFilename, Photo );
println( "DONE!" );
// Open Picture
size(6400, 4800);
PImage C328Image;
C328Image = loadImage(ImageFilename);
image(C328Image, 0, 0);
//Clear the array
Photo = null; // clear array
//Clear totalbytes
TotalBytes = 0;
//Flush serial port
myPort.clear();
return;
}
}
}
}
}
The error is executed on this line when a new picture is imported (2nd cycle):
Code:
Photo = append( Photo, buffer[i] );
Probably the problem is due to the fact that I try to clear the array using this line of code:
Code:
Photo = null; // clear array ????
Is this the problem? If yes, is there another way to clear/delete the array. Or is there something else wrong.....
Thanks in advance,
Cheers.