- 相關推薦
java實現(xiàn)縮略圖和抓屏幕
在全球范圍內(nèi),SUN公司的Java技術(shù)培訓和認證不僅是Java開發(fā)技術(shù)領域最權(quán)威的認證,同時也是開發(fā)類認證中很重要的一種.以下是小編整理的關于java實現(xiàn)縮略圖和抓屏幕,希望大家認真閱讀!
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實現(xiàn)圖片處理
*
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 品質(zhì) 最高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實現(xiàn)縮略圖和抓屏幕】相關文章:
Java語言的特點和實現(xiàn)機制10-01
實現(xiàn)鼠標畫圖的Java程序06-22
Java多線程的實現(xiàn)方式07-08
Java動態(tài)代理實現(xiàn)AOP的方法07-22
Java byte[]轉(zhuǎn)int如何實現(xiàn)05-24
使用JavaScript實現(xiàn)Java的List功能10-26
Java認證輔導:Java實現(xiàn)二叉樹遍歷算法10-21
Java和PHP的區(qū)別08-21