大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
正好写了一个,给你看看哈
创新互联公司主要从事成都网站建设、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务安乡,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务: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)); //这是屏幕分辨率 可以根据自己的屏幕修改
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转换后的背景设置为白色
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.创建BufferedImage类
2.根据BufferedImage类得到一个Graphics2D对象
3.根据Graphics2D对象进行逻辑操作
4.处理绘图
5.将绘制好的图片写入到图片
1.调用"images"文件夹里的文件放在ImageIcon里显示2.在windows下面显示正常Java在图形处理时调用了本地的图形处理库。在利用Java作图形处理(比如:图片缩放,图片签名,生成报表)时,如果运行在windows上不会出问题。如果将程序移植到Linux/Unix上的时候有可能出现图形不能显示的错误。3.提示信息:"Can'tconnecttoX11windowserver"这是由于Linux的图形处理需要一个XServer服务器。(关于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保存后,重新启动就可以处理图形了。