Converting text file to JPG file-help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javadeveloper
    New Member
    • Oct 2007
    • 12

    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..
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by javadeveloper
    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 ...

    Comment

    • javadeveloper
      New Member
      • Oct 2007
      • 12

      #3
      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..

      Comment

      • javadeveloper
        New Member
        • Oct 2007
        • 12

        #4
        moreover the text from the input file must be read line by line and written to a single image..

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by javadeveloper
          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.c odec.jpeg package. I don't know what it does but you can go ahead and read about it.

          Comment

          • javadeveloper
            New Member
            • Oct 2007
            • 12

            #6
            i have found my own way of converting text to image.. thank you for providing much valuable information in time..

            Comment

            • javadeveloper
              New Member
              • Oct 2007
              • 12

              #7
              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???

              Comment

              • javadeveloper
                New Member
                • Oct 2007
                • 12

                #8
                can any one sort out the algorithm to convert a text file to jpeg image as it is using java?

                Comment

                • javadeveloper
                  New Member
                  • Oct 2007
                  • 12

                  #9
                  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..

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by javadeveloper
                    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.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      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

                      Comment

                      • javadeveloper
                        New Member
                        • Oct 2007
                        • 12

                        #12
                        static ArrayList<Buffe redImage> 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.tex t.Image i;

                        public void Convert(String fNameOnly)throw s Exception
                        {

                        ReturnPropertie s rProp=new ReturnPropertie s();
                        ts = new Date((new Date()).getTime () - (24*60*60*1000) );
                        Graphics2D g, g1;
                        FontRenderConte xt fc;
                        String testText = "Testing";
                        int fileCnt = 0;
                        String outPath=rProp.r eturnProperties ("TCSAttachment FolderPath");

                        Font font = null;

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

                        }


                        BufferedImage tempBuffer = new BufferedImage(1 , 1, BufferedImage.T YPE_INT_RGB);
                        g = tempBuffer.crea teGraphics();
                        g.setRenderingH int(RenderingHi nts.KEY_ANTIALI ASING, RenderingHints. VALUE_ANTIALIAS _ON);
                        g.setFont(font) ;
                        fc = g.getFontRender Context();

                        Rectangle2D bounds = font.getStringB ounds(testText, fc);
                        float wrappingWidth = width;
                        lineHeight = (int)bounds.get Height();

                        BufferedReader srcFile = null;

                        BufferedWriter writer = new BufferedWriter( new FileWriter(outP ath+"Log.txt",t rue));

                        try
                        {
                        srcFile = new BufferedReader( new FileReader(rPro p.returnPropert ies("Attachment FolderPath")+fN ameOnly));
                        } catch(Exception e)
                        {
                        System.out.prin tln("Error opening file "+e.getMessage( ));
                        writer.append(" Error opening file "+fNameOnly +"!! The file could not be located in the Attachment Folder!!");
                        }



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

                        while (true)
                        {
                        String line;

                        try
                        {
                        line = srcFile.readLin e();

                        }
                        catch(IOExcepti on ignore)
                        {
                        break;
                        }

                        if (line == null)
                        {
                        break;
                        }

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

                        AttributedStrin g attribString = new AttributedStrin g(line);
                        attribString.ad dAttribute(Text Attribute.FOREG ROUND, Color.WHITE, 0, line.length());
                        attribString.ad dAttribute(Text Attribute.FONT, font, 0, line.length());
                        AttributedChara cterIterator aci = attribString.ge tIterator();
                        LineBreakMeasur er lbm = new LineBreakMeasur er(aci, fc);

                        while (lbm.getPositio n() < line.length())
                        {
                        BufferedImage lineBuffer = new BufferedImage(w idth, lineHeight, BufferedImage.T YPE_INT_RGB);
                        g1 = lineBuffer.crea teGraphics();
                        g1.setRendering Hint(RenderingH ints.KEY_ANTIAL IASING, RenderingHints. VALUE_ANTIALIAS _ON);
                        TextLayout layout = lbm.nextLayout( wrappingWidth);
                        int y = (int)layout.get Ascent();
                        layout.draw(g1, margin, y);
                        images.add(line Buffer);
                        lineCnt += lineHeight;


                        /* New Code

                        images.addAll(i mages);


                        New Code**/

                        }

                        }


                        // if (lineCnt+lineHe ight > 480)
                        //if (lineCnt+lineHe ight > 720)
                        //{
                        try {
                        saveImage(outPa th+fNameOnly+". jpg", fileCnt++);
                        } catch (IOException e) {
                        System.out.prin tln("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(outPa th+fNameOnly+". jpg", fileCnt++);
                        flag=1;
                        } catch (IOException e)
                        {
                        System.out.prin tln("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(Strin g fileName, int fileCnt) throws IOException
                        {
                        Graphics2D g;
                        BufferedImage buffer = new BufferedImage(6 00, 800, BufferedImage.T YPE_INT_RGB);
                        g = buffer.createGr aphics();
                        g.setBackground (Color.BLACK);
                        g.clearRect(0, 0, 600, 800);

                        for (int i = 0; i < images.size(); i++)
                        {
                        g.drawImage((Bu fferedImage)ima ges.get(i), 0, margin+(i*lineH eight), null);
                        }

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

                        StringBuffer fullFileName = new StringBuffer(fi leName)/*.append(addLea dingZeros(fileC nt, 4))*/.append(".jpg") ;
                        OutputStream out = new FileOutputStrea m(new File(fullFileNa me.toString())) ;
                        ImageIO.write(b uffer, "jpg", out);
                        out.close();


                        File imgFile = new File(fullFileNa me.toString());
                        imgFile.setLast Modified(ts.get Time());

                        ts.setTime(ts.g etTime() + (60*1000));
                        }

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

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

                        else if (strValue.lengt h() < len)
                        {
                        retString = "00000000000000 000000000000000 00".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..

                        Comment

                        • javadeveloper
                          New Member
                          • Oct 2007
                          • 12

                          #13
                          Sorry about not embedding it in the [CODE] tags.. I hope this would be excused..

                          Comment

                          • javadeveloper
                            New Member
                            • Oct 2007
                            • 12

                            #14
                            does anyone hava any idea?

                            Comment

                            • Wig Pusha
                              New Member
                              • Nov 2008
                              • 1

                              #15
                              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.

                              Comment

                              Working...