0

I am using the im4Java lib to resize my images. I see you can set up a template operation which has place holder for the input and output files. I know you can initially set the resize dimensions for the operation, but can one set up the operation that the resize dimensions is also dynamic?

This is my current code:

   IMOperation op = new IMOperation();
   op.addImage();
   op.resize(500, 500); // I would like to make this dynamic
   op.quality(70d);
   op.addImage();
Jeffrey
  • 1,068
  • 2
  • 15
  • 25

1 Answers1

0

You can implement DynamicOperation interface and add it to your operation stack.

Sample Use Case:

Need to resize given image to all other image size in the set of image sizes.

for example:
Set1 :[ 16x16, 32x32, 64x64],
Set2 : [12x12, 24x24, 48x48].

If input image is of size 32x32, it need to be resized to 16x16, 64x64.

Code:

...

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;
    }
}
Lijeesh
  • 36
  • 2