我正在使用im4java lib调整图像大小。我看到您可以设置一个模板操作,该模板操作具有用于输入和输出文件的位置持有人。我知道您最初可以为操作设置调整大小尺寸,但是一个人可以设置调整大小尺寸也是动态的操作吗?
这是我当前的代码:
IMOperation op = new IMOperation();
op.addImage();
op.resize(500, 500); // I would like to make this dynamic
op.quality(70d);
op.addImage();
您可以实现Dynamicoperation接口并将其添加到操作堆栈中。
样本用例:
需要在图像尺寸集中调整给定图像大小。
例如:
set1:[16x16,32x32,64x64],
set2:[12x12,24x24,48x48]。
如果输入图像的大小为32x32,则需要调整到16x16,64x64。
代码:
...
DynamicResizeOperation drOp = new DynamicResizeOperation();
IMOperation op = new IMOperation();
op.addImage(inputImagePath);
op.addDynamicOperation(drOp);
op.addImage(); //place holder for out put image
...
for (IMSize imSize : resizeList) {
//set the dynamic size to resize
drOp.setSize(imSize.getWidth(), imSize.getHeight());
...
class DynamicResizeOperation implements DynamicOperation {
private int height;
private int width;
public DynamicResizeOperation() {}
public void setSize(int width, int height) {
this.height = height;
this.width = width;
}
@Override
public Operation resolveOperation(Object... pImages) throws IM4JavaException {
//return the resize operation if and only if both height and width are non zero positive values
if (height > 0 && width > 0) {
IMOperation op = new IMOperation();
op.resize(width, height, "!");
return op;
}
return null;
}
}