1

is there any way I can convert PNG<->JPEG<->TIFF images in Java? I have String i/p passing parameter (PNG or JPEG converted to []byte which get converted to String) to ConvertImage(String xyz) method.

String ConvertImage(String xyz)

So I am looking for following conversion from single method and return byte array or string and then finally I want to store in DB.

1.if i/p is PNG converted o/p is JPEG

2.if i/p is JPEG converted o/p is PNG

3.If i/p is PNG or JPEG converted o/p is TIFF

I am using JAI right now for this conversion(In my prev post I did not mention all these details sorry about that) but don't want to use JAI now.

Please let me know if you have any quick solution for all these 3 types?

Thank you very much in advance!!!

Sandeep
  • 47
  • 1
  • 7
  • In addition, you want to read [Can't read and write a TIFF image file using Java ImageIO standard library](https://stackoverflow.com/questions/1954685/cant-read-and-write-a-tiff-image-file-using-java-imageio-standard-library?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) and [Working with Images](https://docs.oracle.com/javase/tutorial/2d/images/index.html) – MadProgrammer Jun 01 '18 at 23:27

1 Answers1

4

Most image types can be read into memory using a simple call to ImageIO.read:

import javax.imageio.ImageIO;
import java.io.File;

public class ImageTest {
    public static void main(String[] args) {
        BufferedImage image = ImageIO.read(new File("image.png"));//Or image.jpg or image.tiff, etc.
    }
}

Writing images can then be done by passing a RenderedImage (which BufferedImage is an implementation of) to ImageIO.write:

import javax.imageio.ImageIO;
import java.io.File;

public class ImageTest {
    public static void main(String[] args) {
        BufferedImage image = ImageIO.read(new File("image.png"));//Or image.jpg or image.tiff, etc.
        String[] formatNames = ImageIO.getWriterFormatNames();
        //A robust use would validate whatever format you're intending to use against 
        //the canonical list of format names retrieved by that call
        ImageIO.write(image, "tiff", new File("image.tiff"));
    }
}
Xirema
  • 19,889
  • 4
  • 32
  • 68
  • 1
    FYI: Tiff isn’t supported out of the box (at least while I was using it in Java 7) - You’ll need to install the Java Advance Imaging API to get it - unless, as I read, you’re using Java 9+ – MadProgrammer Jun 01 '18 at 22:51
  • Sorry, couldn't post links from the mobile App - From [Writing/Saving an Image](https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html), [Java 8 JavaDocs](https://docs.oracle.com/javase/8/docs/api/javax/imageio/package-summary.html) and [Can't read and write a TIFF image file using Java ImageIO standard library](https://stackoverflow.com/questions/1954685/cant-read-and-write-a-tiff-image-file-using-java-imageio-standard-library?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa). – MadProgrammer Jun 01 '18 at 23:25
  • And also the [Java 9 JavaDocs](https://docs.oracle.com/javase/9/docs/api/javax/imageio/package-summary.html) which does list it – MadProgrammer Jun 01 '18 at 23:25
  • Thanks a lot for your valuable help. I am just trying for TIFF and yes seems like java io is not supporting. but I don't want to use JAI. what should I use for TIFF? If i get any sample example then it will be great. – Sandeep Jun 03 '18 at 23:31