Tuesday, 13 September 2011

Screen capture with mouse position


Here I am showing, that how can get screen shot with mouse pointer position.
This is the small 10% part of VIDEO RECORDING of your desktop activity.

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.io.File;
import javax.imageio.ImageIO;

//    Screen capture with mouse position.

class GetScreenShot implements Runnable {
    private Robot robot;
    private Rectangle r;
    private String filename,seq;
    private int num,maxZero;
    public GetScreenShot() {
        try{
            robot = new Robot();
            num=1;
            maxZero = 11;
            seq = "";

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

    public void run() {
        while(true) {
            try {
            System.out.println(num);
            String numSt=num+"";
            int i;
            for(i=1;i<=maxZero -numSt.length();i++) {
                seq=seq+"0";
            }  
            seq=seq+num+".jpg";
            filename=seq;
            r=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            File file = new File(filename);
            BufferedImage bf=null;
            bf=captureScreen(r);
            ImageIO.write(bf, "JPEG", file);
            num++;
            seq="";
            Thread.sleep(9);
            }
            catch(Exception ee){
                System.out.println(ee.toString());
            }
        }
    }
   
    public BufferedImage captureScreen(Rectangle recordArea) {
        Point mousePosition = MouseInfo.getPointerInfo().getLocation();
        BufferedImage image = robot.createScreenCapture(recordArea);
        Polygon pointer = new Polygon(new int[]{0,16,10,8},new int[]{0,8,10,16},4);
        Polygon pointerShadow = new Polygon(new int[]{6,21,16,14},new int[]{1,9,11,17},4);
        Graphics2D gr12D = image.createGraphics();
        gr12D.translate(mousePosition.x,mousePosition.y);
        gr12D.setColor(new Color(100,100,100,100) );
        gr12D.fillPolygon(pointerShadow);
        gr12D.setColor(new Color(100,100,255,200) );
        gr12D.fillPolygon(pointer);
        gr12D.setColor(Color.red);
        gr12D.drawPolygon(pointer);
        gr12D.dispose();
        return image;
    }  
}
public class ScreenCapWithMousePos {
    public static void main(String ss[]) {
        GetScreenShot gs=new GetScreenShot();
        Thread thread1 = new Thread(gs);
        thread1.start();
    }
}

Monday, 12 September 2011

Extract Corrupt Zip File By Java


I received an zip file from my friend. I have tried to extract with WinZip, but unable to extract zip file because that zip file was corrupt.So I have tried to extract with Java Zip API and got the success. The code is listed below:
I am sure that the zip file having 5% to 10% part corrupt will be unzip with this code, but if it is having more corrupt then the code will not work.

import java.io.*;
import java.util.zip.*;

class ExtractCorruptZip {
public void extractZipFiles(String fileName) {
try {
String zipDir = "c:\\zip\\";
//String zipDir = "/home/usr/amit/zip/";
byte[] by = new byte[2048];
ZipInputStream ziStream = null;
ZipEntry zipEntry;
ziStream = new ZipInputStream(new FileInputStream(fileName));

zipEntry = ziStream.getNextEntry();
int count=1;
while (zipEntry != null) {
String entry = zipDir + zipEntry.getName();
entry = entry.replace('/', File.separatorChar).replace('\\', File.separatorChar);
int n;
FileOutputStream fOTStream;
File newFile = new File(entry);
if (zipEntry.isDirectory()) {
if (!newFile.mkdirs()) {
break;
}
zipEntry = ziStream.getNextEntry();
continue;
}
fOTStream = new FileOutputStream(entry);
while ((n = ziStream.read(by, 0, 2048)) > -1) {
fOTStream.write(by, 0, n);
}
System.out.println("Working....."+(++count));
fOTStream.close();
ziStream.closeEntry();
zipEntry = ziStream.getNextEntry();
}
ziStream.close();
System.out.println("DONE!!!!!");
} catch(IOException ioe) {
ioe.printStackTrace();
}

public static void main(String[] ss) {
try {
String fileName = "w1.zip";
ExtractCorruptZip extractCorruptZip = new ExtractCorruptZip();
extractCorruptZip.extractZipFiles(fileName);
} catch (Exception ex) {
ex.printStackTrace();
}
}

}

Saturday, 10 September 2011

Use proxy server in PHP


How to use proxy server in PHP.

<?php
// First get the Free proxy list from here
// http://hidemyass.com/proxy-list/search-284460

set_time_limit(0) ;
$t1=time();
$urltopost = "http://pshaikh.blogspot.com/";

// $urltopost = http://www.xyz.com/t3resultpage.aspx?type=p=test1&q=test2&rid=7851
// $datatopost = array (
//  "firstname" => "Parvez",
// "lastname" => "Shaikh",
// "blog" => "http://pshaikh.blogspot.com/",
// );

 $crl = curl_init ($urltopost);
 curl_setopt ($crl, CURLOPT_POST, true);
 curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($crl, CURLOPT_PROXY, "115.248.234.254:3128");
 curl_setopt($crl, CURLOPT_PROXYPORT, 3128);
 //curl_setopt ($crl, CURLOPT_POSTFIELDS, $datatopost);
 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, true);
 $ans = curl_exec ($crl);
 $t2=time();
 $lapsed=$t2-$t1;
 echo "<br/>Total time taken" . $lapsed;
 echo $ans;
?>

Thursday, 8 September 2011

Sending email in PHP from Gmail



For sending email in PHP by Gmail id, I have used phpmailer class.
Note that php_openssl must be un-commented in the php.ini file.
In phpMailer directory open "class.phpgmailer.php"  and change the line as listed below:
var $Host        = "ssl://smtp.gmail.com";
var $Port        = 465;


Put listed below code in php file from where you want to send email.
require_once('phpgmailer/class.phpgmailer.php');
$mailToClient=new PHPGMailer();
$mailToClient->IsHTML(true);
$mailToClient->Username='full-gmail-address';//'pxyz@gmail.com';
$mailToClient->Password='gmail-password-here';//abc123
$mailToClient->From='full-gmail-address';//pxyz@gmail.com';
$mailToClient->FromName='Parvez';
$mailToClient->Subject='YOUR-SUBJECT';//sending mail
$mailToClient->AddAddress('recipient email address here');
$mailToClient->Body='Hey, Here is Parvez!';
$mailToClient->Send();

Monday, 5 September 2011

How to show place in Google map


How to show place in Google map
-----------------------------------

1) Create a table "latandlon" in Mysql/Oracle/Sqlserver/DB2 in which you feel easy.

CREATE TABLE latandlon (
  id int(11) NOT NULL auto_increment,
  lat decimal(10,6) NOT NULL default '0.000000',
  lon decimal(10,6) NOT NULL default '0.000000',
  address varchar(255) NOT NULL default '',
  PRIMARY KEY  (id)
);


2) feed the listed below data.
----------------------------------------------------------
id,     lat,            lon,            address
----------------------------------------------------------
1    23.402800    78.454100    Madhya Pradesh
2    26.280000    80.210000    Kanpur
3    31.122027    77.111664    Himanchal Pradesh
4    22.533000    88.367000    Kolkata
5    28.350000    77.120000    Delhi
6    17.230000    78.290000    Hyderabad
----------------------------------------------------------





3) put youe google map key in the index.jsp.
4) change the database connection string in the index.jsp page, coz I have used MySql.

IF YOU WANT CODE GIVE YOUR EMAIL ADDRESS HERE I WILL MAIL YOU.


Saturday, 3 September 2011

Add new row dynamically in JTable


This is Java code allow to you add a new row in JTable.
when you press enter then a new row automatically inserted and you can make more entry.


import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
public class t1 extends JFrame {
DefaultTableModel model;
JTable table;
JScrollPane sc1;
public t1() {
super("Data Entry Form By Amit Gaur (amitt800@gmail.com)");
int i,j;
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
model = new DefaultTableModel();
table = new JTable(model);
model.addColumn("Name");
model.addColumn("ID");
model.addColumn("Salary");
model.addColumn("Jobs");
model.addRow(new Object[]{"","","",""});
table.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
model.addRow(new Object[]{"", "","",""}); } }
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { } } );

sc1 = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); cp.add(sc1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,600);
setVisible(true);
}
public static void main(String[] ss) {
new t1();
}
}

Auto Resume Broken & Progress Bar File Uploader Applet


I have created this Java Auto Resume Broken & Progress Bar File Uploader Applet.Internet connection broken is the very common problem with of all us. When clients are upload large (GBs size) file via web page, they need uninterrupted internet connection supply. If internet connection breaks, then need to re-upload file. and if internet connection breaks many times during uploading, then clients need to upload the file many times agin and again and so on...... So uploading one file many time, waste our time and also expensive in term of bandwidth. By this uploader upload Unlimited GBs file without fear of internet connection break. Also I have added another Fast/Slow send Bytes speed, whatever depends on your internet connection. Suppose you have slow connection, you can set slow send bytes on the other hand if you are having fast connection set fast send bytes. This prevents loosing your valuable data by fluctuation of bandwidth.
I had solved this problem in my previous up-loader.it was upload the file from the position, where internet connection was broken. but this time I felt that during net connection break, user need to click resume button many time, depend on net connection. And also during uploading user need to stay on the computer screen, he/she will engaged and not able to do other side work eg. outside computer room work etc.
So, I have totally overcome this problem and given *ALTERNATE POWER ON FACILITY , to it, and invented this *AUTO Resume Uploader. In this uploader. after it user not need to stay on computer screen and do other work. I have created these uploader in many technologies(JSP/SERVLET, PHP, ASP.NET) and many flavors.

java file upload, java file uploader