คุณสามารถเพิ่มข้อความลงในรูปภาพได้โดยใช้ putText() วิธีการของคลาส org.opencv.imgproc.Imgproc . เมธอดนี้แสดงข้อความที่ระบุในภาพที่กำหนด ยอมรับ -
-
วัตถุเสื่อเปล่าสำหรับเก็บภาพต้นฉบับ
-
วัตถุสตริงเพื่อระบุข้อความที่ต้องการ
-
วัตถุชี้ระบุตำแหน่งของข้อความ
-
ค่าคงที่จำนวนเต็มระบุแบบอักษรของข้อความ
-
ตัวคูณสเกลที่คูณด้วยขนาดฐานเฉพาะฟอนต์
-
วัตถุสเกลาร์ที่ระบุสีของข้อความ
-
ค่าจำนวนเต็มที่ระบุสีของข้อความ
ตัวอย่าง
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();
}
} ใส่รูปภาพ

ผลลัพธ์
