473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 15762
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

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

Similar topics

2
by: gnv | last post by:
Hi all, I am writing a cross-browser(i.e. 6 and netscape 7.1) javascript program to save an XML file to local file system. I have an xml string like below: var xmlStr = "<?xml version="1.0"...
2
by: Adonis | last post by:
I have an XML file hosted by my ISP free web space. It naively treats the file as text/plain. I would like to convert this data into a DOM object. So far my googling has turned up nothing, although...
6
by: pankaj.khandelwal | last post by:
Hi, How can I bundle a text/gif file in the shared object. Basically I want to read the content of this text file but I cannot have it seperately on the disc. Any clues. ? Pankaj
1
by: David Dvali | last post by:
How can I load text from file to TextBox?
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
3
by: Flix | last post by:
Hello. What I want to do is simple: correctly reading a text file whose encoding is not known (it can be Ascii,UTF7,UTF8 or Unicode). I'm thinking of something like that: 1) Read the text...
0
Krishna Ladwa
by: Krishna Ladwa | last post by:
In Sql Server 2000 Version, I found that no Notification message box appears when converting text column to varchar but the data gets truncated to the given size for the varchar. Whereas it appears...
2
by: shireen Eason | last post by:
How to create a text output file from access database?
6
by: wxPythoner | last post by:
Hello! I have trouble understanding something in this code snippet: class TextReader: """Print and number lines in a text file.""" def __init__(self, file): self.file = file
3
dlite922
by: dlite922 | last post by:
Hey guys, I need to maintain short list of messages in a text log file. I was just googling for the best way to delete the first or last line and insert a new one. Basically with each access...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.