Thêm text lên ảnh bằng phương thức putText() của lớp org.opencv.imgproc.Imgproc. Tham số:
- Mat: ảnh nguồn (sẽ bị sửa đổi).
- String: nội dung text.
- Point: vị trí text.
- Int: font (FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN...).
- Double: scale factor.
- Scalar: màu text (BGR).
- Int: độ dày nét.
Ví dụ minh họa
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 {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String file = "D:\\Images\\shapes.jpg";
Mat src = Imgcodecs.imread(file);
String text = "JavaFX 2D shapes";
Point position = new Point(170, 280);
Scalar color = new Scalar(0, 0, 255); // Đỏ (BGR)
int font = Imgproc.FONT_HERSHEY_SIMPLEX;
double scale = 1;
int thickness = 3;
// Thêm text
Imgproc.putText(src, text, position, font, scale, color, thickness);
HighGui.imshow("Adding Text", src);
HighGui.waitKey();
}
}
Hình ảnh đầu vào và kết quả
Input: 
Output: 