Skip to main content

TIFF to JPEG conversion in Servlet

10 replies [Last post]
ajayrl
Offline
Joined: 2007-10-18
Points: 0

Hi,

I have written the following code inside the doGet() method of a servlet to read a TIFF image stored in a blob field in a Database and then convert it to JPEG and render it in the servlet output stream.

But the image is not rendered in the browser, and i do not encounter ant errors on the server logs.

while(rs.next())
{
binstr = rs.getBinaryStream("sign_area");
System.out.println("got thebinary stream from the database");
SeekableStream s = new MemoryCacheSeekableStream(binstr);
RenderedImage ri = JAI.create("stream",s);
ServletOutputStream fos1 = null;
fos1 = response.getOutputStream();
com.sun.media.jai.codec.JPEGEncodeParam JPEGparam = new com.sun.media.jai.codec.JPEGEncodeParam();
ImageEncoder encoder =ImageCodec.createImageEncoder("jpeg",fos1,JP
EGparam);
encoder.encode(ri);

fos1.flush();
fos1.close();
}

can any one suggest where i am going wrong. or suggest an aletrnative way to read tiff from a Database and display in a browser.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
assecosto
Offline
Joined: 2012-02-28
Points: 0

Download JIMI Software Development Kit jimi1_0.zip and set JimiProClasses.zip to your classpath

http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-arc... is older java image library, but it is easy to use and there is no platform dependent code (no native executables, can use it like standard jar)

import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import com.sun.jimi.core.Jimi;

public class JIMIImageConverter {
     
    public static byte[] convert(byte[] inBytes, String inMimeType, String outMimeType) throws Exception{

        Image rawImage = Jimi.getImage(new ByteArrayInputStream(inBytes), inMimeType);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Jimi.putImage(outMimeType, rawImage, outputStream);
        return outputStream.toByteArray();

    }

}

where inMimeType and outMimeType are graphics formats mimetypes

bpb
Offline
Joined: 2004-06-23
Points: 0

For image reading and writing we recommend that you use the Java Image I/O Framework javax.imageio and the Image I/O Tools classes at https://jai-imageio.dev.java.net and https://jai-imageio-core.dev.java.net instead of the com.sun.media.jai.codec classes and the JAI operations that depend on them.

> Hey ajayrl,
>
> Are you able to instantiate JPEGEncodeParam()
> directly? When I tried it I got a "cannot
> instantiate" error.
>
> Also createImageEncoder() does not accept a
> JPEGEncodeParam as an argument.

sripathiraj
Offline
Joined: 2007-09-24
Points: 0

Hi ajay,

I think, you have done some what technical things wrong in servlet. Use OutputStream instead of ServletOutputStream, you have missed the quality also...........

use the following code instead of yours.

{
PlanarImage img;
OutputSteam out;

encodeParam = new JPEGEncodeParam();
encodeParam.setQuality(1.0f);
encoder = ImageCodec.createImageEncoder("JPEG", out, encodeParam);
try {
encoder.encode(img);
out.close();
}
catch (IOException e) {}
}

[u][b]Have a Happy Coding[/b][/u]

I hope its helps you
Sripathi & Asit

ajayrl
Offline
Joined: 2007-10-18
Points: 0

first of all i wish to thank you all for the responses, but i have tried the suggestions and could not get a image in the browser.

my query returns only one row.
Ihave set the mime type in the servlet.
The same servlet is able to stream a tiff image to the browser and is viewable with a quickview plugin in the browser. I get the errors only when i try to convert the image to jpeg before putting it out on the servlet output stream. I am trying to convert it in the servlet because my JAI applets could not render the tiff image from the servlet output stream either.

I tried with the code snippets i.e. the Planar Image with JPEGEncodeParam quality set to 1.0f .

I get the following exception during runtime at the encoder.encode(op) method.

java.lang.RuntimeException: - Unable to render RenderedOp for this operation.

please suggest.

Brian Burkhalter

You might do better to post this problem to a Java EE forum.

On Thu, 18 Oct 2007, jai-interest@javadesktop.org wrote:

> first of all i wish to thank you all for the responses, but i have tried the suggestions and could not get a image in the browser.
>
> my query returns only one row.
> Ihave set the mime type in the servlet.
> The same servlet is able to stream a tiff image to the browser and is viewable with a quickview plugin in the browser. I get the errors only when i try to convert the image to jpeg before putting it out on the servlet output stream. I am trying to convert it in the servlet because my JAI applets could not render the tiff image from the servlet output stream either.
>
> I tried with the code snippets i.e. the Planar Image with JPEGEncodeParam quality set to 1.0f .
>
> I get the following exception during runtime at the encoder.encode(op) method.
>
> java.lang.RuntimeException: - Unable to render RenderedOp for this operation.
>
> please suggest.
> [Message sent by forum member 'ajayrl' (ajayrl)]
>
> http://forums.java.net/jive/thread.jspa?messageID=241128
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

>^..^< >^..^<

Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

ajayrl
Offline
Joined: 2007-10-18
Points: 0

Hi guys,

Thankyou for all the inputs. I like this forum.

I have solved the issue.
one has to first Decode the tiff image (TIFFDecoder) from the binary stream read from the blob field of the database then encode it using the JPEGEncoder , then stream it out to the servlet output stream.

The trick is to first decode the binary stream that your read , then encode to the output stream you wish to write to.
while(rs.next())
{
binstr = rs.getBinaryStream("sign_area");
SeekableStream s = new MemoryCacheSeekableStream(binstr);

TIFFDecodeParam param = null;
int imageToLoad =0;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op1 = dec.decodeAsRenderedImage(imageToLoad);

OutputStream os= response.getOutputStream();
JPEGEncodeParam jparam = new JPEGEncodeParam();
jparam.setQuality(1.0f);

ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", os, jparam);

encoder.encode(op1);
os.flush();
os.close();
}

ermi
Offline
Joined: 2007-10-29
Points: 0

Hey ajayrl,

Are you able to instantiate JPEGEncodeParam() directly? When I tried it I got a "cannot instantiate" error.

Also createImageEncoder() does not accept a JPEGEncodeParam as an argument.

ajayrl
Offline
Joined: 2007-10-18
Points: 0

hi ernie,

please ensure you have the latest jai libraries and have included them in your classpath.
ideally the jai_core.jar,jai_codec.jar and mlibwrapper.jar would be needed.

you can download the jre update for jai from the sun java site

http://developers.sun.com/downloads/

or your can search for downloads available for your Operating System in the JAI home page.

I dont have the exact url from where i downloaded the jai update for my jdk.

Regards
Ajay

ajayrl
Offline
Joined: 2007-10-18
Points: 0

I am sorry i wrongly spelt your name.

loki der quaeler

at first blush - this is not JAI, but rather a servlet question..
things that look wonky in what you're doing below:
. if there's more than one element in the rs iterator, you're
throwing data for multiple images down the stream to where i assume
the browser expecting exactly one image
. you need to set a content type on the response object - maybe
you're doing it, before the block you've pasted, but i have no idea

google's your friend on this one

On Oct 18, 2007, at 12.39 PM, jai-interest@javadesktop.org wrote:

> Hi,
>
> I have written the following code inside the doGet() method of a
> servlet to read a TIFF image stored in a blob field in a Database
> and then convert it to JPEG and render it in the servlet output
> stream.
>
> But the image is not rendered in the browser, and i do not
> encounter ant errors on the server logs.
>
> while(rs.next())
> {
> binstr = rs.getBinaryStream("sign_area");
> System.out.println("got thebinary stream from the database");
> SeekableStream s = new MemoryCacheSeekableStream(binstr);
> RenderedImage ri = JAI.create("stream",s);
> ServletOutputStream fos1 = null;
> fos1 = response.getOutputStream();
> com.sun.media.jai.codec.JPEGEncodeParam JPEGparam = new
> com.sun.media.jai.codec.JPEGEncodeParam();
> ImageEncoder encoder =ImageCodec.createImageEncoder("jpeg",fos1,JP
> EGparam);
> encoder.encode(ri);
>
> fos1.flush();
> fos1.close();
> }
>
> can any one suggest where i am going wrong. or suggest an
> aletrnative way to read tiff from a Database and display in a browser.
> [Message sent by forum member 'ajayrl' (ajayrl)]
>
> http://forums.java.net/jive/thread.jspa?messageID=240889
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net