【问题标题】:How to create a circular bufferedimage rather than creating a rectangular one in Java using Graphics如何使用图形在 Java 中创建圆形缓冲图像而不是创建矩形图像
【发布时间】:2014-05-15 04:35:37
【问题描述】:

我需要创建一个缓冲图像 (圆形类型)但我只能创建矩形类型。然后我想用一些配置在缓冲图像内创建一个椭圆,最后我想在圆形内绘制一个矩形图标,该图标应该插入圆形而不是矩形缓冲图像上。 现在我可以执行以下操作了

BufferedImage img = "SomeImage.png";
BufferedImage bfImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics       = bfImage.createGraphics();
graphics.fillOval(0, 0, 200, 200); 
graphics.drawImage(img, 0, 0, null);

(这会在 bfImage 对象内创建一个圆形椭圆) 现在我需要画出“img”,它是一个 100*100 大小的矩形。 这个我可以用 当我这样做时,我的最终图像被绘制在矩形 BfImage 中,这是我不想要的。我希望将图像“img”绘制在圆形椭圆形中,并且它不应超出圆形椭圆形的边界。 简而言之,不是在矩形 bfImage 上绘制我的最终图像,我可以有一个圆形 bfImage,我可以在其上直接绘制我的图像。 使用图形在 Java2D 中执行此操作的任何逻辑。

【问题讨论】:

    标签: graphics bufferedimage java-2d


    【解决方案1】:

    我从未遇到过不是矩形形式的二维数组意义上的“圆形图像”。如果您只关心圆圈外的像素不可见,只需将这些像素的 alpha 设置为 0。一个简单的方法是先用 ARGB(0,0,0,0) 填充整个矩形图像,然后绘制任何你想要的。

    此外,如果您打算将此缓冲区保存为图像文件,则必须确保导出/保存为支持透明度的格式,如 PNG 或 TIFF。

    【讨论】:

      【解决方案2】:

      正如@justinzane 所说,您不可能拥有真正的圆形图像。所有BufferedImages 都是矩形的。

      但是:您可以通过使用AlphaComposite 类和规则AlphaComposite.SrcIn 来实现您想要的效果。我添加了一个完整运行的示例,以及下面的屏幕截图,compose 方法是重要部分。

      public class AlphaCompositeTest {
          private static BufferedImage compose(final BufferedImage source, int w, int h) {
              BufferedImage destination = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
              Graphics2D graphics = destination.createGraphics();
              try {
                  graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                  graphics.setColor(Color.BLACK); // The color here doesn't really matter
                  graphics.fillOval(0, 0, destination.getWidth(), destination.getHeight());
      
                  if (source != null) {
                      graphics.setComposite(AlphaComposite.SrcIn); // Only paint inside the oval from now on
                      graphics.drawImage(source, 0, 0, null);
                  }
              }
              finally {
                  graphics.dispose();
              }
      
              return destination;
          }
      
          public static void main(String[] args) throws IOException {
              final BufferedImage original = ImageIO.read(new File("lena.png"));
              final BufferedImage template = compose(null, original.getWidth(), original.getHeight());
              final BufferedImage composed = compose(original, original.getWidth(), original.getHeight());
      
              SwingUtilities.invokeLater(new Runnable() {
                  @Override public void run() {
                      JFrame frame = new JFrame();
                      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      
                      JPanel panel = new JPanel(new BorderLayout(10, 10));
                      panel.add(new JLabel("+", new ImageIcon(template), SwingConstants.LEFT), BorderLayout.WEST);
                      panel.add(new JLabel("=", new ImageIcon(original), SwingConstants.LEFT), BorderLayout.CENTER);
                      panel.add(new JLabel(new ImageIcon(composed)), BorderLayout.EAST);
      
                      frame.add(new JScrollPane(panel));
      
                      frame.pack();
                      frame.setLocationRelativeTo(null);
                      frame.setVisible(true);
                  }
              });
          }
      }
      

      请参阅the Compositing tutorial 了解更多信息和示例。

      【讨论】:

        猜你喜欢
        • 2020-01-09
        • 2018-11-29
        • 1970-01-01
        • 1970-01-01
        • 2014-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-13
        相关资源
        最近更新 更多