PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

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

 July 27, 2022     crop, image, java, opencv     No comments   

Issue

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

Solution

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


Answered By - WJS
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing