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();