正好寫了一個,給你看看哈
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站建設、成都做網(wǎng)站、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務安鄉(xiāng),十年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220
import?java.awt.*;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.util.Date;
import?javax.imageio.ImageIO;
public?class?ScreenCapturer?{
public?static?void?main(String[]?args)?throws?Exception{
Date?date?=?new?Date();
Robot?rbt?=?new?Robot();
BufferedImage?bf?=??rbt.createScreenCapture(new?Rectangle(1440,900));??//這是屏幕分辨率??可以根據(jù)自己的屏幕修改
File?file?=?new?File("d://"+?date.toString().replace("?","").replace(":","")+".jpg");??//這是保存路徑D盤根目錄
ImageIO.write(bf,"jpg",file);
System.out.println("截圖成功!保存于D盤根目錄下!時間:"?+?date.toString());
}
}
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.ImageIcon;
public class ChartGraphics {
BufferedImage image;
void createImage(String fileLocation) {
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void graphicsGeneration(String name, String id, String classname, String imgurl) {
int imageWidth = 500;//圖片的寬度
int imageHeight = 400;//圖片的高度
image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imageWidth, imageHeight);
graphics.setColor(Color.BLACK);
graphics.drawString("姓名 : " + name, 50, 75);
graphics.drawString("學號 : " + id, 50, 150);
graphics.drawString("班級 : " + classname, 50, 225);
//ImageIcon imageIcon = new ImageIcon(imgurl);
//graphics.drawImage(imageIcon.getImage(), 230, 0, null);
//改成這樣:
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
} catch (Exception e) {}
if(bimg!=null)
graphics.drawImage(bimg, 230, 0, null);
graphics.dispose();
createImage("G:\\TDDOWNLOAD\\hehe.jpg");
}
public static void main(String[] args) {
ChartGraphics cg = new ChartGraphics();
try {
cg.graphicsGeneration("ewew", "1", "12", "C:/Documents and Settings/Administrator/My Documents/My Pictures/7.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//defaultSuffix是jpg
public static final boolean resizeImage(String fileName, String suffix) throws Exception {
boolean uploaded = false;
BufferedImage input;
if(suffix.equalsIgnoreCase("tif")||suffix.equalsIgnoreCase("tiff")||suffix.equalsIgnoreCase("png")) {
RenderedImage image = JAI.create("fileload", TurbineServlet.getRealPath(imageRoot + fileName+"."+suffix));
WritableRaster raster = image.copyData(null);
BufferedImage bi = new BufferedImage( image.getColorModel(), raster, true, null);
BufferedImage bi2 = new BufferedImage( maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setBackground(java.awt.Color.WHITE);//把tiff和png轉(zhuǎn)換后的背景設置為白色
g2.fillRect(0, 0, maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth());
g2.drawImage(bi, 0, 0, maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth(), null);
PlanarImage pi = PlanarImage.wrapRenderedImage(bi2);
JAI.create("FileStore", pi, TurbineServlet.getRealPath(resizeRoot + fileName+"."+defaultSuffix).replaceAll("\\\\","\\\\\\\\"), "JPEG", new JPEGEncodeParam());
input = pi.getAsBufferedImage();
int w = maxThumbDimension, h = maxThumbDimension;
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = output.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input,0,0,w,h, null);
ImageIO.write(output, defaultSuffix, new File(TurbineServlet.getRealPath(thumbRoot + fileName+"."+defaultSuffix)));
}
else {
input = ImageIO.read(new File(TurbineServlet.getRealPath(imageRoot + fileName+"."+suffix)));
int w = maxThumbDimension, h = maxThumbDimension;
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = output.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input,0,0,w,h, null);
ImageIO.write(output, defaultSuffix, new File(TurbineServlet.getRealPath(thumbRoot + fileName+"."+defaultSuffix)));
BufferedImage output2 = new BufferedImage(maxResizeDimension, input.getHeight()*maxResizeDimension/input.getWidth(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2 = output2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(input,0,0,maxResizeDimension, input.getHeight()*maxResizeDimension/input.getWidth(), null);
ImageIO.write(output2, defaultSuffix, new File(TurbineServlet.getRealPath(resizeRoot + fileName+"."+defaultSuffix)));
}
uploaded = true;
return uploaded;
}
用java代碼模擬一張圖片可以這樣操作:1.創(chuàng)建BufferedImage類
2.根據(jù)BufferedImage類得到一個Graphics2D對象
3.根據(jù)Graphics2D對象進行邏輯操作
4.處理繪圖
5.將繪制好的圖片寫入到圖片
1.調(diào)用"images"文件夾里的文件放在ImageIcon里顯示2.在windows下面顯示正常Java在圖形處理時調(diào)用了本地的圖形處理庫。在利用Java作圖形處理(比如:圖片縮放,圖片簽名,生成報表)時,如果運行在windows上不會出問題。如果將程序移植到Linux/Unix上的時候有可能出現(xiàn)圖形不能顯示的錯誤。3.提示信息:"Can'tconnecttoX11windowserver"這是由于Linux的圖形處理需要一個XServer服務器。(關(guān)于Linux的圖形處理模式參考Tomcat的修改如下:打開%Tomcat_home%/bin/catalina.sh文件在文件的86%處,啟動Tomcat的腳本中加上:-Djava.awt.headless=true/參考:"$_RUNJAVA"$JAVA_OPTS$CATALINA_OPTS/-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS"-classpath"$CLASSPATH"/-Dcatalina.base="$CATALINA_BASE"/-Dcatalina.home="$CATALINA_HOME"/-Djava.io.tmpdir="$CATALINA_TMPDIR"/-Djava.awt.headless=true/org.apache.catalina.startup.Bootstrap"$@"start/"$CATALINA_BASE"/logs/catalina.out21if[!-z"$CATALINA_PID"];thenecho$!$CATALINA_PIDfifielif["$1"="stop"];then保存后,重新啟動就可以處理圖形了。