简体   繁体   English

如何使用 java 将图像裁剪成圆形?

[英]How to crop an image into a circle with java?

I really need some help with that.我真的需要一些帮助。 I'm trying to crop an image into a circle and it's fine but the pixels outside the circle stay white.我正在尝试将图像裁剪成一个圆圈,这很好,但圆圈外的像素保持白色。 How can I put them transparent?我怎样才能让它们透明?

My code it's:我的代码是:

static ColorImage Circulo(ColorImage img, int radius) {
    for (int x=0; x < img.getWidth(); x++ ) {
            for(int y=0; y < img.getHeight(); y++) {
                if((x - (img.getWidth()/2)) * (x - (img.getWidth()/2)) + (y - (img.getHeight()/2) )* (y - (img.getHeight()/2)) <= (radius*radius)) {
                    img.setColor(x, y, img.getColor(x, y));
                }else {
                    Color c = new Color (255, 255, 255);
                    img.setColor(x, y, c );     
                }
             }
        }
        return img;
    }

Try this.尝试这个。 This will paint the image on the screen inside a circle.这将在屏幕上绘制一个圆圈内的图像。 If you want to create a new image, get the Graphics context from a BufferedImage and write the image to that instead of the graphics context in paintComponent .如果要创建新图像,请从BufferedImage获取 Graphics 上下文并将图像写入该图像而不是paintComponent中的图形上下文。 Any image format will work as this does not rely on any transparency mode of the graphics image.任何图像格式都可以使用,因为它不依赖于图形图像的任何透明度模式。

The main idea behind this is setting the clip region to that of a circle.这背后的主要思想是将clip region设置为圆形。 Then whatever you paint will only appear in that region.然后你画的任何东西都只会出现在那个区域。

In this example, I made the diameter of the circle the minimum of the width and height of the image.在此示例中,我将圆的直径设为图像宽度和高度的最小值。 This way, the entire circle will fit in a rectangle.这样,整个圆圈将适合一个矩形。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;



public class ImageInCircle extends JPanel {
    JFrame f = new JFrame();
    Image img;
    int width;
    int height;
    static String imgFile =
             "location/of/image/img.gif";

      

    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->new ImageInCircle().start());
          
    }
    @Override
    public Dimension getPreferredSize() {
         return new Dimension(width, height);
    }
    public ImageInCircle () {
        f.add(this);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    public void start() {
        try {
        img = ImageIO.read(new File(imgFile));
        } catch (IOException fne) {
            fne.printStackTrace();
        }
        width = img.getWidth(null);
        height = img.getHeight(null);
        revalidate();
        f.setVisible(true);
        f.pack();

        f.setLocationRelativeTo(null);
        repaint(); 
    }
    
    public void paintComponent(Graphics g) {
         super.paintComponent(g);
          setBackground(Color.white);
          Graphics2D g2 = (Graphics2D) g;

          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          int circleDiameter = Math.min(width,height);
          Ellipse2D.Double circle = new Ellipse2D.Double(0,0,circleDiameter,circleDiameter);
          g2.setClip(circle);
          g2.drawImage(img,0,0,this);
    }
        
      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM