Home arrow static arrow Java Programming [Archive] - Creating .gif or .jpeg
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Creating wiki/./.gif or .jpeg
5 Duke Stars available
This topic has 3 replies on 1 page.

Posts:1
Registered: 8/2/04
Creating wiki/./.gif or .jpeg  
Aug 2, 2004 9:17 PM



 
I am making a web based application which should have some dynamicly created pictures. Preferably these pictures should either be wiki/./.gif or .jpeg as they are meant to be viewed from a regular browser. I would like to use some API which will allow me to do something along the lines of telling it which pixels to make wich color.

If anyone knows anything like this that will be great, thanks.

 

Posts:150
Registered: 22/07/04
Re: Creating wiki/./.gif or .jpeg  
Aug 2, 2004 9:59 PM (reply 1 of 3)



 
You might find this page handy:
http://www.geocities.com/marcoschmidt.geo/java-image-coding.html
 

Posts:14,142
Registered: 99-04-02
Re: Creating wiki/./.gif or .jpeg  
Aug 3, 2004 7:13 AM (reply 2 of 3)



 
Here's some code... If you have the wiki/./Acme.JPM.Encoders.GifEncoder in your classpath (you can find that listed in the page the other person mentioned), you can encode Gifs with it with this code. Java's Image I/O package doesn't support GIF at this time.
	/**	 * Writes the specified image to the specified file in the specified 	 * format.  For GIF files, if the format is not supported by the 	 * standard Image I/O classes, the class 	 * wiki/./<code>Acme.JPM.Encoders.GifEncoder</code> is used, if it is in the 	 * classpath.  	 * 	 * @param  img     the image object to write	 * @param  file    the file to write to	 * @param  format  image format to save as	 * @throws  IOException  on I/O errors	 */	public static void writeImage(BufferedImage img, File file, String format) 			throws IOException {		FileOutputStream out = new FileOutputStream(file);		writeImage(img, out, format);		out.flush();		out.close();	} 	/**	 * Writes the specified image to the specified file in the specified 	 * format.  For GIF files, if the format is not supported by the 	 * standard Image I/O classes, the class 	 * wiki/./<code>Acme.JPM.Encoders.GifEncoder</code> is used, if it is in the 	 * classpath.  	 * 	 * @param  img     the image object to write	 * @param  out     the output stream to write to	 * @param  format  image format to save as	 * @throws  IOException  on I/O errors	 */	public static void writeImage(BufferedImage img, OutputStream out, String format) 			throws IOException {		format = format.toLowerCase();		Iterator writers = ImageIO.getImageWritersByFormatName(format);		if(writers == null || !writers.hasNext()) {			// if GIF isn't supported by Java, try using the 			// wiki/./Acme.JPM.Encoders.GifEncoder encoder, if it exists...			if(GIF.equalsIgnoreCase(format)) {				// try dynamically loading the GifEncoder so we don't 				// need to have it at compile time.  if not available 				// at runtime, then GIF won't be supported.  				try {					Class c = Class.forName("Acme.JPM.Encoders.GifEncoder");					Class[] paramTypes = { Image.class, OutputStream.class, Boolean.TYPE };					java.lang.reflect.Constructor con = c.getConstructor(paramTypes);					Object[] params = { img, out, new Boolean(true) };					Object gifEncoder = con.newInstance(params);					java.lang.reflect.Method meth = c.getMethod("encode", null);					Object ret = meth.invoke(gifEncoder, null);					out.flush();					return;				// ignore exceptions and just drop thru to the 				// unsupported format exception below				} catch(ClassNotFoundException cnfe) {				} catch(IOException ioe) {				} catch(Exception e) {				}			}			throw new IllegalArgumentException("Unsupported format:  " + format + "");		} 		ImageWriter writer = (ImageWriter)writers.next();		IIOImage iioImg = new IIOImage(img, null, null);		ImageWriteParam iwparam = writer.getDefaultWriteParam();		// for JPEGs... set image quality parameters		if(JPEG.equals(format)) {			iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);			iwparam.setCompressionQuality(1.0f);		}		writer.setOutput(ImageIO.createImageOutputStream(out));		writer.write(null, iioImg, iwparam);	}
 

Posts:10,972
Registered: 10/23/03
Re: Creating wiki/./.gif or .jpeg  
Aug 3, 2004 7:17 AM (reply 3 of 3)



 
jpeg? You're larfin':

[url=http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html#write(java.awt.image.RenderedImage,%20java.lang.String,%20java.io.File)]ImageIO.write[/url](image, "jpeg", file);
 
This topic has 3 replies on 1 page.