【问题标题】:How can I edit a jpg image through Java?如何通过 Java 编辑 jpg 图像?
【发布时间】:2010-11-03 19:43:24
【问题描述】:

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

我一直在尝试找出ImageIcon 类的paintIcon

public void paintIcon(Component c,
                      Graphics g,
                      int x,
                      int y)

这种方法是否允许我以我想要的方式编辑 jpg 图像? Component c 和 Graphics g 参数应该是什么?我会在它的身体上添加什么来画圆圈或字母?

我正在使用 Netbeans 6.5,我是否有任何内置功能(而不是 ImageIcon)?

【问题讨论】:

    标签: java image netbeans


    【解决方案1】:

    使用库来做到这一点。你可以试试JMagick

    【讨论】:

      【解决方案2】:

      我用过Java Advanced Imaging library (http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html),不过你也可以看看ImageJ (http://rsbweb.nih.gov/ij/index.html)

      【讨论】:

        【解决方案3】:

        纯Java方式是使用ImageIOload图像作为BufferedImage。然后就可以调用createGraphics()得到一个Graphics2D对象;然后你可以在图像上绘制任何你想要的东西。

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

        【讨论】:

          【解决方案4】:

          我想你可以使用这种方法来覆盖你每次在 UI 中绘制图像时需要的元素(这会发生很多次,因为你没有在图像数据上绘制它本身),但可能适合你的目的(如果叠加层随时间变化,则有利)。

          类似:

          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);
              }
          };
          

          话虽如此,如果您想修改图像数据,mmyers 的回答要好得多。

          【讨论】:

            【解决方案5】:

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

            可以使用ImageIO 类来加载JPEG 和PNG 等图像。 ImageIO.read 方法接受 File 来读取并返回 BufferedImage,它可用于通过其 Graphics2D(或 Graphics,它的超类)上下文来操作图像。

            Graphics2D 上下文可用于执行许多图像绘制和操作任务。有关信息和示例,The Java TutorialsTrail: 2D Graphics 将是一个非常好的开始。

            以下是一个简化的示例(未经测试),它将打开一个 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();
            

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

            例如,通过使用BufferedImage 创建ImageIcon,可以将图像嵌入JButtonJLabel

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

            JLabelJButton 都具有接受 ImageIcon 的构造函数,因此这是一种将图像添加到 Swing 组件的简单方法。

            【讨论】:

              猜你喜欢
              • 2014-07-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多