Imgproc.line
Imgproc.circle
Imgproc.ellipse
ImgprocfillConvexPoly
Imgproc.fillPoly
Imgproc.putText
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; import javax.swing.JTextField; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class Artist extends JFrame { private int width; private int height; private int canvas_width; private int canvas_height; private boolean isFillPoly = false; private JLabel imageView; private Mat srcMat; private int imageType = 0; // 0点,1线段,2圆,3椭圆,4多边形,5文字,6矩形 private Point lineStartPoint; private Point lineEndPoint; private Point lineReleasePoint; private Point rectStartPoint; private Point rectEndPoint; private Point rectReleasePoint; private Point circleCenterPoint; private Point circleRadiusPoint; private Point circleReleasePoint; private Point ellipseCenterPoint; private Point ellipseRadiusPoint; private Point ellipseReleasePoint; private List<Point> polyPointList = new ArrayList<>(); public Artist(String path) { JRadioButton pointBtn = new JRadioButton("点"); pointBtn.setBounds(15, 10, 50, 25); pointBtn.setSelected(true); pointBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { imageType = 0; } }); JRadioButton rectBtn = new JRadioButton("矩形"); rectBtn.setBounds(15, 30, 70, 25); rectBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { imageType = 6; } }); JRadioButton lineBtn = new JRadioButton("线段"); lineBtn.setBounds(75, 10, 70, 25); lineBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { imageType = 1; } }); JRadioButton circleBtn = new JRadioButton("圆"); circleBtn.setBounds(155, 10, 50, 25); circleBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { imageType = 2; } }); JRadioButton ellipseBtn = new JRadioButton("椭圆"); ellipseBtn.setBounds(225, 10, 70, 25); ellipseBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { imageType = 3; } }); JRadioButton polyBtn = new JRadioButton("多边形"); polyBtn.setBounds(315, 10, 100, 25); polyBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { imageType = 4; } }); JRadioButton textBtn = new JRadioButton("文字"); textBtn.setBounds(415, 10, 70, 25); textBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { imageType = 5; } }); ButtonGroup group = new ButtonGroup(); group.add(pointBtn); group.add(rectBtn); group.add(lineBtn); group.add(circleBtn); group.add(ellipseBtn); group.add(polyBtn); group.add(textBtn); JCheckBox fillPolyBtn = new JCheckBox("填充"); fillPolyBtn.setBounds(315, 30, 60, 25); fillPolyBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { isFillPoly = !isFillPoly; } }); JTextField textField = new JTextField("ABCabc"); textField.setBounds(415, 32, 70, 25); int bar_height = 70; srcMat = Imgcodecs.imread(path); canvas_width = srcMat.width(); canvas_height = srcMat.height(); width = canvas_width; height = canvas_height + bar_height; imageView = new JLabel(); imageView.setBounds(0, bar_height, canvas_width, canvas_height); imageView.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); Point point = new Point(x, y); if (1 == imageType) { // 线 lineStartPoint = point; drawLine(); } else if (2 == imageType) { // 圆 circleCenterPoint = point; drawCircle(); } else if (3 == imageType) { // 椭圆 ellipseCenterPoint = point; drawEllipse(); } else if (6 == imageType) { // 矩形 rectStartPoint = point; drawRect(); } } public void mouseReleased(MouseEvent e) { int x = e.getX(); int y = e.getY(); Point point = new Point(x, y); if (1 == imageType) { // 线 lineReleasePoint = point; drawLine(); } else if (2 == imageType) { // 圆 circleReleasePoint = point; drawCircle(); } else if (3 == imageType) { // 椭圆 ellipseReleasePoint = point; drawEllipse(); } else if (6 == imageType) { // 矩形 rectReleasePoint = point; drawRect(); } } @Override public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); Point point = new Point(x, y); if (0 == imageType) { // 点 drawPoint(point); } else if (4 == imageType) { // 多边形 if (MouseEvent.BUTTON1 == e.getButton()) { // 左键 polyPointList.add(point); drawPoly(false); } else if (MouseEvent.BUTTON3 == e.getButton()) { // 右键 drawPoly(true); } } else if (5 == imageType) { // 文字 drawText(point, textField.getText()); } } }); imageView.addMouseMotionListener(new MouseAdapter() { @Override public void mouseDragged(MouseEvent e) { int x = e.getX(); int y = e.getY(); Point point = new Point(x, y); if (1 == imageType) { // 线 lineEndPoint = point; drawLine(); } else if (2 == imageType) { // 圆 circleRadiusPoint = point; drawCircle(); } else if (3 == imageType) { // 椭圆 ellipseRadiusPoint = point; drawEllipse(); } else if (6 == imageType) { // 矩形 rectEndPoint = point; drawRect(); } } }); this.setTitle("画图"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(width, height + bar_height); this.getContentPane().setLayout(null); this.getContentPane().add(pointBtn); this.getContentPane().add(rectBtn); this.getContentPane().add(lineBtn); this.getContentPane().add(circleBtn); this.getContentPane().add(ellipseBtn); this.getContentPane().add(polyBtn); this.getContentPane().add(fillPolyBtn); this.getContentPane().add(textBtn); this.getContentPane().add(textField); this.getContentPane().add(imageView); setIcon(srcMat); } private void drawPoint(Point point) { Mat canvas = srcMat.clone(); Imgproc.line(canvas, // 画布 point, // 起点 point, // 终点 new Scalar(0, 0, 0), // BGR颜色 10); // 线条粗细 setIcon(canvas); } private void drawLine() { Mat canvas = srcMat.clone(); if (null != lineStartPoint) { Imgproc.line(canvas, lineStartPoint, lineStartPoint, new Scalar(0, 0, 0), 10); } if (null != lineEndPoint) { Imgproc.line(canvas, lineStartPoint, lineEndPoint, new Scalar(0, 0, 0), 10); } if (null != lineReleasePoint) { Imgproc.line(canvas, lineStartPoint, lineReleasePoint, new Scalar(0, 0, 0), 10); lineStartPoint = null; lineEndPoint = null; lineReleasePoint = null; } setIcon(canvas); } private void drawCircle() { Mat canvas = srcMat.clone(); if (null != circleCenterPoint) { Imgproc.line(canvas, circleCenterPoint, circleCenterPoint, new Scalar(255, 125, 255), 8); } if (null != circleRadiusPoint) { circle(canvas, circleCenterPoint, circleRadiusPoint, 8); } if (null != circleReleasePoint) { circle(canvas, circleCenterPoint, circleReleasePoint, -1); circleCenterPoint = null; circleRadiusPoint = null; circleReleasePoint = null; } } private void circle(Mat canvas, Point p1, Point p2, int thickness) { double radius = getDiffOfPoint(p1, p2); Imgproc.circle(canvas, // 画布 circleCenterPoint, // 圆心 (int) radius, // 半径 new Scalar(255, 125, 255), // 颜色 thickness); // 边框粗细。-1为填充 setIcon(canvas); } private void drawEllipse() { Mat canvas = srcMat.clone(); Scalar color = new Scalar(255, 255, 125); if (null != ellipseCenterPoint) { Imgproc.line(canvas, ellipseCenterPoint, ellipseCenterPoint, color, 8); } if (null != ellipseRadiusPoint) { ellipse(canvas, ellipseCenterPoint, ellipseRadiusPoint, 2); } if (null != ellipseReleasePoint) { ellipse(canvas, ellipseCenterPoint, ellipseReleasePoint, -1); ellipseCenterPoint = null; ellipseRadiusPoint = null; ellipseReleasePoint = null; } } private void ellipse(Mat canvas, Point p1, Point p2, int thickness) { double p1x = p1.x; double p1y = p1.y; double p2x = p2.x; double p2y = p2.y; double w = Math.abs(p2x - p1x); double h = Math.abs(p2y - p1y); double theta = Math.atan2(w, h); // 弧度 double angle = Math.toDegrees(theta); // 角度 Imgproc.ellipse(canvas, // 画布 ellipseCenterPoint, // 圆心 new Size(w, h), // 椭圆的2个半径,即一个矩形的宽高。相等时为正圆 angle, // 旋转角度。圆心水平向右为0度,顺时针旋转为正。 0, // 圆弧开始绘制位置的角度 360, // 圆弧结束绘制位置的角度 new Scalar(0, 0, 0), // 颜色 thickness); // 边框粗细。-1为填充 setIcon(canvas); } private void drawPoly(boolean close) { Mat canvas = srcMat.clone(); if (polyPointList.size() < 2) { drawPoint(polyPointList.get(0)); return; } else { MatOfPoint mp = new MatOfPoint(); mp.fromList(polyPointList); List<MatOfPoint> pts = new ArrayList<>(); pts.add(mp); if (isFillPoly) { // 填充效果 // Imgproc.fillConvexPoly(canvas, mp, new Scalar(256, 64, 128)); // 只填充凸多边形。 Imgproc.fillPoly(canvas, pts, new Scalar(256, 64, 128)); // 填充任意形状的图型 } else { // 线框效果 Imgproc.polylines(canvas, pts, close, new Scalar(256, 64, 128), 6); } } setIcon(canvas); if (close) { polyPointList.clear(); } } private double getDiffOfPoint(Point p1, Point p2) { double p1x = p1.x; double p1y = p1.y; double p2x = p2.x; double p2y = p2.y; double xdiff = p1x - p2x; double ydiff = p1y - p2y; return Math.sqrt(Math.abs(xdiff * xdiff + ydiff * ydiff)); } private void drawText(Point point, String text) { Mat canvas = srcMat.clone(); int fontScale = 1; int fontFace = 0; // 0-CV_FONT_HERSHEY_SIMPLEX – 正常大小无衬线字体。 // 1-CV_FONT_HERSHEY_PLAIN – 小号无衬线字体。 // 2-CV_FONT_HERSHEY_DUPLEX – 正常大小无衬线字体。( 比CV_FONT_HERSHEY_SIMPLEX更复杂) // 3-CV_FONT_HERSHEY_COMPLEX – 正常大小有衬线字体。 // 4-CV_FONT_HERSHEY_TRIPLEX – 正常大小有衬线字体 ( 比CV_FONT_HERSHEY_COMPLEX更复杂) // 5-CV_FONT_HERSHEY_COMPLEX_SMALL – CV_FONT_HERSHEY_COMPLEX 的小译本。 // 6-CV_FONT_HERSHEY_SCRIPT_SIMPLEX – 手写风格字体。 // 7-CV_FONT_HERSHEY_SCRIPT_COMPLEX – 比CV_FONT_HERSHEY_SCRIPT_SIMPLEX更复杂。 Imgproc.putText(canvas, text, point, fontFace, fontScale, new Scalar(255, 64, 0)); // 不支持中文 setIcon(canvas); if (1 == 0) return; BufferedImage image = CVUtil.matToBufferedImage(srcMat); Graphics2D graphics = image.createGraphics(); graphics.setColor(Color.BLUE); // 文字颜色 graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.8f)); // 透明度。0完全透明,1不透明 graphics.setFont(new Font("黑体", Font.BOLD, 20)); // 字体,大小 graphics.drawString("汉语文字", (int) point.x, (int) point.y); // 文字和位置 graphics.dispose(); imageView.setIcon(new ImageIcon(image)); } private void drawRect() { Mat canvas = srcMat.clone(); if (null != rectStartPoint) { Imgproc.line(canvas, rectStartPoint, rectStartPoint, new Scalar(0, 128, 0), 5); } if (null != rectEndPoint) { Imgproc.rectangle(canvas, rectStartPoint, rectEndPoint, new Scalar(0, 128, 0), 5); } if (null != rectReleasePoint) { Imgproc.rectangle(canvas, rectStartPoint, rectReleasePoint, new Scalar(0, 128, 0), -1); // -1为填充 rectStartPoint = null; rectEndPoint = null; rectReleasePoint = null; } setIcon(canvas); } private void setIcon(Mat mat) { BufferedImage image = CVUtil.matToBufferedImage(mat); // CVUtil 见 http://www.gaohaiyan.com/3229.html imageView.setIcon(new ImageIcon(image)); } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3300.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设