Skip to main content

[JAI IMAGEIO] TIFF to JPEG Loss DPI, HOW TO?

3 replies [Last post]
grattepoil
Offline
Joined: 2006-12-14

Hi!

I tried to convert a tiff image to jpeg with the same dpi resolution but i don't know how to make it or if i'm on the right way.
Here is my code.
Thanks for your help!

public static void convertFromTifToJpgPrim(String sSourceFileName, String sTargetFileName) throws CoinDatabaseLoadException, SQLException, NamingException{

try
{
RenderedOp renderedOPCrop = JAI.create("fileload", sSourceFileName);

RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);

RenderedOp renderedOPScale = JAI.create("SubsampleAverage",
renderedOPCrop,
new Double((double)0.5),
new Double((double)0.5),
qualityHints);

JPEGEncodeParam jpgparam = new JPEGEncodeParam();
jpgparam.setQuality(1f);

FileOutputStream fs = new FileOutputStream(sTargetFileName);
ImageEncoder enc = ImageCodec.createImageEncoder("jpeg", fs, jpgparam);
enc.encode(renderedOPScale);

fs.flush();
fs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

Regards,
Julien

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bpb
Offline
Joined: 2004-06-23

Julien,

Is this code not working in some way? How?

Brian

> Hi!
>
> I tried to convert a tiff image to jpeg with the same
> dpi resolution but i don't know how to make it or if
> i'm on the right way.
> Here is my code.
> Thanks for your help!
>
>
> public static void convertFromTifToJpgPrim(String
> sSourceFileName, String sTargetFileName) throws
> CoinDatabaseLoadException, SQLException,
> NamingException{
>
> try
> {
> RenderedOp renderedOPCrop = JAI.create("fileload",
> sSourceFileName);
>
> RenderingHints qualityHints = new
> RenderingHints(RenderingHints.KEY_RENDERING,RenderingH
> ints.VALUE_RENDER_QUALITY);
>
>
> RenderedOp renderedOPScale =
> JAI.create("SubsampleAverage",
> renderedOPCrop,
> new Double((double)0.5),
> new Double((double)0.5),
> qualityHints);
>
> JPEGEncodeParam jpgparam = new JPEGEncodeParam();
> jpgparam.setQuality(1f);
>
> FileOutputStream fs = new
> FileOutputStream(sTargetFileName);
> ImageEncoder enc =
> ImageCodec.createImageEncoder("jpeg", fs, jpgparam);
> enc.encode(renderedOPScale);
>
> fs.flush();
> fs.close();
> }
> catch(Exception e)
> {
> e.printStackTrace();
> }
> }
>
>
> Regards,
> Julien

grattepoil
Offline
Joined: 2006-12-14

Hello Brian,

this code works but my tiff is in 200dpi and when i convert in jpeg i loss this parameter. my jpeg is converted in 96 dpi (the default value i think).
So do yo know how can i convert with the same dpi?
Maybe i have to use an other method or add parameter?

bpb
Offline
Joined: 2004-06-23

You need to ensure that the DPI is set on the output JPEG image metadata object. One problem is that the JAI Image I/O Tools JPEG writer does not presently support setting metadata. If you use the Java SE JPEG writer however I think you will preserve the metadata DPI values if you pass the image metadata object obtained from the TIFF reader directly to the TIFF writer. The JPEG writer will not understand the TIFF metadata format but as they both understand the platform-neutral metadata format I think that the JPEG writer should pick up the DPI values.

> Hello Brian,
>
> this code works but my tiff is in 200dpi and when i
> convert in jpeg i loss this parameter. my jpeg is
> converted in 96 dpi (the default value i think).
> So do yo know how can i convert with the same dpi?
> Maybe i have to use an other method or add parameter?