如何在java中将TIFF转换为JPEG/PNG

如何在java中将TIFF转换为JPEG/PNG,java,image,tiff,jai,Java,Image,Tiff,Jai,最近我在尝试显示图像文件时遇到了一个问题。不幸的是,图像格式是TIFF格式,主要web浏览器不支持这种格式(据我所知,只有Safari支持这种格式)。由于某些限制,我必须将此格式转换为主浏览器支持的其他格式。然而,当我尝试转换格式时,它给我带来了很多问题 我在网上搜索过,虽然在这个链接中也有类似的问题,“但我不能得到它所建议的结果。” 因此,我再次提出这个问题,希望大家能有更好的解释和指导 在实施提出的解决方案过程中,我几乎没有遇到什么问题: 1) 根据Jonathan Feinberg提出的答

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

我在网上搜索过,虽然在这个链接中也有类似的问题,“但我不能得到它所建议的结果。”

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

在实施提出的解决方案过程中,我几乎没有遇到什么问题:

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

2) 当我使用默认的ImageIO库读取方法时,它将返回空值,我无法继续

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

 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。

首先,请查看。有关您可以使用的代码

javax.imageio.ImageIO.write(im, type, represFile);

正如您在中所看到的。

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

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();
otPath
是要存储JPEG图像的路径。 例如:“C:/image/abc.JPG”;
infle
是作为TIFF文件的输入文件

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

  • 添加依赖项

     <dependency>
     <groupId>com.github.jai-imageio</groupId>
     <artifactId>jai-imageio-core</artifactId>
     <version>1.3.1</version> </dependency>
    

  • 如果页面较多,请按照以下步骤操作:

  • 添加依赖项:

    <dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    
    
    com.github.jai-imageio
    jai图像核心
    1.4.0
    
  • 使用以下Java8代码

    public void convertTiffToPng(File file) {
    try {
        try (InputStream is = new FileInputStream(file)) {
            try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {
                Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
                if (iterator == null || !iterator.hasNext()) {
                    throw new RuntimeException("Image file format not supported by ImageIO: " + file.getAbsolutePath());
                }
    
    
                // We are just looking for the first reader compatible:
                ImageReader reader = iterator.next();
                reader.setInput(imageInputStream);
    
                int numPage = reader.getNumImages(true);
    
                // it uses to put new png files, close to original example n0_.tiff will be in /png/n0_0.png
                String name = FilenameUtils.getBaseName(file.getAbsolutePath()); 
                String parentFolder = file.getParentFile().getAbsolutePath();
    
                IntStream.range(0, numPage).forEach(v -> {
                    try {
                        final BufferedImage tiff = reader.read(v);
                        ImageIO.write(tiff, "png", new File(parentFolder + "/png/" + name + v + ".png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
    
    public void convertTiffToPng(文件){
    试一试{
    try(InputStream is=新文件InputStream(文件)){
    try(ImageInputStream ImageInputStream=ImageIO.createImageInputStream(is)){
    迭代器迭代器=ImageIO.getImageReaders(imageInputStream);
    if(iterator==null | |!iterator.hasNext()){
    抛出新的RuntimeException(“ImageIO不支持图像文件格式:+file.getAbsolutePath());
    }
    //我们正在寻找第一个兼容的读卡器:
    ImageReader=iterator.next();
    reader.setInput(imageInputStream);
    int numPage=reader.getNumImages(true);
    //它用于放置新的png文件,接近原始示例n0_u0。tiff将位于/png/n0_0.png中
    String name=FilenameUtils.getBaseName(file.getAbsolutePath());
    字符串parentFolder=file.getParentFile().getAbsolutePath();
    IntStream.range(0,numPage).forEach(v->{
    试一试{
    最终缓冲区图像tiff=读卡器读取(v);
    write(tiff,“png”,新文件(parentFolder+“/png/”+name+v+“.png”);
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    });
    }
    }
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    }
    

  • 如果您的目标是Android,您可以尝试使用Github,它提供了许多用于处理、打开和写入.tiff文件的实用程序

    Git中的一个简单示例演示了如何将TIFF转换为JPEG:

    TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();
    //Set to true if you want use java exception mechanism
    options.throwExceptions = false; 
    //Available 128Mb for work
    options.availableMemory = 128 * 1024 * 1024; 
    //Number of tiff directory to convert;
    options.readTiffDirectory = 1;         
    //Convert to JPEG
    TiffConverter.convertTiffJpg("in.tif", "out.jpg", options, progressListener);
    

    看一看ImageMagic()。这是ImageMagic()的java界面。它与在中提出的ImageMagick类似吗?我使用ImageMagick尝试了在那篇文章中提出的方法,但在转到
    ConvertCmd convert=new ConvertCmd()时失败了
    我曾尝试过MKYong的示例,但当它出现ImageIO.Read时,它返回null,因为无法读取TIF格式。如果我读取JPG和PNG,将不会出现问题。一切都很顺利,但只是在尝试读取TIFF格式时。请看,它可能很有用。感谢您的建议,我未能添加JAI jar,尽管我的C中已经包含了JAI jarLASSPATH,但仍然无法导入它..你能提供更多关于如何包含JAI jar的信息吗?非常感谢你的帮助在JAI中挣扎了一段时间后,我终于能够使用它..不幸的是,它会提示我一个错误,我对此一无所知。java.lang.NoClassDefFoundError:无法初始化类javax.media。jai.jai..我在网上搜索过,大多数人说它在服务器旁边找不到jai库。但我确信它已经添加到了jai文件中。你知道这个错误吗?好的,jai名称空间现在在Java 8中被列入黑名单。你可以从这里获得所需的jar:这对小图像很有效,就像我用5.7KB的图像尝试过的那样没问题,但在80 KB的图像上尝试后,我得到了java.lang.IndexOutOfBoundsException。有什么帮助吗?获取错误无法实例化类型JPEGencodeParam多页Tif文件如何?你能将此库与具有多页Tif一起使用吗?你能共享一些代码吗?谢谢。如果他不在Android上怎么办?你建议使用的库本机Android库。事实上,我的建议适用于Android。之所以将其包含在此处,是因为我花了很多时间搜索库,并希望在有人需要时与他人共享。我会澄清它适用于Android。感谢..反馈:)+1感谢您提供了一个有助于Android开发人员的答案。因为这个问题可能更需要对于移动设备,则是桌面。我得到
    javax.imageio.IIOException:水平差分Pr不支持16位样本
    
    TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();
    //Set to true if you want use java exception mechanism
    options.throwExceptions = false; 
    //Available 128Mb for work
    options.availableMemory = 128 * 1024 * 1024; 
    //Number of tiff directory to convert;
    options.readTiffDirectory = 1;         
    //Convert to JPEG
    TiffConverter.convertTiffJpg("in.tif", "out.jpg", options, progressListener);