如何在java中将TIFF转换为JPEG / PNG [英] How to convert TIFF to JPEG/PNG in java

查看:2680
本文介绍了如何在java中将TIFF转换为JPEG / PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在尝试显示图像文件时遇到问题。不幸的是,图像格式是TIFF格式,主要的网络浏览器不支持(因为我知道只有Safari支持这种格式)。由于某些限制,我必须将此格式转换为主浏览器支持的其他格式。但是,当我尝试转换格式时,它给我带来了很多问题。

recently i'm facing problem when try to display an image file. Unfortunately, the image format is TIFF format which not supported by major web browser (as i know only Safari support this format). Due to certain constraint, i have to convert this format to others format that supported by major browser. However, it bring a lots of problem for me when i try to converting the format.

我在网上搜索过,虽然在此链接中发布了类似的问题< a href =https://stackoverflow.com/questions/2291358/how-do-i-convert-a-tif-to-png-in-java>如何在Java中将TIF转换为PNG?但我不能得到它提出的结果..

I had search through the web and although there been posted similar issue in this link How do I convert a TIF to PNG in Java?" but i can't have the result as it proposed..

因此我再次提出这个问题,希望能有更好的解释和指导。 。

Therefore i raise this Question again to wish that can have better explanation and guideline from you all..

在完成建议的解决方案时,我遇到的问题很少:

There were few issue i'm faced during go through with the solution that proposed:

1)根据 Jonathan Feinberg 提出的答案,它需要安装JAI和JAI / ImageIO。
然而,在我安装了它们之后我还是无法在Netbean 7.2中导入文件NetBean 7.2仍然建议导入默认的imageIO库。

1) According to the answer that proposed by Jonathan Feinberg, it need to install JAI and JAI/ImageIO. However, after i installed both of them i still couldn't import the file in Netbean 7.2. NetBean 7.2 remain propose import default imageIO library.

2)当我使用默认的ImageIO库Read方法时,它会返回NULL值,我可以不继续继续。

2) when i'm using default ImageIO library Read method, it will return NULL value and i cannot continue to proceed.

3)我还尝试了其他方法,例如使用BufferedOutputStream方法将TIFF文件转换为BIN文件,但结果文件大于11 MB,这是加载太大而最终加载失败。

3) I also tried others method such as convert TIFF file to BIN File by using BufferedOutputStream method but the result file is greater than 11 MB which is too large to load and end up loading failed.

 if (this.selectedDO != null) {
        String tempDO = this.selectedDO.DONo;
        String inPath = "J:\\" + tempDO + ".TIF";
        String otPath = "J:\\" + tempDO + ".bin";

        File opFile = new File(otPath);

        File inFile = new File(inPath);

        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        try {
            input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);
            output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);

            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }

        } finally {
            try {
                output.flush();
                output.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

因此,希望能得到您的帮助和建议所以我可以将TIFF格式转换为其他格式,例如JPEG / PNG。

Hence, hope that can get help and advise from you all so that i can convert TIFF format to other format such as JPEG/PNG.

推荐答案

经过一些研究和测试,找到了一种将TIFF转换为JPEG的方法,很抱歉等待很长时间只上传了这个答案。

Had gone through some study and testing, found a method to convert TIFF to JPEG and sorry for pending so long only uploaded this answer.

        SeekableStream s = new FileSeekableStream(inFile);
        TIFFDecodeParam param = null;
        ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
        RenderedImage op = dec.decodeAsRenderedImage(0);

        FileOutputStream fos = new FileOutputStream(otPath);
        JPEGEncodeParam jpgparam = new JPEGEncodeParam();
        jpgparam.setQuality(67);
        ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
        en.encode(op);
        fos.flush();
        fos.close();

p / s:otPath是指您想要存储JPEG图像的路径。
例如:C:/image/abc.JPG;
inFile是输入文件,它是TIFF文件

p/s: otPath is meant to the path that you would like to store your JPEG image. For example: "C:/image/abc.JPG"; inFile is the input file which is the TIFF file

至少这种方法对我来说是可行的。如果还有其他更好的方法,请与我们分享。

At least this method is workable to me. If there is any other better method, kindly share along with us.

谢谢和问候,

这篇关于如何在java中将TIFF转换为JPEG / PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆