天天看點

關于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支援上百種格式而且還支援批量加水印,去水印,批量生成縮略圖等很多功能。

繼續閱讀