Creating Image using Java Without any Extra Libs
Creating Image using Java Without any Extra Libs: Images are more interactive way to present the contents rather than presenting everything in plain texts. Dynamic content images can be created using java without any extra libs /additional libraries. Creating dynamic images can be automated using the below java program.
Table of Contents
Requirement:
- No Additional/Extra Libraries.
- Need to give any background image as a input to this program. This program writes only the content over the image based on the x and y values mentioned.
What about the size of the image ?
As I explained above, image size will be same as the input image you passed to the program. This below java program is going to write just a content over the image whichever is defined.
Here for the example I have taken the image with 800*411 size one. If you are looking for any other size then take that size image and pass as a input to the below program.
Font, Font size can be customized ?
Yes. Below program provides the way to customize the font and font size as well.
Creating Image using Java Without any Extra Libs – Java Source Code:
Before running this program ensure you have changed the input and output paths and the image names respectively. For output image, name can be anything.
[java]
package in.javadomain;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class CreateImage {
public static void main(String[] args) {
Image img = new ImageIcon(“C:\\Users\\Naveen\\Pictures\\empty-img.png”)
.getImage();
BufferedImage bufferedImage = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Define the textcolor
Color textColor = Color.yellow;
// Graphics g = bufferedImage.createGraphics();
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(img, 0, 0, null);
// Setting the font and font size, 30 is font size
Font myFont1 = new Font(“Tahoma”, 1, 30);
g.setFont(myFont1);
g.drawString(“Creating image using java without any extra libs”, 50,
200);
// Setting the font and font size, 20 is font size
Font myFont2 = new Font(“Arial”, 1, 20);
g.setFont(myFont2);
g.setColor(textColor);
g.drawString(“Javadomain.in”, 630, 390);
g.dispose();
try {
ImageIO.write(bufferedImage, “png”, new File(
“C:\\Users\\Naveen\\Pictures\\content-img.png”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
[/java]
Input Image:
Output Image: