- 相關推薦
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實現縮略圖和抓屏幕】相關文章:
Java語言的特點和實現機制10-01
實現鼠標畫圖的Java程序06-22
Java多線程的實現方式07-08
Java動態代理實現AOP的方法07-22
Java如何實現簡單的whois查詢09-21
Java byte[]轉int如何實現05-24
使用JavaScript實現Java的List功能10-26
java實現web服務器的方法10-14
Java和PHP的區別08-21