熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用java生成縮略圖和給圖上打上公司logo

2022-06-13   來源: Java核心技術 

  這是我前兩天寫的生成圖片的縮略圖和給圖片打上logo的java代碼沒有怎麼整理就放上來了希望能夠為大家在工作中幫點忙同時寫的有什麼不對的地方希望大家指點指點

  import javaawtGraphicsD;

  import javaawtgeomAffineTransform;

  import javaawtimageAffineTransformOp;

  import javaawtimageBufferedImage;

  import javaioFile;

  import javaioFileInputStream;

  import javaioFileNotFoundException;

  import javaioFileOutputStream;

  import javaioIOException;

  import javaawtColor;

  //生成縮略圖和給圖上打上公司logo

  public static void main(String[] args) throws IOException {

  //源文件必須是有文件

  String pathS=D:/tdeng/My Pictures/showPhotogif;

  //目標文件

  String pathD=D:/temp/showPhotominigif;

  //拷貝文件

  copyfile(pathSpathD);

  //

  //生成縮小mimi圖

  File stadimgfile = new File(pathD);

  //圖片緩存

  BufferedImage img = ImageIOread(stadimgfile);

  //得到圖片的寬和高

  double width = imggetWidth();

  double height = imggetHeight();

  int miniwidth = ;//縮略圖寬度

  int miniheight = ;//縮略圖高度

  double ratew = miniwidth / width;

  double rateh = miniheight / height;

  //獲得適合的縮放比率即以在規定縮略尺寸中完整顯示圖片內容的同時又保證最大的縮放比率

  double rate = Mathmin(ratew rateh);

  rate = (Mathrint((rate * ) + )) / ;

  BufferedImage imgmini = new javaawtimageBufferedImage(miniwidth miniheightBufferedImageTYPE_USHORT__RGB);

  GraphicsD gmini = imgminicreateGraphics();

  gminisetBackground(ColorWHITE);

  gminiclearRect( miniwidth miniheight);

  AffineTransform trans = new AffineTransform();

  transscale(rate rate);

  AffineTransformOp op = new AffineTransformOp(trans AffineTransformOpTYPE_BILINEAR);

  gminidrawImage(img op (int) (miniwidth (width * rate)) / (int) (miniheight (height * rate)) / );

  ImageIOwrite(imgmini jpg stadimgfile);

  //

  //mimi圖加水印

  BufferedImage img = ImageIOread(stadimgfile);

  int mimi_width = imggetWidth();

  int mimi_height = imggetHeight();

  BufferedImage imgmimi = new javaawtimageBufferedImage(mimi_width mimi_height

  BufferedImageTYPE_USHORT__RGB);

  //logo文件的位置必須是真是的

  String logoFile = D:/tdeng/My Pictures/logogif;

  BufferedImage watermark = ImageIOread(new File(logoFile));

  GraphicsD gmimi = imgmimicreateGraphics();

  gmimidrawImage(img null );

  gmimidrawImage(watermark null mimi_width watermarkgetWidth() mimi_height watermarkgetHeight());

  ImageIOwrite(imgmimi jpg stadimgfile);

  }

  //    使用FileInputStream和FileOuStream

  public static void copyfile(String pathSrcString pathDest) throws IOException

  {

  FileInputStream fi=new FileInputStream(pathSrc);

  FileOutputStream fo=new FileOutputStream(pathDest);

  byte data[]=new byte[fiavailable()];

  firead(data);

  fowrite(data);

  ficlose();

  foclose();

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26025.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.