Lejos NXT: draw line and save image

227 Views Asked by At

I've managed to make a LineFollower program; added the feature of "memorizing" the path the robot just followed. The next step is to draw that path and save the image file in the brick and read it in the PC with the NxjBrowse. I thought I'd try using the classic java method, with BufferedImage and saving with ImageIO, but it didn't work and it kept giving me Java Heap Space: My previous question

After that, I've made some research and found that there's a class called javax.microedition.lcdui.Image, so I've created an Image object and used GetGraphics and tried to draw on it; and save it using FileOutputStream, here's my code:

Image img = Image.createImage(300, 300);

Graphics g = img.getGraphics();
g.drawLine(0, 0,  100, 200);

File monFichier = new File("Image2.png");
FileOutputStream fOut = null;

try {
    fOut = new FileOutputStream(monFichier);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
byte[] b = img.getData();
try {
    fOut.write(b);
    fOut.close();

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

The problem is that it writes on the file which is not recognized as an image, when I connect to my PC; I can't open the created file (checked the size, not empty). I don't know if the saving is wrong or the method i'm using to draw is wrong. Short version of the question: How to draw lines with Lejos and save the resultat as an image file? Thank you.

UPDATE:

I used an ImageOutputStream instead of FileOutputStream; and now it's giving me "Java Heap Space" error; after it got stuck in "linking" for a while.

Java Heap Space
Java.lang.OutOfMemoryError
1

There are 1 best solutions below

1
user6067554 On

Image.getData() is an access to the underlying DataBuffer and not a valid PNG or BMP image. Try ImageIO.write(img, "png", outputfile).