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

java實現縮略圖和抓屏幕

2013-11-23 18:56:40  來源: Java核心技術 

  import javaawtContainer;

  import javaawtDimension;

  import javaawtGraphicsD;

  import javaawtImage;

  import javaawtMediaTracker;

  import javaawtRectangle;

  import javaawtRenderingHints;

  import javaawtRobot;

  import javaawtToolkit;

  import javaawtimageBufferedImage;

  import javaioBufferedOutputStream;

  import javaioFile;

  import javaioFileOutputStream;

  import javaximageioImageIO;

  import decjpegJPEGCodec;

  import decjpegJPEGEncodeParam;

  import decjpegJPEGImageEncoder;

  /**

  *

  * 純java API實現圖片處理

  * <p>Title:ImageChangeSize</p>

  * <p>Description: </p>

  * <p>Copyright: Copyright (c) </p>

  * <p>Company: </p>

  * @author

  * @version

  */

  public class ImageChangeSize

  {

  public static void main ( String [] args )

  throws InterruptedException

  {

  //測試生成縮略圖

  createThumbnail(c:\\gif c:\\uuuujpg);

  //測試捕捉屏幕每隔一秒捕獲一次

  String path = c:\\screen\\;

  int i = ;

  for ( ;; )

  {

  i++;

  String filepath = path + i + jpg;

  snatchScreen(filepath);

  ThreadcurrentThread()sleep();

  }

  }

  /**

  * 處理圖片改變起大小可進行縮略和放大

  * @param filename 源文件名稱

  * @param thumbWidth  處理後寬度

  * @param thumbHeight 處理後高度

  * @param quality     品質 最高最小

  * @param outFilename  輸出文件名稱

  */

  public static void createThumbnail ( String filename int thumbWidth int thumbHeight int quality String outFilename )

  {

  Image image = ToolkitgetDefaultToolkit()getImage(filename);

  MediaTracker mediaTracker = new MediaTracker(new Container());

  mediaTrackeraddImage(image );

  try

  {

  mediaTrackerwaitForID();

  double thumbRatio = (double) thumbWidth / (double) thumbHeight;

  int imageWidth = imagegetWidth(null);

  int imageHeight = imagegetHeight(null);

  double imageRatio = (double) imageWidth / (double) imageHeight;

  if ( thumbRatio < imageRatio )

  {

  thumbHeight = (int) ( thumbWidth / imageRatio );

  }

  else

  {

  thumbWidth = (int) ( thumbHeight * imageRatio );

  }

  BufferedImage thumbImage = new BufferedImage(thumbWidth

  thumbHeight BufferedImageTYPE_INT_RGB);

  GraphicsD graphicsD = thumbImagecreateGraphics();

  graphicsDsetRenderingHint(RenderingHintsKEY_INTERPOLATION RenderingHintsVALUE_INTERPOLATION_BILINEAR);

  graphicsDdrawImage(image thumbWidth thumbHeight null);

  BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));

  JPEGImageEncoder encoder = JPEGCodeccreateJPEGEncoder(out);

  JPEGEncodeParam param = encodergetDefaultJPEGEncodeParam(thumbImage);

  quality = Mathmax( Mathmin(quality ));

  paramsetQuality((float) quality / f false);

  encodersetJPEGEncodeParam(param);

  encoderencode(thumbImage);

  outclose();

  }

  catch ( Exception e )

  {

  eprintStackTrace();

  }

  }

  /**

  * 抓取電腦屏幕

  * @param path

  */

  public static void snatchScreen ( String path )

  {

  Robot robot;

  try

  {

  robot = new Robot();

  Dimension d = ToolkitgetDefaultToolkit()getScreenSize();

  Rectangle rect = new Rectangle( dwidth dheight);

  BufferedImage image = robotcreateScreenCapture(rect);

  ImageIOwrite(image jpg new File(path));

  }

  catch ( Exception e )

  {

  Systemoutprintln(Failed to snatch screen );

  eprintStackTrace();

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26013.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.