简体   繁体   English

如何转换PNG < - > JPEG < - > TIFF图像

[英]How to convert PNG<->JPEG<->TIFF images

is there any way I can convert PNG<->JPEG<->TIFF images in Java? 有什么办法可以在Java中转换PNG < - > JPEG < - > TIFF图像吗? I have String i/p passing parameter (PNG or JPEG converted to []byte which get converted to String) to ConvertImage(String xyz) method. 我有String i / p传递参数(PNG或JPEG转换为[]字节转换为String)转换为ConvertImage(String xyz)方法。

String ConvertImage(String xyz) 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. 所以我正在寻找从单个方法转换以及返回字节数组或字符串,然后最后我想存储在DB中。

1.if i/p is PNG converted o/p is JPEG 1.如果i / p是PNG转换的o / p是JPEG

2.if i/p is JPEG converted o/p is PNG 2.如果i / p是JPEG转换,则o / p是PNG

3.If i/p is PNG or JPEG converted o/p is TIFF 3.如果i / p是PNG或JPEG转换,则o / p为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. 我现在正在使用JAI进行此转换(在我的上篇文章中我没有提及所有这些细节,对此抱歉)但是现在不想使用JAI。

Please let me know if you have any quick solution for all these 3 types? 如果您对这3种类型有任何快速解决方案,请告诉我们?

Thank you very much in advance!!! 非常感谢你提前!!!

Most image types can be read into memory using a simple call to ImageIO.read : 使用对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 : 然后可以通过将RenderedImageBufferedImage是其实现)传递给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"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM