关于thumbnailator 和 ImageMagick图像处理

近期开发涉及到了图像处理的问题,以前做项目的时候对于图片的处理仅仅处理一些常见格式,大小的图片,然而这次遇到的tif,而且照片大小从几兆到几百兆不等,需要支持批量上传,批量下载,批量加水印,批量生成缩略图等。最开始使用的是thumbnailator-0.4.2。然而最终却出现这样那样的问题最终不得不放弃来使用ImageMagick来进行对图像处理。

thumbnailator:

 一开始遇到thumbnailator时觉得这个框架包真的是太方便了,里面有针对图片的各种处理,常见的操作有:裁剪、缩放、旋转、加水印等。

  <dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
 </dependency>

  裁剪:

public static boolean cut(String inImg, String outImg, Rectangle r) {
    boolean res;
    BufferedImage img1;
    BufferedImage imgNew;
    res = false;
    try {
      img = ImageIO.read(new File(inImg));
      imgNew = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
      imgNew.setRGB(0, 0, img.getWidth(), img.getHeight(), img.getRGB(0, 0, img.getWidth(), img.getHeight(), new int[img.getWidth() * img.getHeight()], 0, img.getWidth()), 0, img.getWidth());
      Thumbnails.of(img).size(img.getWidth(), img.getHeight()).sourceRegion(r).toFile(outImg);
      res = true;
    }
    catch (Exception ce) {
      logger.error("Error while cut image vertical", ce);
    }
    return res;

  }

旋转:

public static boolean rotate(String inImg,String outImg, Integer rotate) {
    try {
      
      File f = new File(inImg);
      BufferedImage bf = ImageIO.read(f);
      Thumbnails.of(f).size(bf .getWidth(), bf .getHeight()).rotate(rotate).toFile(outImg);
      return  true;
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return false;

  }

缩放:

public static boolean rotate(String inImg,String outImg, double ratio) {
    try {
      
      File f = new File(inImg);

      BufferedImage bf = ImageIO.read(f);

      Thumbnails.of(f).size(bf .getWidth(), bf .getHeight()).toFile(outImg);

      return  true;
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return false;

  }

加水印:

public static boolean rotate(String inImg,String outImg, String wImg) {
    try {
      
      File f = new File(inImg);

      BufferedImage bf = ImageIO.read(f);

      Thumbnails.of(f).size(bf .getWidth(), bf .getHeight()).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(wImg),0.8f))
).toFile(outImg);

      return  true;
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return false;

  }

thumbnailator 方法特别容易使用,但是当处理一些CMYK图片时,市场会出现色彩偏差,而且支持格式也有限。

ImageMagick


裁剪

/**
* 裁剪
* @param x
* @param y
* @param width
* @param height
* @param inImgPath
* @param outImgPath
* @return
*/
public static boolean imgCrop(String inImgPath,String outImgPath,int x,int y,int width,int height){
String convertCmd = String.format("convertx %s %s", inImgPath,width,"x",height ,"+",x,"+",y, inImgPath);
Process process;
boolean cropSuccess=false;
   try {
    process = Runtime.getRuntime().exec(convertCmd);
process.waitFor();
if(process.exitValue()==0){
 cropSuccess= true;
}
} catch (Exception e) {
e.printStackTrace();
cropSuccess= false;
}
return cropSuccess;

}

     旋转

public static boolean imgRotate(String inImgPath,String outImgPath,int rotate){
String convertCmd = String.format("convertx %s %s", inImgPath,"-rotate",rotate, inImgPath);
Process process;
boolean cropSuccess=false;
   try {
    process = Runtime.getRuntime().exec(convertCmd);
process.waitFor();
if(process.exitValue()==0){
 cropSuccess= true;
}
} catch (Exception e) {
e.printStackTrace();
cropSuccess= false;
}
return cropSuccess;

}

  生成缩略图

public static boolean imgSample(String inImgPath,String outImgPath,double ratio){
String convertCmd = String.format("convertx %s %s", inImgPath,"-sample",ratio, inImgPath);
Process process;
boolean cropSuccess=false;
   try {
    process = Runtime.getRuntime().exec(convertCmd);
process.waitFor();
if(process.exitValue()==0){
 cropSuccess= true;
}
} catch (Exception e) {
e.printStackTrace();
cropSuccess= false;
}
return cropSuccess;

}

  生成缩略图

public static boolean imgResize(String inImgPath,String outImgPath,double ratio){
String ratioStr=Integer.parseInt(new DecimalFormat("0").format(ratio*100))+"%";
String convertCmd = String.format("convertx %s %s %s %s", inImgPath,"-resize",ratioStr, outImgPath);
//convertCmd = String.format("convertx -sample  %s %s %s",ratio, inImgPath, outImgPath);
Process process;
boolean cropSuccess=false;
   try {
    process = Runtime.getRuntime().exec(convertCmd);
process.waitFor();
if(process.exitValue()==0){
 cropSuccess= true;
}
} catch (Exception e) {
e.printStackTrace();
cropSuccess= false;
}
   
return cropSuccess;

}

镜像

public static boolean imgflip(String inImgPath,String outImgPath,boolean isFlat){
String command=null;
if(isFlat){
command="-flop";
}else{
command="-flip";
}
String convertCmd = String.format("convertx %s %s %s", command,inImgPath, outImgPath);  
//convertCmd = String.format("convertx -sample  %s %s %s",ratio, inImgPath, outImgPath);
Process process;
boolean cropSuccess=false;
   try {
    process = Runtime.getRuntime().exec(convertCmd);
process.waitFor();
if(process.exitValue()==0){
 cropSuccess= true;
}
System.out.println(process.exitValue());
} catch (Exception e) {
e.printStackTrace();
cropSuccess= false;
}
   
return cropSuccess;

}

ImageMagick支持上百种格式而且还支持批量加水印,去水印,批量生成缩略图等很多功能。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值