• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic JPG with java

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody help me with the code regarding how to create a dynamic JPG file with text string as image using JAVA.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saving a Generated Graphic to a PNG or JPEG File
 
nuthan kumar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thanks for your reply,
I am able to create JPG file but its not displaying text in the image.


public static void main(String[] args)
{
int width = 200;
int height = 100;

// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();

// Draw graphics
g2d.setColor(Color.white);
g2d.fillRect(0, 0, width, height);

g2d.setColor(Color.black);
g2d.drawString("Sample-Text", 0, 0);
RenderedImage rendImage = bufferedImage;
try {
// Save as JPEG
File file = new File("newimage.jpg");
ImageIO.write(rendImage, "jpg", file);
} catch (IOException e) {
}


// Graphics context no longer needed so dispose it
g2d.dispose();

// return bufferedImage;

}
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the API documentation for java.awt.Graphics. The x,y position arguments for drawString is the baseline of the text, not the upper left-hand corner. You can probably see the tail of the "p" at the top of your image.
 
reply
    Bookmark Topic Watch Topic
  • New Topic