天天看点

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

近期开发涉及到了图像处理的问题,以前做项目的时候对于图片的处理仅仅处理一些常见格式,大小的图片,然而这次遇到的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

裁剪

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支持上百种格式而且还支持批量加水印,去水印,批量生成缩略图等很多功能。

继续阅读