JAVA认证 百分网手机站

java实现缩略图和抓屏幕

时间:2017-07-19 16:13:54 JAVA认证 我要投稿

java实现缩略图和抓屏幕

  在全球范围内,SUN公司的Java技术培训和认证不仅是Java开发技术领域最权威的认证,同时也是开发类认证中很重要的一种.以下是小编整理的关于java实现缩略图和抓屏幕,希望大家认真阅读!

  import java.awt.Container;

  import java.awt.Dimension;

  import java.awt.Graphics2D;

  import java.awt.Image;

  import java.awt.MediaTracker;

  import java.awt.Rectangle;

  import java.awt.RenderingHints;

  import java.awt.Robot;

  import java.awt.Toolkit;

  import java.awt.image.BufferedImage;

  import java.io.BufferedOutputStream;

  import java.io.File;

  import java.io.FileOutputStream;

  import javax.imageio.ImageIO;

  import com.sun.image.codec.jpeg.JPEGCodec;

  import com.sun.image.codec.jpeg.JPEGEncodeParam;

  import com.sun.image.codec.jpeg.JPEGImageEncoder;

  /**

  *

  * 纯java API实现图片处理

  *

  Title:ImageChangeSize

  *

  Description:

  *

  Copyright: Copyright (c) 2010

  *

  Company:

  * @author

  * @version 1.0.0

  */

  public class ImageChangeSize

  {

  public static void main ( String [] args )

  throws InterruptedException

  {

  //测试生成缩略图

  createThumbnail("c:\555.gif", 200, 200, 100, "c:\uuuu.jpg");

  //测试捕捉屏幕,每隔一秒捕获一次

  String path = "c:\screen\";

  int i = 0;

  for ( ;; )

  {

  i++;

  String filepath = path + i + ".jpg";

  snatchScreen(filepath);

  Thread.currentThread().sleep(2000);

  }

  }

  /**

  * 处理图片,改变起大小,可进行缩略和放大

  * @param filename 源文件名称

  * @param thumbWidth 处理后宽度

  * @param thumbHeight 处理后高度

  * @param quality 品质 最高100,最小0

  * @param outFilename 输出文件名称

  */

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

  {

  Image image = Toolkit.getDefaultToolkit().getImage(filename);

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

  mediaTracker.addImage(image, 0);

  try

  {

  mediaTracker.waitForID(0);

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

  int imageWidth = image.getWidth(null);

  int imageHeight = image.getHeight(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, BufferedImage.TYPE_INT_RGB);

  Graphics2D graphics2D = thumbImage.createGraphics();

  graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

  graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

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

  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

  JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

  quality = Math.max(0, Math.min(quality, 100));

  param.setQuality((float) quality / 100.0f, false);

  encoder.setJPEGEncodeParam(param);

  encoder.encode(thumbImage);

  out.close();

  }

  catch ( Exception e )

  {

  e.printStackTrace();

  }

  }

  /**

  * 抓取电脑屏幕

  * @param path

  */

  public static void snatchScreen ( String path )

  {

  Robot robot;

  try

  {

  robot = new Robot();

  Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

  Rectangle rect = new Rectangle(0, 0, d.width, d.height);

  BufferedImage image = robot.createScreenCapture(rect);

  ImageIO.write(image, "jpg", new File(path));

  }

  catch ( Exception e )

  {

  System.out.println("Failed to snatch screen ... ");

  e.printStackTrace();

  }

  }

  }

【java实现缩略图和抓屏幕】相关文章:

1.Java语言的特点和实现机制

2.Java中goto实现方法

3.Java多线程的实现方式

4.关于JAVA实现httpClient的实例

5.Java byte[]转int如何实现

6.实现鼠标画图的Java程序

7.使用JavaScript实现Java的List功能

8.Java编程中如何实现中文排序