如何通过Java编辑jpg图像?

时间:2022-11-25 15:13:56

I have loaded a jpg image in which I want to draw letters and circles, given a x,y coordinate.

我已经加载了一个jpg图像,我想要绘制字母和圆圈,给定一个x,y坐标。

I have been trying to figure out the paintIcon of the ImageIcon class

我一直在尝试找出ImageIcon类的画笔

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图像吗?什么是supposd作为组件c和图形g参数?我要给它的身体加些什么来画圆圈或字母?

I'm working on Netbeans 6.5, do I have anything builtin for this task (instead of ImageIcon)?

我正在做Netbeans 6.5,我有什么内置的任务(而不是ImageIcon)吗?

5 个解决方案

#1


16  

The pure-Java way is to use ImageIO to load the 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.

您可以使用嵌入在JLabel中的ImageIcon来进行显示,如果您想让用户编辑图像,您可以将MouseListener和/或MouseMotionListener添加到JLabel中。

#2


9  

Manipulating images in Java can be achieved by using the Graphics or Graphics2D contexts.

通过使用图形或Graphics2D上下文,可以实现Java中的图像操作。

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方法获取要读取的文件并返回BufferedImage,该文件可用于通过Graphics2D(或图形、超类)上下文操作图像。

The Graphics2D context can be used to perform many image drawing and manipulation tasks. For information and examples, the Trail: 2D Graphics of The Java Tutorials would be a very good start.

Graphics2D上下文可以用于执行许多图像绘制和操作任务。对于信息和示例,跟踪:Java教程的2D图形将是一个非常好的开始。

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,因为它是映像的子类。

For example, by creating an ImageIcon using the BufferedImage, one can embed the image into a JButton or JLabel:

例如,通过使用BufferedImage创建一个ImageIcon,可以将图像嵌入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组件添加图像。

#3


5  

Use a library to do just that. One that you might try is JMagick.

使用库来实现这一点。你可以试试JMagick。

#4


2  

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)

#5


1  

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的答案要好得多。

#1


16  

The pure-Java way is to use ImageIO to load the 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.

您可以使用嵌入在JLabel中的ImageIcon来进行显示,如果您想让用户编辑图像,您可以将MouseListener和/或MouseMotionListener添加到JLabel中。

#2


9  

Manipulating images in Java can be achieved by using the Graphics or Graphics2D contexts.

通过使用图形或Graphics2D上下文,可以实现Java中的图像操作。

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方法获取要读取的文件并返回BufferedImage,该文件可用于通过Graphics2D(或图形、超类)上下文操作图像。

The Graphics2D context can be used to perform many image drawing and manipulation tasks. For information and examples, the Trail: 2D Graphics of The Java Tutorials would be a very good start.

Graphics2D上下文可以用于执行许多图像绘制和操作任务。对于信息和示例,跟踪:Java教程的2D图形将是一个非常好的开始。

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,因为它是映像的子类。

For example, by creating an ImageIcon using the BufferedImage, one can embed the image into a JButton or JLabel:

例如,通过使用BufferedImage创建一个ImageIcon,可以将图像嵌入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组件添加图像。

#3


5  

Use a library to do just that. One that you might try is JMagick.

使用库来实现这一点。你可以试试JMagick。

#4


2  

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)

#5


1  

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的答案要好得多。