JAVA处理tiff图片转为png、jpg等常见格式(ImageMagick)

这几天,有个tiff图片转普通图片格式的需求 ,在网上找了很多,如果用JAVA代码处理的话,图片会失真。所以想如果代码处理不了,是否可用软件来处理,利用java命令行的方式来操作软件,完成需求。答案是可行的!

ImageMagick这个工具就非常的强大!!!

https://www.w3cschool.cn/imagemagick_use/  可以了解下这个软件的功能。

这个是官网http://www.imagemagick.org/script/download.php,下载windows版本的安装包

安装就不多说了,安装位置指定之后,后面就默认就好了。下载完之后直接调用下面的命令即可。

在E盘上用控制台CMD进入

输入回车

结果

扫描二维码关注公众号,回复: 5106754 查看本文章

图片转换成功。

用JAVA命令行的方式来处理图片:

注意:

在cmd命令行输入的内容封装为数组,以空格划分。
注意以cmd命令行的方式由于安装的时候默认配置了环境变量,类似JDK,所以直接 magick即可,
现在的话需要指定全路径 

public static void tiffToPng(String tiffFilePath, String toFilePath) {
		Process process = null;
		BufferedReader input = null;
        
                        
		String[] cmd = { "D:/Program Files/ImageMagick-7.0.8-Q16/magick", tiffFilePath, toFilePath };

		try {
			process = Runtime.getRuntime().exec(cmd);
			printMessage(process.getInputStream());
			printMessage(process.getErrorStream());
			int value = process.waitFor();
			// 记录执行命令是否成功
			if (value == 0) {
				logger.info("magick exec success:" + cmd);

			} else {
				logger.error("magick exec failure:" + cmd);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("magick exec failure:", e);
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

    /**
	 * 打印命令执行的结果
	 * 
	 * @param input
	 */
	private static void printMessage(final InputStream input) {
		new Thread(new Runnable() {
			public void run() {
				InputStreamReader reader = new InputStreamReader(input);
				BufferedReader bf = new BufferedReader(reader);
				String line = null;
				try {
					while ((line = bf.readLine()) != null) {
						logger.info("ImageMagick:" + line);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

    public static void main(String[] args) {
		tiffToPng("E:\\a.tiff", "E:\\c.jpg");
	}

测试是可行的。

在linux环境下,下载linux环境对应的安装包即可。

猜你喜欢

转载自blog.csdn.net/weixin_42245930/article/details/86676929