如何使用 Java OpenCV 库将文本添加到图像?
可以使用 org.opencv.imgproc.Imgproc 类的 putText() 方法将文本添加到图像。此方法在给定的图像中呈现指定的文本。它接受−
一个空的 mat 对象来存储源图像。
一个字符串对象来指定所需的文本。
一个 Point 对象,指定文本的位置。
指定文本字体的整型常量。
对字体特定的基本大小相乘的比例因子。
一个指定文本颜色的 Scalar 对象。
指定文本颜色的整数值
示例
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:\Images\shapes.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();
}
}输入图像

输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP