如何使用Java向图像添加文本。(How to add text to an image using Java.)

优质
小牛编辑
128浏览
2023-12-01

问题描述 (Problem Description)

如何使用Java向图像添加文本。

解决方案 (Solution)

以下是使用Java向图像添加文本的程序。

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class AddingTextToImage {
   public static void main(String args[]) {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      //Reading the Image from the file and storing it in to a Matrix object
      String file = "C:/opencv/logo.jpg";
      Mat matrix = Imgcodecs.imread(file);
      //Adding Text
      Imgproc.putText(matrix,        //Matrix obj of the image
         "xnip",           //Text to be added
         new Point(100, 390),        //point
         Core.FONT_HERSHEY_SIMPLEX , //front face
         1,                          //front scale
         new Scalar(0, 0, 0),        //Scalar object for color
         5);                         //Thickness
      Imgcodecs.imwrite("C:/opencv/addingTextOP.jpg", matrix);
      System.out.println("Image Processed");
   }
} 

输入 (Input)

OpenCV复制输入

输出 (Output)

添加了图像文字