Wednesday, February 15, 2012

HTML TO JPEG IN JAVA

Hi,
   Today i will explain you how to convert html page to jpeg in java.

     public class HTML2JPEG{
          public static void main(String args[]){
              //for reading the input file data
              DataInputStream dis = new DataInputStream(new FileInputStream(new File("file.html")));
              //for writing the output file data
              FileOutputStream fos = new FielOutputStream(new File("testfile.jpeg"));
              bytes[]  bytes = new byte[1024];
              StringBuffer sb = new StringBuffer();
              while(dis.available() >0){
                      sb.append(new String(bytes, 0, dis. read(bytes)));
             }
              String str = sb.toString();
              //using awt package for creating the image
              JLabel label = new JLabel(str);
              label.setSize(800,800);
            
               BufferedImage image = new BufferedImage(label.getWidth(),label.getHeight(),BufferedImage.TYPE_BYTE_RGB);
               Graphics g = image.getGraphics();//or else can use createGraphics()              
               g.setColor(Color.BLACK);            
               label.paint(g);
               g.dispose();
            
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              ImageIO.write(image, "jpeg",baos);
              //read the data from baos to byte[]
              byte[] bytesData = baos.toByteArray();  
               //Or Else you can use FileOutputStream
              ImageIO.write(image, "jpeg",fos);
              System.out.println("Completed");
         }
}

Please correct me if at all there is any changes need to be done in the above program.Or else if there is a better way than this, post it immediately.Any help appreciated.

Wednesday, January 25, 2012

LOG4J Configuration in Simple Project

Hi,
     Here i'll be explaining you the Log4j configuration in Sturts1.x with a sample Java Program.
     import org.apache.log4j.*;
     public class Log4jClass {
         //In order to generate the logger file at the time of starting the server.
         static{
                   //create a logger instance
                   Logger log = Logger.getLogger(Log4jClass.class);
                    //Domconfigurator to setting the properties of logger file using xml or properties file.
                    DomConfigurator.configure("log4jpath/log4j.xml"); //or log4j.properties file
          }
      
          public static void main(String args[]) {
              //Different logging levels available in Log4j
              Log4jClass.trace(Log4jClass.class,"This is the trace method usage in log4j file");
              Log4jClass.debug(Log4jClass.class,"This is the debug method usage in log4j file");
              Log4jClass.info(Log4jClass.class,"This is the info method usage in log4j file");
              Log4jClass.warn(Log4jClass.class,"This is the warn method usage in log4j file");
              Log4jClass.error(Log4jClass.class,"This is the error method usage in log4j file");
              Log4jClass.fatal(Log4jClass.class,"This is the fatal method usage in log4j file");
       }
 }

You can set the System Out console to logger also using the System.setOut() method
Where,
            PrintStream ps = new PrintStream(new FileOutputStream("pathtofile");
            System.setOut(ps);
Now,
          System.out.println("Text");
                                                  will be moved to the logger file only but not to system console. Any line before the System.setOut() will be to System Console but not to logger file.

Any modifications or corrections will be appreciated......