如何通过Java编辑jpg图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1009607/
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
How can I edit a jpg image through Java?
提问by andandandand
I have loaded a jpg image in which I want to draw letters and circles, given a x,y coordinate.
我已经加载了一个 jpg 图像,我想在其中绘制字母和圆圈,给定 ax,y 坐标。
I have been trying to figure out the paintIcon of the ImageIconclass
我一直在试图找出ImageIcon类的paintIcon
public void paintIcon(Component c,
Graphics g,
int x,
int y)
Does this method allow me to edit jpg images the way I want to? What are supposd to be the Component c and Graphics g paramethers? What would I add to its body to paint circles or letters?
这种方法是否允许我以我想要的方式编辑 jpg 图像?什么是组件 c 和图形 g 参数?我会在它的身体上添加什么来绘制圆圈或字母?
I'm working on Netbeans 6.5, do I have anything builtin for this task (instead of ImageIcon)?
我正在研究 Netbeans 6.5,我是否有针对此任务的内置内容(而不是 ImageIcon)?
采纳答案by Michael Myers
The pure-Java way is to use ImageIO
to loadthe image as a BufferedImage
. Then you can call createGraphics()
to get a Graphics2D
object; you can then draw whatever you want onto the image.
纯Java的方法是使用ImageIO
到加载的图像作为BufferedImage
。然后就可以调用createGraphics()
获取一个Graphics2D
对象;然后你可以在图像上绘制任何你想要的东西。
You can use an ImageIcon
embedded in a JLabel
to do the displaying, and you can add a MouseListener
and/or a MouseMotionListener
to the JLabel
if you're trying to allow the user to edit the image.
您可以使用ImageIcon
嵌入在 a 中的 aJLabel
进行显示,如果您试图允许用户编辑图像,则可以将 aMouseListener
和/或 a添加到 。MouseMotionListener
JLabel
回答by Pablo Santa Cruz
回答by James Black
I have used Java Advanced Imaging library (http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html), but you can also look at ImageJ (http://rsbweb.nih.gov/ij/index.html)
我使用过 Java 高级成像库 ( http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html),但您也可以查看 ImageJ ( http://rsbweb.nih.gov /ij/index.html)
回答by Steve Skrla
I imagen you could use this method to overlay the elements you need every time the image is drawn in the UI (this would happen numerous times as you are not drawing ON the image data its self) but may be suitable to your purposes (and advantageous if the overlay changes over time).
我想象你可以使用这种方法在每次在 UI 中绘制图像时覆盖你需要的元素(这会发生很多次,因为你没有在图像数据上绘制它自己)但可能适合你的目的(并且有利)如果叠加层随时间变化)。
Something like:
就像是:
new ImageIcon("someUrl.png"){
public void paintIcon(Component c, Graphics g, int x, int y) {
super(c, g, x, y);
g.translate(x, y);
g.drawOval(0, 0, 10, 10);
...
g.translate(-x, -y);
}
};
Having said that, mmyers' answer is much better if you want to modify the image data.
话虽如此,如果您想修改图像数据,mmyers 的答案要好得多。
回答by coobird
Manipulating images in Java can be achieved by using the Graphics
or Graphics2D
contexts.
在 Java 中操作图像可以通过使用Graphics
或Graphics2D
上下文来实现。
Loading images such as JPEG and PNG can be performed by using the ImageIO
class. The ImageIO.read
method takes in a File
to read in and returns a BufferedImage
, which can be used to manipulate the image via its Graphics2D
(or the Graphics
, its superclass) context.
可以使用ImageIO
该类执行加载 JPEG 和 PNG 等图像。该ImageIO.read
方法接受 aFile
来读入并返回 a BufferedImage
,它可用于通过其Graphics2D
(或其Graphics
超类)上下文来操作图像。
The Graphics2D
context can be used to perform many image drawing and manipulation tasks. For information and examples, the Trail: 2D Graphicsof The Java Tutorialswould be a very good start.
该Graphics2D
上下文可以用于执行多种图像绘制和操作任务。有关信息和示例,Trail: 2D Graphicsof The Java Tutorials将是一个非常好的开始。
Following is a simplified example (untested) which will open a JPEG file, and draw some circles and lines (exceptions are ignored):
以下是一个简化的示例(未经测试),它将打开一个 JPEG 文件,并绘制一些圆圈和线条(忽略例外):
// Open a JPEG file, load into a BufferedImage.
BufferedImage img = ImageIO.read(new File("image.jpg"));
// Obtain the Graphics2D context associated with the BufferedImage.
Graphics2D g = img.createGraphics();
// Draw on the BufferedImage via the graphics context.
int x = 10;
int y = 10;
int width = 10;
int height = 10;
g.drawOval(x, y, width, height);
g.drawLine(0, 0, 50, 50);
// Clean up -- dispose the graphics context that was created.
g.dispose();
The above code will open an JPEG image, and draw an oval and a line. Once these operations are performed to manipulate the image, the BufferedImage
can be handled like any other Image
, as it is a subclass of Image
.
上面的代码将打开一个 JPEG 图像,并绘制一个椭圆和一条线。一旦执行这些操作来操作图像,BufferedImage
就可以像处理任何其他Image
一样处理,因为它是 的子类Image
。
For example, by creating an ImageIcon
using the BufferedImage
, one can embed the image into a JButton
or JLabel
:
例如,通过创建ImageIcon
using BufferedImage
,可以将图像嵌入到JButton
或 中JLabel
:
JLabel l = new JLabel("Label with image", new ImageIcon(img));
JButton b = new JButton("Button with image", new ImageIcon(img));
The JLabel
and JButton
both have constructors which take in an ImageIcon
, so that can be an easy way to add an image to a Swing component.
在JLabel
和JButton
都有哪些需要在构造函数ImageIcon
,这样可以将图像添加到一个Swing组件的简单方法。