Save Images with Java
Some steps are to do:
- Locate the right ImageWriter
- (optional) Create an image
- Save the image
Locate the right ImageWriter
An ImageWriter can found with static method ImageIO.getImageWritersByFormatName (String extension). If we need an ImageWriter to save an JPEG image with Java we tell as extension "JPEG". As result we get all ImageWriter for this image format.
If an user select the format we need a method to get all ImageWriters - ImageIO.getWriterFormatNames(). A source code can be like this:
ArrayList<Action> actions = new ArrayList<Action> ();
ImageWriter icoWriter = null;
for (String name: ImageIO.getWriterFormatNames()) {
boolean add = true;
// Check for registered Action in our collection for (Action a : actions) {
final Class c1 = ImageIO.getImageWritersByFormatName(name).next().getClass();
final Class c2 = ImageIO.getImageWritersByFormatName((String)a.getValue(Action.NAME)).next().getClass();
if (c1.equals(c2)) {
add = false;
break;
}
}
if (add) { // If new Writer, add him...
this.addWriter(actions, name);
}
}
Create an image
One way to create an java.awt.Image are to paint for example on a BufferedImage over the Graphics object. Later we see how I this in my application SVG Viewer do. Please check before how many color can use - here some lesser for WBMP file format.
Save the image
It is easy to save an image with an ImageWriter. We give our ImageWriter object method setOutput() an OutputStream. Here for java.awt.Image is the MemoryCacheImageOutputStream the right choice.
Now back to the source - the method addWriter. Please check: The method addWriter are defined in class MainFrame; grafik is instance of SVGPanel.
protected void addWriter (final Collection forAdd, final String extensionToAdd) {
Action nAction = new ExtendedAction ("~"+extensionToAdd.toUpperCase()) {
public void actionPerformed (final ActionEvent e) {
final String extension = "."+this.getValue(Action.NAME);
JFileChooser jfc = new JFileChooser ();
jfc.setLocale (Locale.getDefault ());
jfc.updateUI ();
jfc.setCurrentDirectory (new File ());
switch (jfc.showSaveDialog (new JFrame())) {
case JFileChooser.APPROVE_OPTION:
try {
String name = jfc.getSelectedFile ().toString ();
if (!name.toLowerCase ().endsWith (extension)) {
name += extension;
}
final String ext = (String)this.getValue (Action.NAME);
BufferedImage bi = null;
if ("WBMP".equals (this.getValue (Action.NAME))) {
bi = new BufferedImage (MainFrame.this.grafik.getWidth (),
MainFrame.this.grafik.getHeight (),
BufferedImage.TYPE_BYTE_BINARY);
}
else {
bi = new BufferedImage (MainFrame.this.grafik.getWidth (),
MainFrame.this.grafik.getHeight (),
BufferedImage.TYPE_INT_RGB);
}
MainFrame.this.grafik.getDoc ().paint (new Component () {}, (Graphics2D) bi.getGraphics ());
writer.setOutput (new MemoryCacheImageOutputStream (new FileOutputStream (name)));
writer.write (bi);
bi.getGraphics ().dispose ();
writer.dispose ();
}
catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog (new JFrame(),
ex.getLocalizedMessage (),
ex.getClass ().getName (),
JOptionPane.ERROR_MESSAGE);
}
catch (IOException ex) {
JOptionPane.showMessageDialog (new JFrame(),
ex.getLocalizedMessage (),
ex.getClass ().getName (),
JOptionPane.ERROR_MESSAGE);
}
}
}
};
}
Thats all folks...

