Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write result image using jmagick

Tags:

java

jmagick

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 ?

like image 229
Hieu Lam Avatar asked Mar 24 '26 04:03

Hieu Lam


1 Answers

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.

like image 198
dslamnig Avatar answered Mar 28 '26 03:03

dslamnig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!