- It appears there are no convenience methods for both
-resample
and -layers
options.
The only thing in JMagick's API docs that resembles any of those options is the method sampleImage
in class MagickImage
. However, that operates only in pixels. There is indeed a setUnits
method that allows you to change the units declared in the header of the image file. But that's about it. It doesn't modify the image itself. And there seems to be no connection between the sampleImage
and setUnits
methods.
There is some code out there to resample an image using "manual" calculations. The following snippet is based on the one available here:
MagickImage lightImg = new MagickImage (new ImageInfo (strOrigPath));
//Get the original resolution
double origXRes = lightImg.getXResolution();
double origYRes = lightImg.getYResolution();
//Get present dimensions
int w = (int)lightImg.getDimension().getWidth();
int h = (int)lightImg.getDimension().getHeight();
//Calculate new dimensions
double new_w = w / origXRes * newXRes;
double new_h = h / origYRes * newYRes;
//Scale image
lightImg =lightImg.scaleImage((int)new_w, (int)new_h);
//Update info on image file
lightImg.setFileName(strDestPath);
lightImg.setXResolution( newXRes);
lightImg.setYResolution(newYRes);
//Save image
lightImg.writeImage(new ImageInfo());
I'd suggest you try im4java instead. From the website:
JMagick is a thin JNI layer above the ImageMagick C-API. im4java in
contrast just generates the commandline for the ImageMagick commands
and passes the generated line to the selected IM-command (using the
java.lang.ProcessBuilder.start()-method).
So, whatever command option ImageMagick has, im4java should have a method for it. I've had a quick look at the API and there is indeed a resample
and a layers
method. Using them, your code would look something like this (based on example from here):
// create command
convertCmd cmd = new ConvertCmd();
// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.units("pixelsperinch");
op.addImage(strOrigPath);
op.resample(300, 300);
// execute the operation
cmd.run(op);
Hope this helps!