Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:34:51+00:00 2026-06-09T13:34:51+00:00

I’m trying to write strings to images, so it’s harder to copy the text

  • 0

I’m trying to write strings to images, so it’s harder to copy the text and run it through a translator.

My code works fine, but I get always a really long image – I rather would like to have a more readable box in where the string is written. My method “StringDiver” does add “\n” but it does not help when writing the string to an image.

Right now I get this output.

Any hint what I could do?

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class writeToImage {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    String newString = "Mein Eindruck ist, dass die politische und öffentliche Meinung in Deutschland anfängt, die wirtschaftliche Zerstörung im Inland und in Europa zu erkennen, die auf einen eventuellen Zusammenbruch des Euro folgen würde.";
    String sampleText = StringDivider(newString);

    //Image file name
   String fileName = "Image";

    //create a File Object
    File newFile = new File("./" + fileName + ".jpg");

    //create the font you wish to use
    Font font = new Font("Tahoma", Font.PLAIN, 15);

    //create the FontRenderContext object which helps us to measure the text
    FontRenderContext frc = new FontRenderContext(null, true, true);

    //get the height and width of the text
    Rectangle2D bounds = font.getStringBounds(sampleText, frc);
    int w = (int) bounds.getWidth();
    int h = (int) bounds.getHeight();

    //create a BufferedImage object
   BufferedImage image = new BufferedImage(w, h,   BufferedImage.TYPE_INT_RGB);

    //calling createGraphics() to get the Graphics2D
    Graphics2D g = image.createGraphics();

    //set color and other parameters
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);

   g.drawString(sampleText, (float) bounds.getX(), (float) -bounds.getY());

  //releasing resources
  g.dispose();

    //creating the file
   try {
    ImageIO.write(image, "jpg", newFile);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 }

public static String StringDivider(String s){

    StringBuilder sb = new StringBuilder(s);

    int i = 0;
    while ((i = sb.indexOf(" ", i + 30)) != -1) {
        sb.replace(i, i + 1, "\n");
    }

    return sb.toString();



}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:34:52+00:00Added an answer on June 9, 2026 at 1:34 pm
    g.drawString(sampleText, (float) bounds.getX(), (float) -bounds.getY());
    

    Split text and write every part to image.

    Rectangle2D bounds = font.getStringBounds(sampleText, frc);
    int w = (int) bounds.getWidth();
    int h = (int) bounds.getHeight();
    
    String[] parts = sampleText.split("\n");
    //create a BufferedImage object
    BufferedImage image = new BufferedImage(w, h * parts.length,   BufferedImage.TYPE_INT_RGB);
    
    int index = 0;  
    for(String part : parts){
        g.drawString(part, 0,  h * index++);
    }
    

    ex:

    first part:  x=0 ; y=0
    second part: x=0 ; y=5
    third part:  x=0 ; y=10;
    

    heightText = h

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.