- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to add text to an image using Java
Problem Description
How to add text to an image using Java.
Solution
Following is the program to add text to an image using 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
"Tutorialspoint", //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
Output
java_opencv
Advertisements