java 在 BufferedImage 上绘制填充矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11634867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing Filled Rectangle over a BufferedImage
提问by John Fox
So I am attempting to create an application that can black-out sections of a survey that contains sensitive information. However I've run into a bit of a problem.
因此,我正在尝试创建一个应用程序,该应用程序可以屏蔽包含敏感信息的调查部分。但是,我遇到了一些问题。
What I want to do is draw filled black rectangles over a BufferedImage given x, y, width, and height of desired region to black out, then write that new image back to my filesystem. Here's my code.
我想要做的是在给定 x、y、宽度和高度的 BufferedImage 上绘制填充的黑色矩形以将所需区域涂黑,然后将该新图像写回我的文件系统。这是我的代码。
File imageFile = new File("images/template.jpg");
BufferedImage img = ImageIO.read(imageFile);
Graphics2D graph = img.createGraphics();
graph.setColor(Color.BLACK);
graph.fill(new Rectangle(x, y, width, height));
graph.dispose();
ImageIO.write(img, "jpg", new File("images/template.jpg"));
For whatever reason the image in the resource doesn't change after this code segment. Any ideas on what I'm doing wrong?
无论出于何种原因,资源中的图像在此代码段之后都不会更改。关于我做错了什么的任何想法?
回答by Gilbert Le Blanc
I created a runnable example of your code, and it worked fine for me. I ran this code using Java 8.
我为您的代码创建了一个可运行的示例,它对我来说效果很好。我使用 Java 8 运行了这段代码。
Here's the altered image. I drew the black square on an image I had.
这是更改后的图像。我在我拥有的图像上画了黑色方块。
And here's the code I ran. I read the original image directly from my file system.
这是我运行的代码。我直接从我的文件系统中读取原始图像。
package com.ggl.testing;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageProcessing implements Runnable {
public static void main(String[] args) {
new ImageProcessing().run();
}
@Override
public void run() {
File imageFile = new File("C:\Users\Owner\Pictures\Saved Pictures\Analog Clock Calendar.jpg");
BufferedImage img;
try {
img = ImageIO.read(imageFile);
} catch (IOException e1) {
e1.printStackTrace();
return;
}
Graphics2D graph = img.createGraphics();
graph.setColor(Color.BLACK);
graph.fill(new Rectangle(100, 100, 100, 100));
graph.dispose();
try {
ImageIO.write(img, "jpg",
new File("altered.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
My conclusion is that you either didn't read the image correctly, your x, y, width, and/or height were outside the limits of the image, or something else that I'm missing.
我的结论是你要么没有正确阅读图像,你的 x、y、宽度和/或高度超出了图像的限制,或者我遗漏的其他东西。
回答by Nelson Macieira
Very late to this answer but you are saving the image and not the graph you are creating. I think it must be a BufferedImage again to save
这个答案很晚,但您正在保存图像而不是您正在创建的图形。我认为它必须再次是 BufferedImage 才能保存
回答by Lucarnosky
I know is an old question, but maybe it can be useful to someone, I think you shoud use this
我知道这是一个老问题,但也许它对某人有用,我认为你应该使用这个
graph.drawImage(x,y,width,height); //First you draw the image
graph.setColor(Color.black); //Then set the color to black
graph.fillRect(img.getX(), img.getY(), img.getWidth(), img.getHeight());// Finally draw a black rectangle on it
By the way is hard to find a solution without a little more code. Hope it will be usefull.
顺便说一句,如果没有更多代码,很难找到解决方案。希望它会很有用。
回答by Billal dz
You have just to replace this line:
您只需替换此行:
Graphics2D graph = img.createGraphics();
with this:
有了这个:
Graphics2D graph = img.getGraphics();