Tuesday, August 20, 2013

Converting Text into array and creating Combined image(Text + image)

  
               public String path(String text,String id) {
         
                String query;

                BufferedImage image;
                OutputStream targetFile1;
                //Image image1;
                byte[] buf=new byte[1024];


                byte[] fileBytes;
                ResultSet rs;
                int i;
                char a[]=text.toCharArray();  
                int t= text.length();
                System.out.println("i is"+t);
                float x=t/40;
                double value=Math.ceil(x);
                int val=(int)value;
                String [] str=new String[val+1];
                int len=0;
                int k=0,j;
                char [] temp={' '};
                StringBuffer strbuf;
                
                
                
                for( i=1;i<=val;i++)
                {
                    //System.out.println("print value"+i);
                   for(j=40*(i-1);j<t;j++)
                   {
                
                if(j==40*i)
                {
                    for( k=40*i;k>40*(i-1);k--)
                    {
                        
                        
                        
                        if(a[k]==(char)(32))
                        {
                            
                            //System.out.println("k is"+k);
                            
                            break;
                        }
                    }
                    //System.out.println("len  is........"+len);
                    strbuf =new StringBuffer();
                    for(int l=len;l<k;l++)
                    {

                        //System.out.println("print"+a[l]);
   
                          strbuf.append(a[l]);
          
                    }
                    //System.out.println("a39 is..."+a[39]);
                    //System.out.println("a39 is"+a[40]);
                    //System.out.println("print stringbuffer"+strbuf); 
                       
                len=k;
                
                
                       String s=new String();
                    s=strbuf.toString();
                       //System.out.println("print karo"+s);
                        str[i-1]=s;
                        //System.out.println(str[i-1]+"check");
                    
                    strbuf=null;
                    break;
                    
                
                }
            
                
                
                   }   
                   
                //System.out.println(j+"welcome to all");
                }
                
                
               strbuf =new StringBuffer();
                System.out.println("what is val-1"+val);
                for(int last=k;last<t;last++)
                {
                    strbuf.append(a[last]);
                    //System.out.print("ye h ........"+a[last]);
                }

                   String s=new String();
                s=strbuf.toString();
                   System.out.println("print karo"+s);
                    str[val]=s;
                    
                    //System.out.println(str[i-1]+"check");
                
                strbuf=null;

                for(int extra=0;extra<=val;extra++)
                {
                    System.out.println("hello ji kya hua "+str[extra]+"\n");
                }
                
                try
                {

                    connect();
                        query = "select image from save1";


                        rs= st.executeQuery(query);
                        if (rs.next())
                       {
                            System.out.println("hello");
                                 fileBytes = rs.getBytes("image");
                                 OutputStream targetFile=  
                                 new FileOutputStream(
                                      "d://flex.JPG");

                                 targetFile.write(fileBytes);
                                 targetFile.close();
                       }        
                       
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }


        //String totallen=text;

                try
                {

                    BufferedImage imagefinal = ImageIO.read(new File("d://flex.jpg"));
                                
                //    int w = Math.max(image.getWidth(), overlay.getWidth());
                    //int h = Math.max(image.getHeight(), overlay.getHeight());
                    
                    BufferedImage combined = new BufferedImage(500, 800, BufferedImage.TYPE_INT_ARGB);
                    
                    
                    //combined.setRGB(40,30,30);
                    
                    
                    Graphics g = combined.getGraphics();
                    
                    Font font = new Font("Serif", Font.PLAIN, 24);
                       g.setFont(font);
                       g.setColor(Color.black);
                       
                       g.drawImage(imagefinal, 20, 20, null);
                       int faltu=20;
                for(int m=0;m<=val;m++)
                {

                    g.drawString(str[m], 20, 150+m*faltu);

                }
                    
                    //g.drawImage(overlay, 40, 40, null);// Save as 

                   ImageIO.write(combined, "PNG", new File("d://final.jpg")); 

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

                         int len1;
                    String query1;
                  
                    PreparedStatement pstmt;
                    try
                   
                    {
                        connect();
                            File file = new File("d://final.jpg");
                            FileInputStream fis = new FileInputStream(file);
                           
                            len1 = (int)file.length();

                            query1 = ("insert into last VALUES(?,?)");
                            pstmt = con.prepareStatement(query1);
                            pstmt.setString(1,id);
                            
                            // Method used to insert a stream of bytes
                            pstmt.setBinaryStream(2, fis, len1); 
                            pstmt.executeUpdate();

                    }

                    catch (Exception e)
                    {
                            e.printStackTrace();
                    }
           
                  //sending
               
                 int len1;
                    String query1;
                  
                    PreparedStatement pstmt;
                    try
                   
                    {
                        connect();
                            File file = new File("d://final.jpg");
                            FileInputStream fis = new FileInputStream(file);
                           
                            len1 = (int)file.length();

                            query1 = ("insert into last VALUES(?,?)");
                            pstmt = con.prepareStatement(query1);
                            pstmt.setString(1,id);
                            
                            // Method used to insert a stream of bytes
                            pstmt.setBinaryStream(2, fis, len1); 
                            pstmt.executeUpdate();

                    }

                    catch (Exception e)
                    {
                            e.printStackTrace();
                    }

                return null;
 
            }

No comments:

Post a Comment