1

I have a buffered image from byte array. How do I make the image into a circle? Crop? I don't want a circle, I want the orginal image to become circle shape and display

 def bufferedImage = imgSvc.convertByteArrayToBufferedImage(crop.image)
user903772
  • 1,504
  • 5
  • 31
  • 52
  • I won't post this as an answer, seeing as it might not be the best solution, but using the radius as a measurement for Pythagoras and copying the pixel images to another image for all in range of the radius could work, it might be sloppy. You would have to use a BufferedType.TYPE_ARGB. To include alpha for transparency of course. –  Feb 06 '13 at 16:38

3 Answers3

4

If bufferedImage is squared, then with this code :

int width = bufferedImage.getWidth();
BufferedImage circleBuffer = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = circleBuffer.createGraphics();
g2.setClip(new Ellipse2D.Float(0, 0, width, width));
g2.drawImage(bufferedImage, 0, 0, width, width, null);

you get a circular cropped image in circleBuffer

johanvs
  • 4,093
  • 3
  • 23
  • 26
1

this can help

    g.setClip(new Ellipse2D.Float(x, y, w, h));
    g.drawImage(yourBufferedImage, x, y, w, h, null);
Rahul
  • 271
  • 2
  • 7
0

You can use setClip() method of the Graphics class to restrict the drawing area of a graphics context to a specific region. The drawback of this is that this clipping will not be anti-aliased.

There are some more advanced tricks to achieve a better-looking result, see the answers to the following questions:

Drawing a part of an image (Java)

How to make a rounded corner image in Java

Community
  • 1
  • 1
lbalazscs
  • 16,984
  • 7
  • 41
  • 48