use of org.im4java.core.ConvertCmd in project quick-media by liuyueyi.
the class ImgBaseOperate method operate.
/**
* 执行图片的复合操作
*
* @param operates
* @param sourceFilename 原始图片名
* @param outputFilename 生成图片名
* @return
* @throws ImgOperateException
*/
public static boolean operate(List<ImgWrapper.Builder.Operate> operates, String sourceFilename, String outputFilename) throws ImgOperateException {
try {
IMOperation op = new IMOperation();
boolean operateTag = false;
String waterFilename = null;
for (ImgWrapper.Builder.Operate operate : operates) {
if (!operate.valid()) {
continue;
}
if (operate.getOperateType() == ImgWrapper.Builder.OperateType.CROP) {
op.crop(operate.getWidth(), operate.getHeight(), operate.getX(), operate.getY());
// if (operate.getRadio() != null && Math.abs(operate.getRadio() - 1.0) > 0.005) {
// // 需要对图片进行缩放
// op.resize((int) Math.ceil(operate.getWidth() * operate.getRadio()));
// }
operateTag = true;
} else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.ROTATE) {
// fixme 180度旋转后裁图,会出现bug, 先这么兼容
double rotate = operate.getRotate();
if (Math.abs((rotate % 360) - 180) <= 0.005) {
rotate += 0.01;
}
op.rotate(rotate);
operateTag = true;
} else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.SCALE) {
if (operate.getRadio() == null) {
if (operate.isForceScale()) {
// 强制根据给定的参数进行压缩时
StringBuilder builder = new StringBuilder();
builder.append("!").append(operate.getWidth() == null ? "" : operate.getWidth()).append("x");
builder.append(operate.getHeight() == null ? "" : operate.getHeight());
op.addRawArgs("-resize", builder.toString());
} else {
op.resize(operate.getWidth(), operate.getHeight());
}
} else if (Math.abs(operate.getRadio() - 1) > 0.005) {
// 对图片进行比例缩放
op.addRawArgs("-resize", "%" + (operate.getRadio() * 100));
}
if (operate.getQuality() != null && operate.getQuality() > 0) {
op.quality(operate.getQuality().doubleValue());
}
operateTag = true;
} else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.FLIP) {
op.flip();
operateTag = true;
} else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.FLOP) {
op.flop();
operateTag = true;
} else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.WATER && waterFilename == null) {
// 当前只支持添加一次水印
op.geometry(operate.getWidth(), operate.getHeight(), operate.getX(), operate.getY()).composite();
waterFilename = operate.getWaterFilename();
operateTag = true;
} else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.BOARD) {
op.border(operate.getWidth(), operate.getHeight()).bordercolor(operate.getColor());
operateTag = true;
}
}
if (!operateTag) {
throw new ImgOperateException("operate illegal! operates: " + operates);
}
op.addImage(sourceFilename);
if (waterFilename != null) {
op.addImage(waterFilename);
}
op.addImage(outputFilename);
/**
* 传true到构造函数中,则表示使用GraphicMagic, 裁图时,图片大小会变
*/
ConvertCmd convert = new ConvertCmd();
convert.run(op);
} catch (IOException e) {
log.error("file read error!, e: {}", e);
return false;
} catch (InterruptedException e) {
log.error("interrupt exception! e: {}", e);
return false;
} catch (IM4JavaException e) {
log.error("im4java exception! e: {}", e);
return false;
}
return true;
}
use of org.im4java.core.ConvertCmd in project entando-core by entando.
the class ImageResource method saveResizedImage.
/**
* Redim images using im4Java
* @param bean
* @param dimension
* @param mimeType
* @param baseDiskFolder
* @throws ApsSystemException
*/
private void saveResizedImage(ResourceDataBean bean, ImageResourceDimension dimension) throws ApsSystemException {
if (dimension.getIdDim() == 0) {
// salta l'elemento con id zero che non va ridimensionato
return;
}
String imageName = this.getNewInstanceFileName(bean.getFileName(), dimension.getIdDim(), null);
String subPath = super.getDiskSubFolder() + imageName;
try {
this.getStorageManager().deleteFile(subPath, this.isProtectedResource());
ResourceInstance resizedInstance = new ResourceInstance();
resizedInstance.setSize(dimension.getIdDim());
resizedInstance.setFileName(imageName);
resizedInstance.setMimeType(bean.getMimeType());
String tempFilePath = System.getProperty("java.io.tmpdir") + File.separator + "temp_" + imageName;
File tempFile = new File(tempFilePath);
long realLength = tempFile.length() / 1000;
resizedInstance.setFileLength(String.valueOf(realLength) + " Kb");
this.addInstance(resizedInstance);
// create command
ConvertCmd convertCmd = new ConvertCmd();
// Is it a windows system?
if (this.isImageMagickWindows()) {
// yes so configure the path where ImagicMagick is installed
convertCmd.setSearchPath(this.getImageMagickPath());
}
// create the operation, add images and operators/options
IMOperation imOper = new IMOperation();
imOper.addImage();
imOper.resize(dimension.getDimx(), dimension.getDimy());
imOper.addImage();
convertCmd.run(imOper, bean.getFile().getAbsolutePath(), tempFile.getAbsolutePath());
this.getStorageManager().saveFile(subPath, this.isProtectedResource(), new FileInputStream(tempFile));
tempFile.delete();
} catch (Throwable t) {
_logger.error("Error creating resource file instance '{}'", subPath, t);
throw new ApsSystemException("Error creating resource file instance '" + subPath + "'", t);
}
}
use of org.im4java.core.ConvertCmd in project selenium_java by sergueik.
the class VisualTest method resizeImagesWithImageMagick.
public void resizeImagesWithImageMagick(String... pImageNames) throws Exception {
ConvertCmd cmd = new ConvertCmd();
cmd.setSearchPath(imageMagickPath);
IMOperation imOperation = new IMOperation();
imOperation.addImage();
imOperation.resize(200, 150);
imOperation.addImage();
for (String srcImage : pImageNames) {
String dstImage = srcImage.substring(0, srcImage.lastIndexOf('.') - 1) + "_small.jpg";
try {
System.err.println(String.format("Resized image: '%s'", dstImage));
cmd.run(imOperation, srcImage, dstImage);
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
throw ex;
} catch (IM4JavaException ex) {
System.err.println("Exception (ignored): " + ex.getClass());
ex.printStackTrace();
}
}
}
Aggregations