469,287 Members | 2,637 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

Converting text file to JPG file-help

Can anybody help me with the code regarding how to convert a text file to JPG file... Its really urgent for me.. I was able to convert a text file to PDF.. But now I need to convert a text file to JPEG..
Oct 18 '07 #1
15 15315
r035198x
13,262 8TB
Can anybody help me with the code regarding how to convert a text file to JPG file... Its really urgent for me.. I was able to convert a text file to PDF.. But now I need to convert a text file to JPEG..
You realize of course that a JPEG file is usually a picture?
Perhaps if you specify what your conversion rules are ...
Oct 18 '07 #2
Actually i need to read data from a file(with no extensions) in a folder and get the text in it written as an image.. I am able to read the contents of the file.. But i am not sure about how to put the text as JPEG file..
Oct 18 '07 #3
moreover the text from the input file must be read line by line and written to a single image..
Oct 18 '07 #4
r035198x
13,262 8TB
Actually i need to read data from a file(with no extensions) in a folder and get the text in it written as an image.. I am able to read the contents of the file.. But i am not sure about how to put the text as JPEG file..
So you want to create a JPG image using Java code and have the text displayed as part of the image?
There is a com.sun.image.codec.jpeg package. I don't know what it does but you can go ahead and read about it.
Oct 18 '07 #5
i have found my own way of converting text to image.. thank you for providing much valuable information in time..
Oct 29 '07 #6
I have got a little bit of a problem.. I am unable to convert the whole text file to a single image file.. It gets stored as a number of images.. Moreover I get into problems with alignment.. Can anybody help? I am using BuffferedImage class to generate the image.. is there any other possible way???
Nov 5 '07 #7
can any one sort out the algorithm to convert a text file to jpeg image as it is using java?
Nov 5 '07 #8
is there a way to read text file line by line and write it as a jpg file line by line using java.. need not be too complex.. but i expect something which can do it perfectly.. i m getting problems in alignment in my algorithm.. would be very thankful for any sort of solution in java.. i had been breaking my head to get this perfect..
Nov 6 '07 #9
r035198x
13,262 8TB
is there a way to read text file line by line and write it as a jpg file line by line using java.. need not be too complex.. but i expect something which can do it perfectly.. i m getting problems in alignment in my algorithm.. would be very thankful for any sort of solution in java.. i had been breaking my head to get this perfect..
Perhaps if you post the code you've written that is not aligning, I (for one) might be able to understand what you are trying to do.
Nov 6 '07 #10
JosAH
11,448 Expert 8TB
I convert my documents to (read only) pdf files by printing them to a virtual printer
that converts the documents to pdf format for me. All that my Java code has to
do is print.

kind regards,

Jos
Nov 6 '07 #11
static ArrayList<BufferedImage> images;
Image img;
static int lineHeight;
static int width = 800;
static int margin = 5;
static String fontName = "Arial";
static int fontSize = 12;
static Date ts;
com.lowagie.text.Image i;

public void Convert(String fNameOnly)throws Exception
{

ReturnProperties rProp=new ReturnProperties();
ts = new Date((new Date()).getTime() - (24*60*60*1000));
Graphics2D g, g1;
FontRenderContext fc;
String testText = "Testing";
int fileCnt = 0;
String outPath=rProp.returnProperties("TCSAttachmentFolde rPath");

Font font = null;

try {
font = new Font(fontName,Font.PLAIN, (int)fontSize);
} catch (Exception e)
{
System.out.println("Error opening font file : "+e.getMessage());

}


BufferedImage tempBuffer = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
g = tempBuffer.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(font);
fc = g.getFontRenderContext();

Rectangle2D bounds = font.getStringBounds(testText, fc);
float wrappingWidth = width;
lineHeight = (int)bounds.getHeight();

BufferedReader srcFile = null;

BufferedWriter writer = new BufferedWriter(new FileWriter(outPath+"Log.txt",true));

try
{
srcFile = new BufferedReader(new FileReader(rProp.returnProperties("AttachmentFolde rPath")+fNameOnly));
} catch(Exception e)
{
System.out.println("Error opening file "+e.getMessage());
writer.append("Error opening file "+fNameOnly+"!! The file could not be located in the Attachment Folder!!");
}



images = new ArrayList<BufferedImage>();
int lineCnt = margin+margin;

while (true)
{
String line;

try
{
line = srcFile.readLine();

}
catch(IOException ignore)
{
break;
}

if (line == null)
{
break;
}

if ("".equals(line))
{
line = " ";
}

AttributedString attribString = new AttributedString(line);
attribString.addAttribute(TextAttribute.FOREGROUND , Color.WHITE, 0, line.length());
attribString.addAttribute(TextAttribute.FONT, font, 0, line.length());
AttributedCharacterIterator aci = attribString.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, fc);

while (lbm.getPosition() < line.length())
{
BufferedImage lineBuffer = new BufferedImage(width, lineHeight, BufferedImage.TYPE_INT_RGB);
g1 = lineBuffer.createGraphics();
g1.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G, RenderingHints.VALUE_ANTIALIAS_ON);
TextLayout layout = lbm.nextLayout(wrappingWidth);
int y = (int)layout.getAscent();
layout.draw(g1, margin, y);
images.add(lineBuffer);
lineCnt += lineHeight;


/* New Code

images.addAll(images);


New Code**/

}

}


// if (lineCnt+lineHeight > 480)
//if (lineCnt+lineHeight > 720)
//{
try {
saveImage(outPath+fNameOnly+".jpg", fileCnt++);
} catch (IOException e) {
System.out.println("Error writing image : "+e.getMessage());
}

images.clear();
lineCnt = margin+margin;
}
/* Here is where a number of images are saved and I commented it*/
/*if (lineCnt != 0)
{
try {
saveImage(outPath+fNameOnly+".jpg", fileCnt++);
flag=1;
} catch (IOException e)
{
System.out.println("Error writing image : "+e.getMessage());
flag=0;
}

}

if (flag==1)
{
writer.append("File "+fNameOnly+" has been successfully converted to JPG ");
writer.newLine();
}

}*/

static void saveImage(String fileName, int fileCnt) throws IOException
{
Graphics2D g;
BufferedImage buffer = new BufferedImage(600, 800, BufferedImage.TYPE_INT_RGB);
g = buffer.createGraphics();
g.setBackground(Color.BLACK);
g.clearRect(0, 0, 600, 800);

for (int i = 0; i < images.size(); i++)
{
g.drawImage((BufferedImage)images.get(i), 0, margin+(i*lineHeight), null);
}

/////////////////////////////////
buffer.flush();
images.add(buffer);
////////////////////////////////

StringBuffer fullFileName = new StringBuffer(fileName)/*.append(addLeadingZeros(fileCnt, 4))*/.append(".jpg");
OutputStream out = new FileOutputStream(new File(fullFileName.toString()));
ImageIO.write(buffer, "jpg", out);
out.close();


File imgFile = new File(fullFileName.toString());
imgFile.setLastModified(ts.getTime());

ts.setTime(ts.getTime() + (60*1000));
}

static String addLeadingZeros(int value, int len)
{
String strValue = Integer.toString(value);
String retString = "";

if (strValue.length() >= len)
{
retString = strValue;
}

else if (strValue.length() < len)
{
retString = "0000000000000000000000000000000".substring(0, len - strValue.length()) + strValue;
}

return retString;
}




A code I got in the net.. But it creates a number of images.. It was designed so as to create PSP size images(480x272).. I modified it to get 800x600 image.. And it creates a number of images without proper alignment... I need a single 800x600 image for a file..
Nov 10 '07 #12
Sorry about not embedding it in the [code] tags.. I hope this would be excused..
Nov 10 '07 #13
does anyone hava any idea?
Nov 10 '07 #14
it got real quiet after you posted all that code lol.did u ever find out because im having the same problem.

basically i wrote something in the notepad (.txt) and i want to copy it and post it on a website with the particular font i selected.

only problem is the website doesnot support the style of font i want to use.so i figured id have to make it as an image file.thank you in advance.
Nov 1 '08 #15
Might not work, but have you tried storing the entire contents of the file into a String?

you can do this by simply declaring a public string (say strFileContents) and then replace:

Expand|Select|Wrap|Line Numbers
  1. line = srcFile.readLine(); 
  2.  
with

Expand|Select|Wrap|Line Numbers
  1. strFileContents += srcFile.readLine()+"\r\n";
  2.  
Actually I'm not too sure if the breakline escapes are needed or not...just find out by trial and error I suppose.

EDIT: Just took a more in depth look at your code. If this solution works, your alignment might mess up a little...
Nov 1 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Adonis | last post: by
6 posts views Thread by pankaj.khandelwal | last post: by
1 post views Thread by David Dvali | last post: by
12 posts views Thread by Frederik Vanderhaeghe | last post: by
6 posts views Thread by wxPythoner | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.