Write result image using jmagick

905 views Asked by At

I want to crop part of the image and then write the result to another image file. My code looks like this

ImageInfo imageInfo = new ImageInfo("file path");
MagickImage image = new MagickImage(imageInfo );

Rectangle cropInfo = new Rectangle();
cropInfo.x = 20;
cropInfo.y = 20;
cropInfo.width = 300;
cropInfo.height = 300;

MagickImage result = image.cropImage(cropInfo);

result.setFileName("path to result file");
boolean s = result.writeImage(imageInfo);

The above code just works but why writeImage using the old ImageInfo? and the MagickImage.setFileName don't make sense to me. I think we should create a new ImageInfo object and then write to that ImageInfo. The following code make more sense but don't work as expected.

MagickImage result = image.cropImage(cropInfo);
ImageInfo resulInfo = new ImageInfo("path to new file");
boolean s = result.writeImage(imageInfo);

Does anyone experience with this ?

1

There are 1 answers

0
dslamnig On

It was inconvenient for me to keep the original ImageInfo around, so I tried this and it also worked:

result.setFileName("path to result file");
boolean s = result.writeImage(new ImageInfo());

As to why the parameter is needed at all, the mystery remains. Null will not work.