java实现缩略图和抓屏幕
在全球范围内,SUN公司的Java技术培训和认证不仅是Java开发技术领域最权威的认证,同时也是开发类认证中很重要的一种.以下是小编整理的关于java实现缩略图和抓屏幕,希望大家认真阅读!
import ainer;
import nsion;
import hics2D;
import e;
import aTracker;
import angle;
import eringHints;
import t;
import kit;
import eredImage;
import eredOutputStream;
import ;
import OutputStream;
import eIO;
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);
entThread()p(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 = efaultToolkit()mage(filename);
MediaTracker mediaTracker = new MediaTracker(new Container());
mage(image, 0);
try
{
ForID(0);
double thumbRatio = (double) thumbWidth / (double) thumbHeight;
int imageWidth = idth(null);
int imageHeight = eight(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, _INT_RGB);
Graphics2D graphics2D = teGraphics();
enderingHint(_INTERPOLATION, E_INTERPOLATION_BILINEAR);
Image(image, 0, 0, thumbWidth, thumbHeight, null);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
quality = (0, (quality, 100));
uality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
de(thumbImage);
e();
}
catch ( Exception e )
{
tStackTrace();
}
}
/**
* 抓取电脑屏幕
* @param path
*/
public static void snatchScreen ( String path )
{
Robot robot;
try
{
robot = new Robot();
Dimension d = efaultToolkit()creenSize();
Rectangle rect = new Rectangle(0, 0, h, ht);
BufferedImage image = teScreenCapture(rect);
e(image, "jpg", new File(path));
}
catch ( Exception e )
{
tln("Failed to snatch screen ... ");
tStackTrace();
}
}
}