建立不同类型的图片并查看格式,这里不支持webp格式图片的建立。
可以建立 png jpg jpeg 格式。图片建立的思路:
(1) 先了解图片格式及尺寸
(2) 使用Bufferedimage类建立图像
(3) 使用ImageIo.write()进行图片的保存
public static void main(String[] args) throws IOException {
var i = new BufferedImage(800,600,1);
//建立 jpg格式
//ImageIO.write(i,"jpg",new File("a.jpg"));
//建立 png格式
//ImageIO.write(i,"png",new File("b.png"));
//建立 jpeg格式
// ImageIO.write(i,"jpeg",new File("c.jpeg"));
//输出图片
ImageIO.write(i,"jpg",new File("ok.jpg"));
//获取图片的长和宽
System.out.println(i.getWidth());
System.out.println(i.getHeight());
System.out.println(i.getData());
}
输出结果:
![](https://i-blog.csdnimg.cn/blog_migrate/95fd54f6bf74dad0361605093223544e.png)
将有色图片变为黑白图片,本质就是把图片的透明度改变
public static void main(String[] args) throws Exception {
//有色图片变为黑白图片
//首先获取原始图片
BufferedImage img = ImageIO.read(new File("D:/pic/456.png"));
//File img1= new File("D:/pic/456.png");
//获取文件的大小
System.out.println(img.getHeight());
System.out.println(img.getWidth());
//根据获得的图片大小建立新的画布
//BufferedImage.TYPE_BYTE_GRAY 把图片变为黑白并进行新的输出
var s = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
//建立画笔
var i = s.createGraphics();
i.drawImage(img,0,0,null);
i.dispose();
ImageIO.write(s,"png",new File("789.png"));
Runtime.getRuntime().exec("cmd /k start 789.png");
}
自动打开的命令:
Runtime.getRuntime().exec("cmd /k start https://baidu.com");
shutdown 命令的用法:
shutdown /s/t +时间,以秒为单位 实现定时关机
![](https://i-blog.csdnimg.cn/blog_migrate/533924c64131dc4c5543e5050cbab6f3.png)
水印文字的实现
//水印文字效果的实现
//找到原图片位置
var s = ImageIO.read(new File("D://pic//download//123.jpeg"));
System.out.println(s.getWidth());
System.out.println(s.getHeight());
int sw = s.getWidth();
int sh = s.getHeight();
//要进行创建,不要获取,创建一个新的画板
var g =s.createGraphics();
//设置线条和文件的抗锯齿(平滑)
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
//在新的画步写入当前时间上
var text = String.format("%tF %<tT",System.currentTimeMillis());
//获取外部字体文件
//var font = Font.createFont(Font.TYPE1_FONT,new File("华文行楷")).deriveFont(Font.BOLD,100f);
//使用系统的字体
Font font = new Font("华文楷体",Font.BOLD,36);
g.setFont(font);
//设置字体的颜色
Random rand= new Random();
var co = new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
g.setColor(co);
int tx = sw - g.getFontMetrics(font).stringWidth(text);
int ty = sh- 15;
g.drawString(text,tx,ty);
g.dispose();
ImageIO.write(s,"jpg", new File("shuiyin.jpg"));
Runtime.getRuntime().exec("cmd /k start shuiyin.jpg");
}
添加水印图标:
public static void main(String[] args) throws Exception {
//加载图标
//var logo = ImageIO.read(new File("d:/download/baidu.png"));
var logo = ImageIO.read(new File("d:/pic/shuiyin.jpg"));
var ww = logo.getWidth();
var hh = logo.getHeight();
//加载图像,添加水印图标效果
var i = ImageIO.read(new File("d:/pic/456.png"));
var w = i.getWidth();
var h = i.getHeight();
var g = i.createGraphics();
//设置线条和文件的,抗锯齿(平滑)
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
//水印图标坐标
int x = (w - ww) / 2+120;
int y = (h - hh) / 2+120;
//图标的透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, .3f));
//放大图标
//g.drawImage(logo, x,y, ww+330, hh+(int)(hh*330f/ww), null);
//缩小图标
g.drawImage(logo, x,y, ww-90, hh-90, null);
//正常大小水印图标
// g.drawImage(logo, x, y, ww, hh, null);
g.dispose();
ImageIO.write(i, "jpg", new File("g111.jpg"));
Runtime.getRuntime().exec("cmd /k start g111.jpg");
}