简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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