Monday, September 5, 2011

Resize the image from database and insert it...

import java.sql.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageReSizeTnsec{
    public static void main(String args[]){
         if ( args.length < 2 )
         {
             System.out.println("Usage: java ImageReWorkNew <dtname> ");
             System.exit(1);
         }
        String dcode = args[0];
        String tname = args[1];
        if (!( args[1].equals("detail") || args[1].equals("sup")) ){
            System.out.println("Usage: java ImageReWorkNew <tname> should be detail or sup ");
            System.exit(1);
        }
        String connectionURL = "jdbc:postgresql://localhost:5432/test";
        String imageQuery    = "select uidno from "+dcode+".e_"+tname;
        try{
            Class.forName("org.postgresql.Driver").newInstance();
            Connection con = DriverManager.getConnection(connectionURL,"uname","pwd");
            Statement st1=con.createStatement();
            ResultSet rs1 = st1.executeQuery(imageQuery);
            while (rs1.next())
            {
                long uidNo   = rs1.getLong(1);
                String ImageQuery2="select photo from "+dcode+".e_"+tname+" where uidno="+uidNo;
                Statement st2=con.createStatement();
                ResultSet rs2 = st2.executeQuery(ImageQuery2);
                while(rs2.next()){
                         InputStream imageStream = rs2.getBinaryStream("photo");
                         String imageName = "/tmp/"+dcode+tname+uidNo+"image.jpg";
                         if(rs2.getBinaryStream("photo") == null || rs2.getBinaryStream("photo").equals("") || rs2.getBinaryStream("photo").equals(" "))
                         {
                             String uidInsertQuery = "insert into "+dcode+"."+tname+"_photo(uidno) values (?)";
                             PreparedStatement pstUid = con.prepareStatement(uidInsertQuery);
                             pstUid.setLong(1, uidNo);
                             pstUid.executeUpdate();
                             pstUid.close();
                         }else{
                             FileOutputStream f = new FileOutputStream(imageName);
                             byte buff[] = new byte[1024];
                             int l;
                             while ((l = imageStream.read(buff)) > 0)
                             {
                                 f.write(buff, 0, l);
                             }
                             f.close();
                             File fl = new File("/tmp/"+dcode+tname+"image.jpg");
                             System.out.println("File Created and length is="+fl.length());
                             if(fl.length() > 0)
                             {
                                 String osExe = "convert  /tmp/"+dcode+tname+uidNo+"image.jpg -colors 8 -resize 100x100 /tmp/"+dcode+tname+uidNo+"Image2.jpg";
                                 Process p = Runtime.getRuntime().exec(osExe);
                                 Thread.sleep(100);
                                 BufferedImage originalImage = ImageIO.read(new File("/tmp/"+dcode+tname+uidNo+"Image2.jpg"));
                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                 ImageIO.write(originalImage, "jpg", baos );
                                 byte[] imageInByte  = baos.toByteArray();
                                 String imgInsertQuery = "insert into "+dcode+".e_"+tname+"_photo(uidno, photo) values (?,?)";
                                 PreparedStatement pst = con.prepareStatement(imgInsertQuery);
                                 pst.setLong(1, uidNo);
                                 pst.setBytes(2, imageInByte);
                                 pst.executeUpdate();
                                 pst.close();
                                 baos.close();
                             }else{
                                 String uidInsertQuery = "insert into "+dcode+".e_"+tname+"_photo(uidno) values (?)";
                                 PreparedStatement pstUid = con.prepareStatement(uidInsertQuery);
                                 pstUid.setLong(1, uidNo);
                                 pstUid.executeUpdate();
                                 System.out.println("Uid Only inserted in file");
                                 pstUid.close();
                             }
                             fl.deleteOnExit();
                }
                }
                rs2.close();
            }
            rs1.close();
            st1.close();
            con.close();
      }
      catch (Exception e){
         e.printStackTrace();
      }
   }
}

No comments:

Post a Comment