package com.lqh;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class PictureTest {
public static void main(String[] args) throws IOException {
new PictureTest().start();
}
public void start() throws IOException {
ImageIcon icon = new ImageIcon("D:\\8.jpg");
Image image = icon.getImage(); //得到文件对应的Image对象
//实现缩略功能
/*
* 构造一个预定义图像类型的BufferedImage对象
* 创建Graphics2D对象,用于在BufferedImage对象上绘图(即绘需缩小的Image对象)
*/
BufferedImage bi = new BufferedImage(320, 240, BufferedImage.TYPE_INT_BGR);
Graphics2D g = bi.createGraphics();
Color c = g.getColor();
g.setColor(Color.white);
g.fillRect(0, 0, 320, 240);
g.drawImage(image, 0, 0, 320, 240, null);
g.dispose(); //释放图形上下文使用的系统资源
//实现水印功能
/*
* 获取Graphics对象
* 在图片上绘制文字
*/
Graphics gg = bi.getGraphics();
Font font = new Font("lqh", Font.BOLD, 48);
gg.setFont(font);
gg.setColor(Color.black);
gg.drawString("我是中国人", 10, 200);
gg.dispose();
//输出图片
File output = new File("D:\\hehe.jpg");
output.createNewFile();
ImageIO.write(bi, "jpg", output);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class PictureTest {
public static void main(String[] args) throws IOException {
new PictureTest().start();
}
public void start() throws IOException {
ImageIcon icon = new ImageIcon("D:\\8.jpg");
Image image = icon.getImage(); //得到文件对应的Image对象
//实现缩略功能
/*
* 构造一个预定义图像类型的BufferedImage对象
* 创建Graphics2D对象,用于在BufferedImage对象上绘图(即绘需缩小的Image对象)
*/
BufferedImage bi = new BufferedImage(320, 240, BufferedImage.TYPE_INT_BGR);
Graphics2D g = bi.createGraphics();
Color c = g.getColor();
g.setColor(Color.white);
g.fillRect(0, 0, 320, 240);
g.drawImage(image, 0, 0, 320, 240, null);
g.dispose(); //释放图形上下文使用的系统资源
//实现水印功能
/*
* 获取Graphics对象
* 在图片上绘制文字
*/
Graphics gg = bi.getGraphics();
Font font = new Font("lqh", Font.BOLD, 48);
gg.setFont(font);
gg.setColor(Color.black);
gg.drawString("我是中国人", 10, 200);
gg.dispose();
//输出图片
File output = new File("D:\\hehe.jpg");
output.createNewFile();
ImageIO.write(bi, "jpg", output);
}
}