import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import javax
import dec
import dec
import dec
/**
*
* 純java API實現圖片處理
* <p>Title:ImageChangeSize</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c)
* <p>Company: </p>
* @author
* @version
*/
public class ImageChangeSize
{
public static void main ( String [] args )
throws InterruptedException
{
//測試生成縮略圖
createThumbnail(
//測試捕捉屏幕
String path =
int i =
for ( ;; )
{
i++;
String filepath = path + i +
snatchScreen(filepath);
Thread
}
}
/**
* 處理圖片
* @param filename 源文件名稱
* @param thumbWidth 處理後寬度
* @param thumbHeight 處理後高度
* @param quality 品質 最高
* @param outFilename 輸出文件名稱
*/
public static void createThumbnail ( String filename
{
Image image = Toolkit
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker
try
{
mediaTracker
double thumbRatio = (double) thumbWidth / (double) thumbHeight;
int imageWidth = image
int imageHeight = image
double imageRatio = (double) imageWidth / (double) imageHeight;
if ( thumbRatio < imageRatio )
{
thumbHeight = (int) ( thumbWidth / imageRatio );
}
else
{
thumbWidth = (int) ( thumbHeight * imageRatio );
}
BufferedImage thumbImage = new BufferedImage(thumbWidth
thumbHeight
Graphics
graphics
graphics
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));
JPEGImageEncoder encoder = JPEGCodec
JPEGEncodeParam param = encoder
quality = Math
param
encoder
encoder
out
}
catch ( Exception e )
{
e
}
}
/**
* 抓取電腦屏幕
* @param path
*/
public static void snatchScreen ( String path )
{
Robot robot;
try
{
robot = new Robot();
Dimension d = Toolkit
Rectangle rect = new Rectangle(
BufferedImage image = robot
ImageIO
}
catch ( Exception e )
{
System
e
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26013.html