Re: How do I write an ImageIcon object into a file on my computer?
I got it!!!
/*
* ImageDownloader.java
*
* Created on January 17, 2007, 9:22 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ImageTools;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.imageio.*;
import java.awt.image.*;
import FileTools.FileDownloader;
import CollectionTools.ArrayFunctionality;
import javax.imageio.stream.FileImageOutputStream;
/**
*
* @author ppowell-c
*/
public class ImageDownloader extends FileDownloader implements
ImageDownloadable {
private static String[] mimeArray;
//------------------------- --* GETTER/SETTER METHODS *--
----------------
private static String[] getMimeArray() {
return mimeArray;
}
private static void setMimeArray(String[] myMimeArray) {
mimeArray = myMimeArray;
}
//---------------------------- --* MAIN METHODS *--
-----------------------
public void downloadImage() {}
public static void downloadImage(BufferedImage bi, File file)
throws IOException, Exception {
String mimeType = ImageMIMERetriever.retrieveMIME(file);
if (getMimeArray() == null)
setMimeArray(ImageIO.getReaderMIMETypes());
if (!hasAppropriateMIME(mimeType, getMimeArray()) &&
System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0
) {
mimeType = "image/vnd.microsoft.icon"; // ADD ICON TYPE FOR
WINDOWS
setMimeArray((String[])ArrayFunctionality.add(getMimeArray(),
new String[] {"image/vnd.microsoft.icon"}));
}
downloadImage(bi, file, mimeType);
}
public static void downloadImage(BufferedImage bi, File file,
String mimeType)
throws IOException, Exception {
if (getMimeArray() == null)
setMimeArray(ImageIO.getReaderMIMETypes());
if (hasAppropriateMIME(mimeType, getMimeArray())) {
Iterator<ImageWriter> iter =
ImageIO.getImageWritersByMIMEType(mimeType);
if (iter.hasNext()) {
ImageWriter writer = iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionType("BI_RGB");
FileImageOutputStream fios = new
FileImageOutputStream(file);
writer.setOutput(fios);
IIOImage image = new IIOImage(bi, null, null);
writer.write(null, image, iwp);
writer.dispose();
} else {
throw new Exception("No appropriate ImageWriter was
available");
}
} else {
throw new Exception("Your mime type: " + mimeType + " is
inappropriate");
}
}
protected static boolean hasAppropriateMIME(String mimeType,
String[] mimeArray) {
try {
for (int i = 0; i < mimeArray.length; i++) {
if (mimeArray[i].equals(mimeType)) return true;
}
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
return false;
}
}
And I call it like this:
/**
* Download the icon to a ".ico" icon
*
* @see ImageDownloader
*/
public void downloadIcon() {
try {
if (getOrigIconFilePath() == null) generateIconFilePath();
ImageDownloader.downloadImage(
toBufferedImage(getIcon()),
new File(getOrigIconFilePath()),
"image/bmp");
} catch (Exception e) {
System.out.println("Error trying to save icon to file: " +
e.getMessage());
}
}
And it works! I get a perfect 16x16 icon for Windows [can't predict if
it will work within *nix)
Phil
phillip.s.powell@gmail.com wrote:
RedGrittyBrick wrote:
Knute Johnson wrote:
Maybe you need to explain it to someone with ADD as if I were a 10-year
old. Try it that way.
How do you write to a new file anything, like String content or Image
content or Excel spreadsheet content.. anything at all, into a new
file? How do you do it?
Phil
String data is written with a Writer, Object data is written with an
ObjectOutputStream and byte data is written with an OutputStream.
Where is your data coming from?
The web?
A file on a disk?
A Java program?
Is it a byte stream?
I shouldn't really jump in and answer the question for the OP but there
seems to be an impedance mismatch in the communications channel.
Oh please speak English!
AIUI the OP wants
a) Use HTTP to obtain an image from a website somewhere.
e.g. http://foo.example.com/img/frog.jpg
b) Write the obtained data to a local disk file in such a way that it
constitutes a valid image file that can be read by common image viewing
and editing software. e.g. to c:\frog.jpg as a valid JPEG image file.
No I'm sorry that is not it. This is what I want to do.
I have an application, IconMaker.java. Its function is to take an
image from HTTP and display it within my JPanel as an ImageIcon type,
that is, an "icon". It's not really an icon as Windows understands it,
and furthermore, it is not a real file on the system anywhere, so I
want to take that ImageIcon and somehow "magically" convert it into an
actual Windows .ico Icon.
This method in IconMaker creates the ImageIcon:
/**
* Returns an ImageIcon, or null if the path was invalid
*
* @param urlPath the String url path
* @return a new ImageIcon object or null
* @see ImageIcon
* @link
http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/32d030195835a490/5ff39713bea3dc71?lnk=st&q=how+to+resize+an+ImageIcon+java&rnum=1&hl=en#5ff39713bea3dc71
*/
protected ImageIcon createImageIcon(String urlPath) {
try {
setURL(new URL(urlPath));
ImageIcon icon = new ImageIcon(this.getURL(), "Your new
icon");
// SIMPLE VERSION OF RESIZING
icon.setImage(icon.getImage().getScaledInstance(16, 16,
Image.SCALE_AREA_AVERAGING));
return icon;
} catch (Exception e) {
System.out.println("Unable to create icon:" +
e.getMessage());
return null;
}
}
I don't think he cares how the data is temporarily represented in Java.
Followups set to comp.lang.java.help only (dropping .programmer)