Image generating code hangs up

edited August 2016 in Library Questions

Hi all, i have the following code which seems to me alright:

import processing.pdf.*;
import processing.serial.*;


Serial myPort;  // Create object from Serial class

int layers=24;

int switches;

byte[] temp = new byte[4];


PImage[] source = new PImage[layers+1];      // Source image
PImage destination;  // Destination image

void setup() {
  size(732, 732);

  myPort = new Serial(this, "/dev/tty.usbmodem621", 9600);


for(int n=0;n<layers+1;n++){
  source[n] = loadImage("layer"+n+".jpg"); 
} 
  // The destination image is created as a blank image the same size as the source.
  destination = createImage(source[0].width, source[0].height, RGB);


  set();
}

void draw() {  
  background(255);

  if ( myPort.available() > 0) {  // If data is available,

    switches = Integer.parseInt(myPort.readString());
    println(switches);
    if(switches==-9294){
    beginRecord(PDF,"output.pdf");
    destination=source[24];
    image(destination,0,0);
    set();
    print();
    }else{
        loop();
    beginRecord(PDF,"output.pdf");
    display();
    textSize(26);
    fill(0);
    text(switches,width-150,height-30);
    print();
    }
  }  
  /*switches=55;*/  
  display();

}

void set(){

destination.loadPixels();
  for (int x = 0; x < destination.width; x++) {
    for (int y = 0; y < destination.height; y++ ) {
      int loc = x + y*source[0].width;
      // Test the brightness against the threshold      
        destination.pixels[loc]  = color(255);  // White
    }
  }

}

void createimg(int n){

float threshold = 127;

  // We are going to look at both image's pixels
  source[n].loadPixels();
  destination.loadPixels();

  for (int x = 0; x < source[n].width; x++) {
    for (int y = 0; y < source[n].height; y++ ) {
      int loc = x + y*source[n].width;
      // Test the brightness against the threshold
      if (brightness(source[n].pixels[loc]) < threshold) {
        destination.pixels[loc]  = color(0);
      }
    }
  }

  // We changed the pixels in destination
  destination.updatePixels();

}

void display(){

  // Display the destination
  image(destination,0,0);
  set();
}

void loop(){

  for(int i = 0; i < layers; i++)
  {
    if(isBitOn(i, switches) && (switches != 0))
    {
    createimg(i);
    }
  }
}

boolean isBitOn(int bit, long value)
{
  long mask = 1;
  mask = mask << bit;
  if((value & mask)>0){
    return true;
  }else{
    return false;
  }
}

void print(){
endRecord();
saveFrame("img/test/bild-######.png");
open("printpdf.app");
}

When it stops, the error is "NumberFormatEcxeption: For input string:"xxxxx". Does anybody find the problem? Thanks for the help!

Answers

  • edited August 2016

    Set and loop and print are already existing method names within processing. I would choose different names.

    I'd move the if else block that starts on line 40 to a separate method that took a single parameter. Then I could unit test that bit of the code without needing the serial connection, make sure it works. If everything was fine with that then I'd look at the serial connection itself.

    What does the debug say? What happens when it stops?

    It's bad practice to have the end record in a method when the begin record isn't.

  • When it stops, the error is

    "NumberFormatEcxeption: For input string:"xxxxx"

    Maybe it's because I read the Integer with an Arduino as long, not as int and send it via serial to the script?

  • I'd move the if else block that starts on line 40 to a separate method that took a single parameter. Then I could unit test that bit of the code without needing the serial connection, make sure it works.

    How do i exactly do that? I'm sorry, the code was written together with friends – maybe you could give me an example of how it could look?

    Thank you very much!

  • When it stops, the error is "NumberFormatEcxeption: For input string:"xxxxx"

    This is the kind of info that needs to go in the first post, don't wait until someone asks you for it.

    This points to it being, as you say, a problem with the comms from the arduino. But I can't help you there as I don't have one.

  • Alright, thanks! But maybe you could help me in structure the Processing code right?

  • well, the serial error is the main problem, i guess. the rest is less important.

    but change the names of those methods because it's confusing otherwise.

    and use ctrl-t in the editor to indent the code properly.

Sign In or Register to comment.