im4java学习---阅读documentation文档

Utilities----im提供的一些工具类


①、读取图片文件信息---Info类

我们之前的做法:

op.format("%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");
IdentifyCmd identifyCmd = new IdentifyCmd(useGM);

使用工具类Info:

Info imageInfo = new Info(filename,true);
System.out.println("Format: " + imageInfo.getImageFormat());
System.out.println("Width: " + imageInfo.getImageWidth());
System.out.println("Height: " + imageInfo.getImageHeight());
System.out.println("Geometry: " + imageInfo.getImageGeometry());
System.out.println("Depth: " + imageInfo.getImageDepth());
System.out.println("Class: " + imageInfo.getImageClass());

第二个参数true,表示只获取图片的基本信息。
***这个工具类,在1.3.0版本之前,方法实现有问题。针对TIF和GIF图片,

imageInfo.getImageWidth()  返回的是第一帧的图片宽度(原文是first scene)
这个问题我也不懂,只是写出来下,大家都注意下,详细的还是看文档吧。


②、读取某个目录下所有指定后缀的图片文件---FilenameLoader类
你还在用这种批量图片处理方式么?
public void resizeImages(String... pImageNames)
恭喜你,out了。嘿嘿,开玩笑^-^
让我们来看下新的方法:
ExtensionFilter filter = new ExtensionFilter("jpg");	//指定后缀
filter.setRecursion(true);	//递归扫描
filter.ignoreDotDirs(true);	//忽略那些带点的隐藏目录(此处是个人说法,看到过,Android手机里很多这目录,但是不懂到底怎样命名)
FilenameLoader  loader = new FilenameLoader(filter);
List<String> files = loader.loadFilenames(mydir);	//mydir文件夹路径
这个类在API文档中有详细介绍。


③、既然有批量转换,我们也需要命令这一大堆目标文件的名称---FilenamePatternResolver类
// define operation and command
IMOperation op = new IMOperation();
op.addImage();                         // input-file
op.addImage();                         // output-file
ConvertCmd cmd = new ConvertCmd();

// load files
ExtensionFilter filter = new ExtensionFilter("jpg");
FilenameLoader  loader = new FilenameLoader(filter);
List<String> files = loader.loadFilenames(mydir);

// create the resolver-----看这里,baby go
FilenamePatternResolver resolver = 
    new FilenamePatternResolver("%P/%f.tif");

// now iterate over all files
for (String img:files) {
  cmd.run(op,img,resolver.createName(img));
}

个人理解:你不用每次都拼凑目标图片名字了,有很便利的方法实现。
  • %P: full pathname of source-image (i.e. the directory)
  • %p: last component of %P(用于相对路径?)
  • %F: full filename without directory part
  • %f: filename without directory part and extension
  • %e: only the extension
  • %D: drive-letter (on windows systems). Not available for source-files with an UNC-name.(盘符C:...)
这样就好理解 new FilenamePatternResolver("%P/%f.tif");了,就是绝对目录路径+文件名+tif


④、Debugging--窝不是类,只是一种操作方法

版本号>1.0,这个一般都是成立的。

IMOperation op = new IMOperation();
...
ConvertCmd cmd = new ConvertCmd();
cmd.createScript("myscript.sh",op);

没错,就是它了。把最后要执行的cmd+op备份到myscrip.sht脚本中。

在windows下,createScript()生成的脚本会自动加上 .bat 后缀


⑤、批量转换处理(适用于客户端程序,不适合web-application)

ExtensionFilter filter = new ExtensionFilter("jpg");
filter.setRecursion(false);
FilenameLoader loader =  new FilenameLoader(filter);
List<String> images=loader.loadFilenames(dir);
	

After you have the list, you create your BatchConverter and use it's run()-method to process the images:

// create a simple thumbnail operation
op = new IMOperation();
op.size(80);
op.addImage();     // placeholder input filename
op.thumbnail(80);
op.addImage();     // placeholder output filename

// create a template for the output-files:
// we put them in targetDir with the same filename as the original
// images
String template=targetDir+"%F";

// create instance of BatchConverter and convert images-----看这里,baby go
BatchConverter bc = new BatchConverter(BatchConverter.Mode.PARALLEL);
bc.run(op,images,targetDir+"%F");

BatchConverter有三种执行模式:BatchConverter.SEQUENTIALBatchConverter.PARALLEL,BatchConverter.BATCH。

分别为顺序处理,并行处理(CPU多核),批处理(单核)


最后,还是推荐大家去看下官方英文文档,本人水平有限,本着交流的精神,所以才发帖献丑一番,主要是为了记录自己的学习过程。

原文地址:https://www.cnblogs.com/ycpanda/p/3637257.html