Skip to main content

Tiff as byte [], need to convert to jpeg, and end up with OutputStream?

4 replies [Last post]
sadden
Offline
Joined: 2008-10-02
Points: 0

I have searched everywhere, but mostly people are opening image files, converting, and writing out to files.

I have a SINGLE tiff image (not multi) in memory as a byte [], I have included the JAI jars, in Eclipse, and need to

1) convert the image to Jpeg.
2) end up with an InputStream to return it to another application that will
put it in a reports file.

I've tried understanding the JAI documentation, but most of it seems geared to writing to screen, or file, and input from a file.

Any pertinent code examples would really help!
Thanks!

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-archive-downloads-java-client-419417.html#7259-jimi_sdk-1.0-oth-JPR

JIMI 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
pietblok
Offline
Joined: 2003-07-17
Points: 0

Hi Sadden,

Not completely sure what you want to achieve, but what about the following code?

[code]
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ImageStreamUtils {

public static BufferedImage getImage(byte[] bytes) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
return ImageIO.read(bais);
} catch (IOException e) {
throw new Error("Should not happen", e);
}
}

public static InputStream toJPEGInputStream(BufferedImage image) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", baos);
baos.close();
return new ByteArrayInputStream(baos.toByteArray());
} catch (IOException e) {
throw new Error("Should not happen", e);
}
}

}
[/code]
I didn't test this, but hope it helps,

Piet

Nidel, Mike

you don't need JAI at all.

what you probably want is JAI ImageIO Tools. there's a separate
mailing list for that, but i'll sketch out what you can do. all the
classes you should need are in the core JDK, you probably won't even
need to use any JAI ImageIO Tools-specific code because TIFF support
is handled as a plugin to the JDK.

create an ImageInputStream from your byte[] (using
ByteArrayInputStream).
then get an ImageReader for this image from the ImageIO class (see JDK
docs).
then get a RenderedImage by calling readAsRenderedImage() or just
read(). The latter will give you a BufferedImage.

Now use ImageIO.getImageWriter() to get an ImageWriter for the format
you want (JPEG).
Then you want to create an output stream in such a way that you can
convert it to an
InputStream for your other application. I'm not sure I quite understand
what you want,
but maybe you can create a ByteArrayOutputStream, write the image to it,
then get the
byte[] from it and pass this to a NEW ByteArrayInputStream that goes on
to the next app.

Set the output stream on the ImageWriter using setOutput(), then create
an IIOImage with
your image from the ImageReader and pass it to write() and you're done.

Thad also posted a simpler suggestion that uses the JAI mechanism to do
basically what
I've explained here. That could also work, and may be simpler, but it
requires
JAI so that's up to you.

if you have any questions about my solution, you should post them to the
jai-imageio list.

> -----Original Message-----
> From: jai-interest@javadesktop.org
> [mailto:jai-interest@javadesktop.org]
> Sent: Thursday, October 02, 2008 10:19 AM
> To: interest@jai.dev.java.net
> Subject: [JAI] Tiff as byte [], need to convert to jpeg, and
> end up with OutputStream?
>
> I have searched everywhere, but mostly people are opening
> image files, converting, and writing out to files.
>
> I have a SINGLE tiff image (not multi) in memory as a byte
> [], I have included the JAI jars, in Eclipse, and need to
>
> 1) convert the image to Jpeg.
> 2) end up with an InputStream to return it to another
> application that will put it in a reports file.
>
> I've tried understanding the JAI documentation, but most of
> it seems geared to writing to screen, or file, and input from a file.
>
> Any pertinent code examples would really help!
> Thanks!
> [Message sent by forum member 'sadden' (sadden)]
>
> http://forums.java.net/jive/thread.jspa?messageID=302945
>
> ---------------------------------------------------------------------
> 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

Thad Humphries

Wouldn't this work?

PlanarImage pi = JAI.create("ImageRead", SeekableStream.wrapInputStream(
byteArrayInputStream,true));

ImageIO.write(pi, "jpg", fileOutputStream);

On Thu, Oct 2, 2008 at 10:18 AM, wrote:

> I have searched everywhere, but mostly people are opening image files,
> converting, and writing out to files.
>
> I have a SINGLE tiff image (not multi) in memory as a byte [], I have
> included the JAI jars, in Eclipse, and need to
>
> 1) convert the image to Jpeg.
> 2) end up with an InputStream to return it to another application that will
> put it in a reports file.
>
> I've tried understanding the JAI documentation, but most of it seems geared
> to writing to screen, or file, and input from a file.
>
> Any pertinent code examples would really help!
> Thanks!
> [Message sent by forum member 'sadden' (sadden)]
>
> http://forums.java.net/jive/thread.jspa?messageID=302945
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

--
"As flies to wanton boys, are we to the gods. They kill us for their sport."
-- William Shakespeare, 'King Lear', IV, i
[att1.html]