I got this to work using both the JPEGEncoder classes, as well as ImageIO.
Man, it's several of orders of magnitude-more-work to do it with ImageIO. If
anyone has any comments about how I implemented it using ImageIO please feel
free to chime in!!
Thanks to everyone who helped me out.
Rob
-------------
Here's the easy solution:
public void saveBufferedImageAsJPEG(BufferedImage bi, int dpi, String
fileName)
{
// save image as Jpeg
FileOutputStream out = null;
try
{
out = new FileOutputStream(fileName+".jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1f, false);
param.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
param.setXDensity(dpi);
param.setYDensity(dpi);
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (IOException io)
{
System.out.println(io);
}
}
catch (FileNotFoundException fnf)
{
System.out.println("File Not Found");
}
} //end public void saveBufferedImageAsJPEG(int dpi, String
fileName)
----------------------------------------------------------
Of course, this isn't portable, that's the downside.
Here's the ImageIO method:
-----------------------------------------------------------
// Here is a function to save a jpeg using ImageIO:
public static void saveImageToDisk(BufferedImage bi, int dpi, String
fileName)
{
Iterator i = ImageIO.getImageWritersByFormatName("jpeg");
//are there any jpeg encoders available?
if (i.hasNext()) //there's at least one ImageWriter, just use the
first one
{
ImageWriter imageWriter = (ImageWriter) i.next();
//get the param
ImageWriteParam param = imageWriter.getDefaultWriteParam();
ImageTypeSpecifier its = new
ImageTypeSpecifier(bi.getColorModel(), bi.getSampleModel());
//get metadata
IIOMetadata iomd = imageWriter.getDefaultImageMetadata(its,
param);
String formatName = "javax_imageio_jpeg_image_1.0";//this is the
DOCTYPE of the metadata we need
Node node = iomd.getAsTree(formatName);
//what are child nodes?
NodeList nl = node.getChildNodes();
for (int j = 0; j < nl.getLength(); j++)
{
Node n = nl.item(j);
System.out.println("node from IOMetadata is : " +
n.getNodeName());
if (n.getNodeName().equals("JPEGvariety"))
{
NodeList childNodes = n.getChildNodes();
for (int k = 0; k < childNodes.getLength(); k++)
{
System.out.println("node #" + k + " is " +
childNodes.item(k).getNodeName());
if
(childNodes.item(k).getNodeName().equals("app0JFIF"))
{
NamedNodeMap nnm =
childNodes.item(k).getAttributes();
//get the resUnits, Xdensity, and Ydensity
attribuutes
Node resUnitsNode =
getAttributeByName(childNodes.item(k), "resUnits");
Node XdensityNode =
getAttributeByName(childNodes.item(k), "Xdensity");
Node YdensityNode =
getAttributeByName(childNodes.item(k), "Ydensity");
//reset values for nodes
resUnitsNode.setNodeValue("1"); //indicate DPI
mode
XdensityNode.setNodeValue(String.valueOf(dpi));
YdensityNode.setNodeValue(String.valueOf(dpi));
System.out.println("name=" +
resUnitsNode.getNodeName() + ", value=" + resUnitsNode.getNodeValue());
System.out.println("name=" +
XdensityNode.getNodeName() + ", value=" + XdensityNode.getNodeValue());
System.out.println("name=" +
YdensityNode.getNodeName() + ", value=" + YdensityNode.getNodeValue());
} //end if
(childNodes.item(k).getNodeName().equals("app0JFIF"))
} //end if (n.getNodeName().equals("JPEGvariety")
break; //we don't care about the rest of the children
} //end if (n.getNodeName().equals("JPEGvariety"))
} //end for (int j = 0; j < nl.getLength(); j++)
try
{
iomd.setFromTree(formatName,node);
}
catch (IIOInvalidTreeException e)
{
e.printStackTrace(); //To change body of catch statement
use Options | File Templates.
}
//attach the metadata to an image
IIOImage iioimage = new IIOImage(bi, null, iomd);
try
{
imageWriter.setOutput(new FileImageOutputStream(new
File(fileName+".jpg")));
imageWriter.write(iioimage);
}
catch (IOException e)
{
e.printStackTrace();
}
} //end if (i.hasNext()) //there's at least one ImageWriter, just
use the first one
} //end public static void saveImageToDisk(BufferedImage bi, int dpi,
String fileName)
/**
* @param node
* @param attributeName - name of child node to return
* @return
*/
static Node getAttributeByName(Node node, String attributeName)
{
if (node == null) return null;
NamedNodeMap nnm = node.getAttributes();
for (int i = 0; i < nnm.getLength(); i++)
{
Node n = nnm.item(i);
if (n.getNodeName().equals(attributeName))
{
return n;
}
}
return null; // no such attribute was found
} //end Node getAttributeByName(Node node, String attributeName)
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".