简体   繁体   English

ImageMagick示例,将多个JPEG文件转换为JAVA中的多页TIFF

[英]ImageMagick example to convert multiple JPEG files to multi-page TIFF in JAVA

My requirement is to convert multiple jpeg files to a multi-page Tiff file. 我的要求是将多个jpeg文件转换为多页的Tiff文件。 Initially, I've gone through this post and I was able to create tiff files in java using jai_imageio libraries but unfortunately these libraries are not open source. 最初,我已经看完了这篇文章,并且能够使用jai_imageio库在Java中创建tiff文件,但是不幸的是,这些库不是开源的。 Later, I've heard about ImageMagick which could exactly give me what I want. 后来,我听说ImageMagick可以给我我想要的东西。 I installed ImageMagick in my machine and I wrote a small utility program which takes multiple jpegs as input and gives a TIFF file as output. 我在计算机上安装了ImageMagick,并编写了一个小型实用程序,该程序将多个jpeg作为输入,并提供TIFF文件作为输出。

The code: 编码:

try {
    Process p = Runtime
        .getRuntime()
        .exec("C:/Program Files/ImageMagick-6.8.8-Q16/convert E:/1.jpg E:/2.jpg E:/3.jpg -compress JPEG " 
    +"E:/mul.tiff");
    p.waitFor();

    } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
}

The utility works very fine but the problem with the above program is, I cannot debug and the above code wouldn't throw any error even if I specify wrong path for input files. 该实用程序可以很好地运行,但是上面程序的问题是,即使我为输入文件指定了错误的路径,我也无法调试并且上面的代码也不会抛出任何错误。

I know about Jmagick which provides a Java Interface for ImageMagick. 我了解Jmagick ,它为ImageMagick提供了Java接口。 It'd be helpful for me if anyone provides me a Jmagick sample program in java which can create multi-page tiff by multiple jpegs as input. 如果有人在Java中为我提供Jmagick示例程序,这对我会有所帮助,该程序可以通过输入多个jpeg创建多页tiff。

Thanks. 谢谢。

After a lot of googling, I found im4java which helped me to convert multiple jpegs to a single tiff. 经过大量的搜索,我发现了im4java ,它帮助我将多个jpeg转换为单个tiff。 It is a java wrapper for ImageMagick . 它是ImageMagick的Java包装器。 And for the set-up and examples visit this . 有关设置和示例的信息,请访问this Download the required jars here and put it in your classpath. 此处下载所需的jar,并将其放在您的类路径中。

Here's the sample code I'm using. 这是我正在使用的示例代码。

     // create the operation, add images and operators/options
        ConvertCmd cmd = new ConvertCmd();
        IMOperation op = new IMOperation();
        op.addImage("E:/jpeg/001.jpg");
        op.addImage("E:/jpeg/003.jpg");
        op.addImage("E:/jpeg/006.jpg");

        op.compress("JPEG");
        op.format("TIFF");// set the format.
        op.addImage("E:/im4j-compressed.tiff");

        // execute the operation
        cmd.run(op);

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

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