目前我使用的是 Graphics2d 做的。但是怎么生成都有难看的锯齿? github 上也没找到相应的库,可能是我搜索的关键词不对。求大佬指点下
![]() | 1 guolaopi 2018-12-28 16:00:07 +08:00 菜鸡给个思路。图片上画个圆,然后把圆里面的像素内容啥的拷到一个新的图片里保存 |
2 NEETLEE 2018-12-28 16:53:02 +08:00 ![]() 如果你要将图片展示在类 web 项目可以用另一种思路:css 将 img 标签弄成圆的,图片只转换成固定的大小 |
3 luman 2018-12-28 16:54:42 +08:00 像素设高点试试 |
![]() | 4 zmlu 2018-12-28 16:56:03 +08:00 OpenCV |
![]() | 5 redtea 2018-12-28 17:04:22 +08:00 via iPhone 放到阿里云 OSS |
![]() | 6 swordne 2018-12-28 17:16:51 +08:00 如果是 web 项目,交给前端去做就好了,一个 border-radius 的事。 如果不是的话,有点烦。 |
7 DsuineGP 2018-12-28 17:25:18 +08:00 之前有个差不多的需求..Graphics2d 生成的图片感觉都是带锯齿的..跟产品说没办法,只能这么着了 |
8 yrom 2018-12-28 17:36:41 +08:00 |
![]() | 9 samples OP |
10 jzmws 2018-12-28 17:51:37 +08:00 Graphics2D 可以做到抗锯齿 |
11 yzpure 2018-12-28 19:03:57 +08:00 随便写了下,ARGB 带透明效果,输出 png 图片也会比 jpg 大不少,实时生成图片比较耗时的,访问量大的话要注意 ----------------------代码分割线------------------------- public static void main(String[] args) throws IOException { String str = "这里是图片链接"; URL url = new URL(str); BufferedImage srcImage = ImageIO.read(url); int width = srcImage.getWidth(); int height = srcImage.getHeight(); BufferedImage dstImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = dstImage.createGraphics(); TexturePaint texturePaint = new TexturePaint(srcImage, new Rectangle2D.Float(0, 0, width, height)); g2.setPaint(texturePaint); Ellipse2D.Float ellipse = new Ellipse2D.Float(0, 0, width, height); // 抗锯齿 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.fill(ellipse); g2.dispose(); // write to file ImageIO.write(dstImage, "png", new File("portrait.png")); // to bytes or inputStream 略 } |