Bufferedimage into circle shape

ε祈祈猫儿з 提交于 2019-12-08 03:48:51

问题


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)

回答1:


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




回答2:


this can help

    g.setClip(new Ellipse2D.Float(x, y, w, h));
    g.drawImage(yourBufferedImage, x, y, w, h, null);



回答3:


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



来源:https://stackoverflow.com/questions/14731799/bufferedimage-into-circle-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!