如何在 Java 中将 TIF 转换为 PNG?

发布于 2024-08-22 06:10:50 字数 88 浏览 10 评论 0原文

在 Java 下,将 TIF 文件转换为 PNG 的最佳方法是什么?

简单是更好的选择,但如果最简单的方法是使用第三方库,那么我会考虑该解决方案。

Under Java what is the best way to go about converting an TIF file to a PNG?

Simplicity is preferable, but if the simplest way is to use a third party library then I would consider that solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

余生共白头 2024-08-29 06:10:50

首先,安装 JAI。然后安装 JAI/ImageIO。然后做

public static void main(final String[] args) throws Exception
{
    final BufferedImage tif = ImageIO.read(new File("test.tif"));
    ImageIO.write(tif, "png", new File("test.png"));
}

First, install JAI. Then install JAI/ImageIO. Then do

public static void main(final String[] args) throws Exception
{
    final BufferedImage tif = ImageIO.read(new File("test.tif"));
    ImageIO.write(tif, "png", new File("test.png"));
}
绻影浮沉 2024-08-29 06:10:50

使用 imageMagic java 库,例如 im4java,它们的性能和质量比 JAI 好得多,

例如:

import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;

public static void convertTifToPng(File inputImage, File outputImage){
  IMOperation op = new IMOperation();
  op.addImage(); //place holder for input file
  op.addImage(); //place holder for output file

  ConvertCmd convert = new ConvertCmd();
  convert.run(op, new Object[]{inputImage.getAbsolutePath(), outputImage.getAbsolutePath()});
}

im4java 的 maven 依赖项是

<dependency>
  <groupId>im4java</groupId>
  <artifactId>im4java</artifactId>
  <version>0.98.0</version>
</dependency>

Use imageMagic java libraries like im4java, their performance and quality is much better then JAI

for example:

import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;

public static void convertTifToPng(File inputImage, File outputImage){
  IMOperation op = new IMOperation();
  op.addImage(); //place holder for input file
  op.addImage(); //place holder for output file

  ConvertCmd convert = new ConvertCmd();
  convert.run(op, new Object[]{inputImage.getAbsolutePath(), outputImage.getAbsolutePath()});
}

maven dependency for im4java is

<dependency>
  <groupId>im4java</groupId>
  <artifactId>im4java</artifactId>
  <version>0.98.0</version>
</dependency>
难如初 2024-08-29 06:10:50

Java 高级成像 APi 是一个很好的图像操作库

http://java .sun.com/products/java-media/jai/iio.html

Java advanced imaging APi is a good library for image manipulations

http://java.sun.com/products/java-media/jai/iio.html

初雪 2024-08-29 06:10:50

下载 JIMI Software Development Kit jimi1_0.zip 并将 JimiProClasses.zip 设置为类路径

http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-client-419417.html #7259-jimi_sdk-1.0-oth-JPR

JIMI是较旧的java图像库,但它很容易使用,并且没有平台相关的代码(没有本机可执行文件,可以像标准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();

}

}

其中inMimeType和outMimeType图形格式是 mimetypes

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

混吃等死 2024-08-29 06:10:50

也许你可以使用这个代码,对我有用

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.IOException;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;

public class ImageConvert {

    public static RenderedImage[] readMultiPageTiff(String fileName)throws IOException{
           File file = new File(fileName);
           SeekableStream seekableStream = new FileSeekableStream(file);
           ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", seekableStream, null);
           int numPages = decoder.getNumPages();
           RenderedImage image[]= new RenderedImage[numPages];
           int count = 0;
           for(int i=0;i<decoder.getNumPages();i++){
               image[i] = decoder.decodeAsRenderedImage(i);
               count++;
           }

           String newFolderName;
           String s3 = fileName;
           String [] temp = null;
           temp = s3.split("\\.");


           int j;
               j = 0;
               do{
                     newFolderName = temp[j];
                     String spoonFeeding = newFolderName;
                     File f = new File(spoonFeeding);
                     f.mkdirs();
                     j++;
               }while (j<1);

           for (int i = 0; i < count; i++) {
               RenderedImage page = decoder.decodeAsRenderedImage(i);
               File fileObj = new File(newFolderName+"/" + (i+1) + ".png");
               System.out.println("Saving " + fileObj.getCanonicalPath());
               ParameterBlock parBlock = new ParameterBlock();
               parBlock.addSource(page);
               parBlock.add(fileObj.toString());
               parBlock.add("png");
               RenderedOp renderedOp = JAI.create("filestore",parBlock);
               renderedOp.dispose();
           }
           return image;
        }

}

maybe you can use this code, works for me

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.IOException;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;

public class ImageConvert {

    public static RenderedImage[] readMultiPageTiff(String fileName)throws IOException{
           File file = new File(fileName);
           SeekableStream seekableStream = new FileSeekableStream(file);
           ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", seekableStream, null);
           int numPages = decoder.getNumPages();
           RenderedImage image[]= new RenderedImage[numPages];
           int count = 0;
           for(int i=0;i<decoder.getNumPages();i++){
               image[i] = decoder.decodeAsRenderedImage(i);
               count++;
           }

           String newFolderName;
           String s3 = fileName;
           String [] temp = null;
           temp = s3.split("\\.");


           int j;
               j = 0;
               do{
                     newFolderName = temp[j];
                     String spoonFeeding = newFolderName;
                     File f = new File(spoonFeeding);
                     f.mkdirs();
                     j++;
               }while (j<1);

           for (int i = 0; i < count; i++) {
               RenderedImage page = decoder.decodeAsRenderedImage(i);
               File fileObj = new File(newFolderName+"/" + (i+1) + ".png");
               System.out.println("Saving " + fileObj.getCanonicalPath());
               ParameterBlock parBlock = new ParameterBlock();
               parBlock.addSource(page);
               parBlock.add(fileObj.toString());
               parBlock.add("png");
               RenderedOp renderedOp = JAI.create("filestore",parBlock);
               renderedOp.dispose();
           }
           return image;
        }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文