How to load an image in a Java console application? (without applet)

I am creating a Java console application that outputs a series of image files, and I want to draw an image file as part of the output. getImage doesn't seem to work, it needs Toolkits or something like that.

Image cover = getImage("cover.png");

Any ideas?

Edit: the program does not display images, it generates them and saves them in a series of files. I figured out how to save images, and draw basic geometry, but not for any reason.

+3
source share
3 answers

Another way to work with various graphic formats is the ImageIO class. The following example converts jpg to png and draws a cross.

public class ImageReaderExample {

    public static void main(String[] args) {
     try{
          BufferedImage image = ImageIO.read(new File("/tmp/input.jpg"));

          image.getGraphics().drawLine(1, 1, image.getWidth()-1, image.getHeight()-1);
          image.getGraphics().drawLine(1, image.getHeight()-1, image.getWidth()-1, 1);

          ImageIO.write(image, "png", new File("/tmp/output.png"));
     }
     catch (IOException e){
         e.printStackTrace();
     }
    }
}
+4
source

, awt-, awt, , java.awt.headless. , awt :

System.setProperty("java.awt.headless", "true"); 

:

java -Djava.awt.headless=true Program
+1

Where do you want to draw it? Seeing that you need some kind of output, this may help, assuming bmps (but other formats are explained):

http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html

 // Define the source and destination file names.
 String inputFile = /images/FarmHouse.tif
 String outputFile = /images/FarmHouse.bmp

 // Load the input image.
 RenderedOp src = JAI.create("fileload", inputFile);

 // Encode the file as a BMP image.
 FileOutputStream stream =
     new FileOutputStream(outputFile);
 JAI.create("encode", src, stream, BMP, null);

 // Store the image in the BMP format.
 JAI.create("filestore", src, outputFile, BMP, null);

Reads and writes a bmp file.

0
source

All Articles