51,409
社区成员
发帖
与我相关
我的任务
分享
public class Image {
public static void main(String[] args){
try {
//BufferedImage是Image的一个子类,方便我们操作图片,比如里面有很多有用的方法;如:获取图片长度、宽度
//1.从本地读取图片
BufferedImage image = ImageIO.read(new File("D:/test/a.jpg"));
//或从网络上读取图片
//BufferedImage image =ImageIO.read(new File("url"));
//2.替换颜色
changeImage(image);
//3.生成新的图片
//将图片写入另一文件,调用ImageIO的write方法:ImageIO.write(source,formatName,new File(Path));
//第一个参数是原图片,第二个参数是设置输出的图片格式,第三个参数是输出的路径
ImageIO.write(image, "jpg", new File("D:/test/a-change.jpg"));
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 替换颜色
*/
private static void changeImage(BufferedImage image) {
//获取图片的宽度
int width = image.getWidth();
//获取图片的长度
int height = image.getHeight();
//Graphics是java提供的一个用于绘图和显示表格格式化文字的一个工具,就是一个画笔
//Graphics中的setColor方法将文字、边框或要填充的区域为指定的颜色
Graphics graphics = image.getGraphics();
//设置颜色:setColor(new Color(int r, int g, int b));
//RGB颜色设置方法,RGB是红、绿、蓝三个颜色,每个颜色强度是0~255,由红、绿、蓝按照不同比例混合就能产生一种颜色,
graphics.setColor(getRandomColor());
//填充图片:fillRect(int x,int y, int width,int height);
//其中x代表距矩形左边缘的位置,y是代表距上边缘的位置,width代表图片的宽度,height代表高度
graphics.fillRect(0, 0, width, height);
//释放资源
graphics.dispose();
}
/**
* 获取随机颜色
*/
private static Color getRandomColor() {
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
return new Color(r, g, b);
}
}
你就不能排下版吗,这样乱糟糟的挤在一起你让人怎么看