如何使用Java OpenCV库向图像添加文本?

您可以使用 org.opencv.imgproc.Imgproc 类 的 putText() 方法向图像添加文本。此方法在给定图像中呈现指定文本。它接受 -

  • 一个用于存储源图像的空垫对象。

  • 一个要指定的字符串对象所需的文本。

  • 指定文本位置的 Point 对象。

  • 指定文本字体的整数常量.

  • 比例因子乘以特定于字体的基本尺寸。

  • 指定颜色的标量对象text。

  • 指定文本颜色的整数值

示例

import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class AddingText {    public static void main(String args[]) throws Exception {       //Loading the OpenCV core library       System.loadLibrary( Core.NATIVE_LIBRARY_NAME );       //Reading the contents of the image       String file ="D:Imagesshapes.jpg";       Mat src = Imgcodecs.imread(file);       //Preparing the arguments       String text = "JavaFX 2D shapes";       Point position = new Point(170, 280);       Scalar color = new Scalar(0, 0, 255);       int font = Imgproc.FONT_HERSHEY_SIMPLEX;       int scale = 1;       int thickness = 3;       //Adding text to the image       Imgproc.putText(src, text, position, font, scale, color, thickness);       //Displaying the resultant Image       HighGui.imshow("Contours operation", src);       HighGui.waitKey();    } }登录后复制