[
https://issues.apache.org/jira/browse/IMAGING-126?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13910785#comment-13910785
]
Tilman Hausherr commented on IMAGING-126:
-----------------------------------------
Here's the code I used:
{code}
public static boolean writeImage(BufferedImage image, String imageFormat,
Object outputStream, int resolution,
float quality)
throws IOException
{
// convert image format String to Apache Imaging constant
imageFormat = imageFormat.toUpperCase();
if ("TIF".equals(imageFormat))
{
imageFormat = "TIFF";
}
ImageFormat foundImageFormat = null;
for (ImageFormats imf : ImageFormats.values())
{
if (imf.getExtension().equals(imageFormat))
{
foundImageFormat = imf;
break;
}
}
if (foundImageFormat == null)
{
LOG.error("imageFormat " + imageFormat + " is unknown");
return false;
}
// prepare parameters (resolution and tiff compression if applicable)
Map<String, Object> params = new HashMap<String, Object>();
params.put(ImagingConstants.PARAM_KEY_PIXEL_DENSITY,
PixelDensity.createFromPixelsPerInch(resolution, resolution));
if (foundImageFormat == ImageFormats.TIFF)
{
// CCITT G4 compression is better for b/w images
if (image.getType() == BufferedImage.TYPE_BYTE_BINARY)
{
params.put(ImagingConstants.PARAM_KEY_COMPRESSION,
TiffConstants.TIFF_COMPRESSION_CCITT_GROUP_4);
}
else
{
params.put(ImagingConstants.PARAM_KEY_COMPRESSION,
TiffConstants.TIFF_COMPRESSION_LZW);
}
}
try
{
Imaging.writeImage(image, (File) outputStream, foundImageFormat,
params);
return true;
}
catch (ImageWriteException ex)
{
LOG.error("ImageWriteException", ex);
return false;
}
}
{code}
> TIFF and PNG images should not be bigger than the ones created by java ImageIO
> ------------------------------------------------------------------------------
>
> Key: IMAGING-126
> URL: https://issues.apache.org/jira/browse/IMAGING-126
> Project: Commons Imaging
> Issue Type: Improvement
> Components: Format: PNG, Format: TIFF
> Affects Versions: 1.0
> Environment: W7
> Reporter: Tilman Hausherr
> Priority: Minor
> Attachments: pdfbox-1870-devicen3-01.png,
> pdfbox-1870-devicen3-01.tif, pdfbox-1870-devicen3.pdf-1.png,
> pdfbox-1870-devicen3.pdf-1.tif
>
>
> I tried to use Apache Imaging for the PDFBOX project (PDFBOX-1734) because of
> problems with setting the tiff resolution in java imageio.
> While the code is pretty nice, I found that the generated images are
> sometimes much bigger in size than the ones generated by java imageio.
> Example:
> pdfbox-1870-devicen3-01.png 50 KB (imageio)
> pdfbox-1870-devicen3.pdf-1.png 70 KB (imaging)
> pdfbox-1870-devicen3-01.tif 401 KB (imageio)
> pdfbox-1870-devicen3.pdf-1.tif 1063 KB (imaging)
--
This message was sent by Atlassian JIRA
(v6.1.5#6160)