开发中遇到一个需求,就是在抓拍的图片上按照点位画出有效区域,并且区域有正选和反选,所以需要填充多边形内和多边形外。

花了些时间看源码找资料,搞出了个demo

1、图片上绘制多边形区域并填充颜色

/*** 填充矩形(单色)* @param fromFile* @param x* @param y* @param toPath*/
public static void fillPolygon(String fromFile, int[] x, int[] y, String toPath) {try {BufferedImage from = ImageIO.read(new File(fromFile));Graphics2D g2d = from.createGraphics();g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);g2d.setColor(new Color(255, 255, 255, 100)); //设置颜色g2d.setStroke(new BasicStroke(5f)); //设置划线粗细Polygon polygon = new Polygon(x, y, 4);g2d.drawPolygon(polygon);g2d.fillPolygon(polygon);String toFile = toPath + System.currentTimeMillis() + ".jpg";FileOutputStream out = new FileOutputStream(toFile);ImageIO.write(from, "jpg", out);out.close();System.out.println("==fillPolygon==================" + toFile);} catch (IOException e) {e.printStackTrace();}
}

2、图片上绘制多边形区域,反向填充颜色

/*** 反向填充矩形(单色)* @param fromFile* @param x* @param y* @param toPath*/public static void reverseFillPolygon(String fromFile, int[] x, int[] y, String toPath) {try {BufferedImage img_from = ImageIO.read(new File(fromFile));//1、绘制裁剪矩形Polygon polygon = new Polygon(x, y, 4);//设置裁剪后的背景色Rectangle2D rectangle = new Rectangle(0,0,img_from.getWidth(),img_from.getHeight());TexturePaint tPaint = new TexturePaint(img_from,rectangle);BufferedImage img_clip = new BufferedImage(img_from.getWidth(), img_from.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);Graphics2D g2d_clip = img_clip.createGraphics();g2d_clip.setPaint(tPaint);g2d_clip.fillPolygon(polygon);g2d_clip.dispose();//2、将裁剪后的原图整个填充背景色Graphics2D g2d = img_from.createGraphics();g2d.setColor(new Color(255, 255, 255, 150)); //设置颜色g2d.fillRect(0, 0, img_from.getWidth(), img_from.getHeight()); //填充指定的矩形//3、将裁剪的矩形绘制到原图g2d.drawImage(img_clip, 0, 0, null);g2d.dispose();String toFile2 = toPath + System.currentTimeMillis() + ".jpg";FileOutputStream out2 = new FileOutputStream(toFile2);ImageIO.write(img_from, "jpg", out2);out2.close();System.out.println("==reverseFillPolygon==================" + toFile2);} catch (IOException e) {e.printStackTrace();}}

3、图片上绘制多边形区域,填充斜线

/*** 填充矩形(斜线)* @param fromFile* @param x* @param y* @param toPath*/public static void fillPolygonWithLine(String fromFile, int[] x, int[] y, String toPath) {try {BufferedImage from = ImageIO.read(new File(fromFile));Graphics2D g2d = from.createGraphics();g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);g2d.setColor(new Color(255, 255, 255, 200)); //设置颜色g2d.setStroke(new BasicStroke(1.5f)); //设置划线粗细Polygon polygon = new Polygon(x, y, 4);//裁切g2d.setClip(polygon);//取得多边形外接矩形Rectangle rect = polygon.getBounds();//绘制填充线ifor (int i = rect.y; i-rect.width < rect.y + rect.height; i = i + 6) {Line2D line = new Line2D.Float(rect.x, i, (rect.x + rect.width), i-rect.width);g2d.draw(line);}//绘制多边形g2d.drawPolygon(polygon);//输出绘制后的图片String toFile = toPath + System.currentTimeMillis() + ".jpg";FileOutputStream out = new FileOutputStream(toFile);ImageIO.write(from, "jpg", out);out.close();System.out.println("==fillPolygonWithLine==================" + toFile);} catch (IOException e) {e.printStackTrace();}}

4、运行结果

public static void main(String[] args) {String fromFile = "D:\\William\\Desktop" + File.separator + "draw\\44a64248a444_6_20230515191005_1.jpg";String toPath = "D:\\William\\Desktop" + File.separator + "draw\\";int[] xPoint = {474, 471, 963, 855};int[] yPoint = {711, 996, 1005, 612};fillPolygon(fromFile, xPoint, yPoint, toPath);reverseFillPolygon(fromFile, xPoint, yPoint, toPath);fillPolygonWithLine(fromFile, xPoint, yPoint, toPath);}

原图:

fillPolygon结果图:

reverseFillPolygon结果图:

fillPolygonWithLine结果图:

java使用Graphics在图片上绘制形状相关推荐

  1. 如何用Python在图片上绘制BoundingBox

    参考资料: https://blog.csdn.net/weixin_41735859/article/details/106599903 在目标检测等CV领域的任务里,经常会涉及到在图片上绘制BBo ...

  2. 使用ImageMagick 在图片上绘制粗斜体的中文也许是一个错误。

    测试发现: ImageMagick使用中文字体,在图片上绘制带粗或斜体的中文,看不到效果. 如果使用英文字体,绘制粗或斜体的英文,99%都有效果. 今天无意看到一篇文章提到: convert -lis ...

  3. java制作海报一:java使用Graphics2D 在图片上写字,文字换行算法详解

    文章目录 前言 一.直接上代码 1. 写字方法 2. 换行算法 二. 叙述换行算法 前言 代码都上传到GitHub了,这里仅仅是贴出来主要部分,GitHub传送门:https://github.com ...

  4. python在图片上绘制标注框

    前言 最近帮人跑代码的时候遇到了不少问题,后来发现他提供给我的数据集中标注文件不太准确,部分box框没有很好地框到物体.所以写了一个代码,通过在图片上绘制标注框来直观地判断标注文件是否存在问题,也可以 ...

  5. python 使用opencv在图片上绘制矩形、圆形以及中英文

    最近看了下python的第三方库opencv,在python中用来对图片进行一些简单处理还是非常好用的,比如在图形上画矩形框,圆框,指示线,椭圆以及文字等,只需要调用几个相应的函数,设置好起止坐标点以 ...

  6. 图片上绘制点阵汉字(C++)

    在Ubuntu下用C/C++(或python) 调用opencv库编程显示一张图片,并打开一个名为"logo.txt"的文本文件(其中只有一行文本文件,包括你自己的名字和学号),按 ...

  7. canvas在图片上绘制图形

    说明 在vue项目中,后台返回图片的url和矩形的顶点坐标(左上和右下),需要在图片上绘制矩形框,并在前端进行展示(一张张的播放图片). 其中返回的数据是多张图片的集合,前端也需要整合一个绘制后的图片 ...

  8. wangEditor Java富文本的图片上传

    上一篇 wangEditor的配置:https://blog.csdn.net/qq_35349982/article/details/103288508 这里主要讲 java后台 我这里用的是七牛云 ...

  9. 【OpenCV在图片上绘制点、圆(C++)】

    文章目录 前言 一.引入头文件 二.在图像上绘制圆.点 1.关键函数 2.举个例子 总结 前言 也没啥前言好说的,就是做项目时候要绘制点和圆,自己学会了就在这记录一下(其实是好久以前了,一直在草稿箱, ...

最新文章

  1. [转载] 别人的心得感悟
  2. 用ASP.NET AJAX 开发Web程序 — UpdatePanel篇
  3. WeChat报错微信小程序图片加载失败渲染层网络层错误Failed to load image /pages/index/image/index.jpg:用绝对路径不用相对路径
  4. JDK 8 新特性 之 Lambda表达式
  5. Nginx实现tomcat集群进行负载均衡
  6. 连载四:Oracle升级文章大全(完结篇)
  7. 基础才是重中之重~类是怎么执行的
  8. php极速链,php PHP极速链 战群源码系列(全自动采集更新+引流神器无数据库版) WEB(ASP,PHP,...) 261万源代码下载- www.pudn.com...
  9. python的质量控制模块_11. 语言学数据管理 - 2.2 质量控制 - 《Python 自然语言处理 第二版》 - 书栈网 · BookStack...
  10. 经典算法归纳(c语言)
  11. xampp使用教程方法免费分享
  12. 微信PC版多开的方法
  13. 四、vue 项目使用高德地图画面(多边形)
  14. MapReduce关系代数运算——差
  15. 如何手动制作透明的图片相框 可以供android使用
  16. C语言谁是凶手-思路详解
  17. 上市即亏损?谈谈爱奇艺和拼多多上市后的艰难人生
  18. hdu1151Air Raid poj2594Treasure Exploration题解
  19. 华为交换机 access,trunk,hybrid理解,需一点基础,应该是最完整的
  20. 中科院 鲁士文 计算机网络,《计算机网络-鲁士文》10_基于IP的多协议标记交换技术.pptx...

热门文章

  1. 头文件为什么要加#ifndef #define #endif
  2. PDF增强插件:PitStop Pro 2021 for Mac中文版
  3. 普元eos使用svn_普元DevOps给DevOps打上企业级最佳实践标签
  4. js oninput 移动端替代 keyup
  5. Sql Server datetime 常用日期格式转换
  6. Doris Routine Load正则表达实战
  7. Ubuntu — pac配置及谷歌pac无效的解决方案
  8. MySQL卸载、下载、安装、配置、目录结构、源码、登录、编码设置、可视化工具、MySQL的常见问题的解决
  9. multisim加法放大电路_运算放大器真的懂了吗?这里有一份内部构造及原理图详解...
  10. 这是我离马化腾最近的一次