如何通过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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:13:44  来源:igfitidea点击:

How can I edit a jpg image through Java?

javaimagenetbeans

提问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 ImageIOto loadthe image as a BufferedImage. Then you can call createGraphics()to get a Graphics2Dobject; you can then draw whatever you want onto the image.

纯Java的方法是使用ImageIO加载的图像作为BufferedImage。然后就可以调用createGraphics()获取一个Graphics2D对象;然后你可以在图像上绘制任何你想要的东西。

You can use an ImageIconembedded in a JLabelto do the displaying, and you can add a MouseListenerand/or a MouseMotionListenerto the JLabelif you're trying to allow the user to edit the image.

您可以使用ImageIcon嵌入在 a 中的 aJLabel进行显示,如果您试图允许用户编辑图像,则可以将 aMouseListener和/或 a添加到 。MouseMotionListenerJLabel

回答by Pablo Santa Cruz

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

使用库来做到这一点。您可以尝试的一种是JMagick

回答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 Graphicsor Graphics2Dcontexts.

在 Java 中操作图像可以通过使用GraphicsGraphics2D上下文来实现。

Loading images such as JPEG and PNG can be performed by using the ImageIOclass. The ImageIO.readmethod takes in a Fileto 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 Graphics2Dcontext 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 BufferedImagecan be handled like any other Image, as it is a subclass of Image.

上面的代码将打开一个 JPEG 图像,并绘制一个椭圆和一条线。一旦执行这些操作来操作图像,BufferedImage就可以像处理任何其他Image一样处理,因为它是 的子类Image

For example, by creating an ImageIconusing the BufferedImage, one can embed the image into a JButtonor JLabel:

例如,通过创建ImageIconusing BufferedImage,可以将图像嵌入到JButton或 中JLabel

JLabel l = new JLabel("Label with image", new ImageIcon(img));
JButton b = new JButton("Button with image", new ImageIcon(img));

The JLabeland JButtonboth have constructors which take in an ImageIcon, so that can be an easy way to add an image to a Swing component.

JLabelJButton都有哪些需要在构造函数ImageIcon,这样可以将图像添加到一个Swing组件的简单方法。