How to add text to an image in java? - java

I need to add some texts to an existing table image (png).
Which means that I need to "write" on the image and I need the option to select the text location.
How can I do it?

It's easy, just get the Graphics object from the image and draw your string onto the image. This example (and output image) is doing that:
public static void main(String[] args) throws Exception {
final BufferedImage image = ImageIO.read(new URL(
"http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.drawString("Hello World!", 100, 100);
g.dispose();
ImageIO.write(image, "png", new File("test.png"));
}
Output (test.png):

Related

How to place two images in one canvas through java (not jframe)

I just want to place two different images on one canvas and make it a .jpg file in Java. I just want to make a resulting file, not a GUI.
I want to make a result file like below with both images above:
You can use BufferedImage to combine the two images. The following code shows a simple implementation:
public static void combineImages(String imagePath1, String imagePath2, String outputPath) throws IOException {
int intervalWidth = 20; // The interval between two images
BufferedImage image1 = ImageIO.read(new File(imagePath1));
BufferedImage image2 = ImageIO.read(new File(imagePath2));
int combinedWidth = image1.getWidth() + image2.getWidth() + intervalWidth;
int combinedHeight = Math.max(image1.getHeight(), image2.getHeight());
BufferedImage combined = new BufferedImage(combinedWidth, combinedHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = combined.createGraphics();
g.setColor(Color.WHITE);
// Fill the background with white
g.fillRect(0, 0, combinedWidth, combinedHeight);
// Draw the two images on the combined image
g.drawImage(image1, 0, 0, null);
g.drawImage(image2, image1.getWidth() + intervalWidth, 0, null);
ImageIO.write(combined, "jpg", new File(outputPath));
}

how to add utf8 text to image with diffrent ttf fonts?

how do i add utf8 text to image with diffrent utf8 ttf fonts?(another language)
i have tried:
public static void main(String[] args) throws Exception {
final BufferedImage image = ImageIO.read(new File("E:/logo.png"));
Graphics g = image.getGraphics();
Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("E:/BYekan.ttf"));
font.deriveFont(24F);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawString("some utf8 text", 20, 20);
g.dispose();
ImageIO.write(image, "png", new File("E:/image.png"));}
my problem is when i execute this code ,that result is instead clearly my text show just point into image how to fix this code?
my result is :
my created image
did you use canvases.drawText()
update.
you can add font to assets folder and set the typeface with it.

Convert imagicon to image

I need to convert an ImageIcon into an Image for a program how should I go about doing it? I there a method of casting I can use or a built in method to do it?
Building on the answer from the question I linked. I'm not sure which Image you mean, save it as a file or get a java Image instance, but you can use just the second method if you mean the latter, or use the former (which depends on the latter) if you need that.
// format should be "jpg", "gif", etc.
public void saveIconToFile(ImageIcon icon, String filename, String format) throws IOException {
BufferedImage image = iconToImage(icon);
File output = new File(filename);
ImageIO.write(bufferedImage, format, outputfile);
}
public BufferedImage iconToImage(ImageIcon icon) {
BufferedImage bi = new BufferedImage(
icon.getIconWidth(),
icon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0,0);
g.dispose();
return bi;
}

Image transparency - losing alpha on overlap

I'm having problems with image transparency. It's the following:
I have image1 and I need to overlap image2 over it. image2 is png with transparency. I want to create an image with watermark, that would be transparent image2 on top of image1.
When I open image2, that has transparency, and put it in a JFrame just to preview it, it opens with transparency. But when I use a BufferImage object's method getRGB to get image2's pixels and I use setRGB to overlay it over image1, image2 loses transparency and gets white background. Here's the code:
public class Test {
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("c:/images.jpg"));
BufferedImage image2 = ImageIO.read(new File("c:/images2.png"));
int w = image2.getWidth();
int h = image2.getHeight();
int[] pixels = image2.getRGB(0, 0, w, h, null, 0, w);
image2.setRGB(0, 0, w, h, pixels ,0 ,w);
// here goes the code to show it on JFrame
}
}
Please, can someone tell me what I'm doing wrong? I noticed that this code is losing image2's alpha. How could I make it to not lose alpha?
The problem's that setPixel will use the encoding for the image that receives the pixel in a direct way, without interpreting the graphical context for the original image. That won't happen if you use the graphics object.
Try:
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("c:/images.jpg"));
BufferedImage image2 = ImageIO.read(new File("c:/images2.png"));
int w = image2.getWidth();
int h = image2.getHeight();
Graphics2D graphics = image.createGraphics();
graphics.drawImage(image2, 0, 0, w, h, null);
graphics.dispose();
// here goes the code to show it on JFrame
}

Is it possible to create image programmatically on Java, Android?

I need to create .jpeg/.png file on my Android application programmatically. I have simple image (black background), and it need to write some text on it programmatically. How can I do it? Is it possible?
It's definately possible.
To write text on an image you have to load the image in to a Bitmap object. Then draw on that bitmap with the Canvas and Paint functions. When you're done drawing you simply output the Bitmap to a file.
If you're just using a black background, it's probably better for you to simply create a blank bitmap on a canvas, fill it black, draw text and then dump to a Bitmap.
I used this tutorial to learn the basics of the canvas and paint.
This is the code that you'll be looking for to turn the canvas in to an image file:
OutputStream os = null;
try {
File file = new File(dir, "image" + System.currentTimeMillis() + ".png");
os = new FileOutputStream(file);
finalBMP.compress(CompressFormat.PNG, 100, os);
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
screenGrabFilePath = file.getPath();
} catch(IOException e) {
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
Log.e("combineImages", "problem combining images", e);
}
Yes, see here
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
You can also use awt's Graphics2D with this compatibility project
Using Graphics2d you can create a PNG image as well:
public class Imagetest {
public static void main(String[] args) throws IOException {
File path = new File("image/base/path");
BufferedImage img = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.YELLOW);
g2d.drawLine(0, 0, 50, 50);
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 50, 0, 100);
g2d.setColor(Color.RED);
g2d.drawLine(50, 50, 100, 0);
g2d.setColor(Color.GREEN);
g2d.drawLine(50, 50, 100, 100);
ImageIO.write(img, "PNG", new File(path, "1.png"));
}
}

Categories

Resources