- 相關(guān)推薦
java實(shí)現(xiàn)縮略圖和抓屏幕
在全球范圍內(nèi),SUN公司的Java技術(shù)培訓(xùn)和認(rèn)證不僅是Java開(kāi)發(fā)技術(shù)領(lǐng)域最權(quán)威的認(rèn)證,同時(shí)也是開(kāi)發(fā)類認(rèn)證中很重要的一種.以下是小編整理的關(guān)于java實(shí)現(xiàn)縮略圖和抓屏幕,希望大家認(rè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實(shí)現(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
{
//測(cè)試生成縮略圖
createThumbnail("c:\555.gif", 200, 200, 100, "c:\uuuu.jpg");
//測(cè)試捕捉屏幕,每隔一秒捕獲一次
String path = "c:\screen\";
int i = 0;
for ( ;; )
{
i++;
String filepath = path + i + ".jpg";
snatchScreen(filepath);
Thread.currentThread().sleep(2000);
}
}
/**
* 處理圖片,改變起大小,可進(jìn)行縮略和放大
* @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實(shí)現(xiàn)縮略圖和抓屏幕】相關(guān)文章:
Java語(yǔ)言的特點(diǎn)和實(shí)現(xiàn)機(jī)制10-01
實(shí)現(xiàn)鼠標(biāo)畫(huà)圖的Java程序06-22
Java動(dòng)態(tài)代理實(shí)現(xiàn)AOP的方法07-22
Java如何實(shí)現(xiàn)簡(jiǎn)單的whois查詢09-21
Java byte[]轉(zhuǎn)int如何實(shí)現(xiàn)05-24
使用JavaScript實(shí)現(xiàn)Java的List功能10-26
java實(shí)現(xiàn)web服務(wù)器的方法10-14
Java認(rèn)證輔導(dǎo):Java實(shí)現(xiàn)二叉樹(shù)遍歷算法10-21
Java和PHP的區(qū)別08-21