Thursday, August 8, 2013

Take Screen Shot in Java





The following code shows you how to take a screenshot using java.The heart of this code is the Robot class. This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
To use this class we need to import java.awt.Robot. In eclipse shift+ctrl+o will do all this import stuffs for you.

Here is the method to capture screen shot

/*
 * This method takes screen shot
 * @param filename is the file name of the screenshot
 */
 public static void captureScreen(String fileName) throws Exception {
   
                   //get the dimention (i.e. width & height in pixels) of the screen
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                   //Create a rectangular shape of size of the screen
     Rectangle screenRectangle = new Rectangle(screenSize);
                   //create a new robot object
     Robot robot = new Robot();
                   //take a snapshot of the screen
     BufferedImage image = robot.createScreenCapture(screenRectangle);
                   //write the image to file
     ImageIO.write(image, "jpg", new File(fileName));
   
 }

createScreenCapture(Rectangle screenRect)creates an image containing pixels read from the screen & returns it as a BufferedImage. You can change the image format from jpg to any other format you like.


For more info on Robot class visit oracle docs here


Wednesday, August 7, 2013

Download torrent anonymously using Tor Browser


Now a days torrent is banned in may countries & many institutes. For this reason we cannot download many useful file too.
Using Tor Browser we can download torrents anonymously.

What is Tor?

Tor (originally short for The Onion Router)[5] is free software for enabling online anonymity. Tor directs Internet traffic through a free, worldwide volunteer network consisting of more than three thousand relays[6] to conceal a user's location or usage from anyone conducting network surveillance or traffic analysis. Using Tor makes it more difficult to trace Internet activity, including "visits to Web sites, online posts, instant messages and other communication forms", back to the user [7] and is intended to protect users' personal privacy, freedom, and ability to conduct confidential business by keeping their internet activities from being monitored. ( Read More on Wikipedia)

What is u torrent?

It is probably the most popular light-weight bit torrent client.the program was designed to use minimal computer resources while offering functionality comparable to larger BitTorrent clients such as Vuze or BitComet.The program has received consistently good reviews for its feature set, performance, stability, and support for older hardware and versions of Windows.


How to use tor to download torrent anonymously

Step 1: Download Tor Browser from here Download Tor

Step 2: Download u-torrent from here Download u torrent

Step 3: Open the uTorrent and go to Options --> Preferences (ctrl+P) --> Connection and change the settings as shown in the screen shot.

Step 4: Now run tor browser "Start tor browser.exe" from the folder where you extracted it.

Step 5: Start tor browser.When the tor browser starts it will open firefox and this message will be displayed -

Now you can download torrent anonymously. Enjoy...


This article is for educational purpose, author is not responsible for any kind of misuse of this knowledge.

Tuesday, August 6, 2013

Epub Reader





EPUB (short for electronic publication) is a free and open e-book standard by the International Digital Publishing Forum (IDPF). Files have the extension .epub.

EPUB is designed for reflowable content, meaning that an EPUB reader can optimize text for a particular display device. EPUB also supports fixed-layout content. The format is intended as a single format that publishers and conversion houses can use in-house, as well as for distribution and sale. It supersedes the Open eBook standard. (source wikipedia)


Five Best EPUB Readers for PC:


Firefox plugin

This is one of the most popular tool for reading epub files in windows.Very simple interface & customizable.



Cool Reader

Supports epub (non-DRM), fb2, txt, rtf, html, chm, tcr, doc, pdb formats.
Other features:

  • Pages or scroll view
  • Table of contents
  • Bookmarks
  • Text search
  • Hyphenation dictionaries
  • Most complete FB2 format support: styles, tables, footnotes
  • Additional fonts support (.ttf)
  • Can read books from zip archives
  • Automatic reformatting of .txt files (autodetect headings etc.)
  • Styles can be customised in wide range using external CSS
Moreover there are six good looking page skins. Give it a try.



FBReader

FBReader is free e-book reader for Android, Linux, Mac OS, Windows, Unix, and other Mobile devices, Its open-source eBook reader that can also support .ePub files.



Calibre

Calibre is a free and open source e-book library management application.
Features:

  • Library Management
  • E-book conversion
  • Syncing to e-book reader devices
  • Downloading news from the web and converting it into e-book form
  • Comprehensive e-book viewer
  • Content server for online access to your book collection
This software is really good.


magicscroll.net

This is a online reader. No need to install any software,you just need a working internet connection.


Happy Reading


Monday, August 5, 2013

Resize image in Java


Java code for re-sizing all images in a folder


If you use digital camera then you may have noticed that the image format is jpeg(a highly compressed image format)
but still image size is very large.This is because the resolution of image is very high.
We generally do not need such high resolution images.Moreover if we want to store images
in cloud drive (like Google Drive,Dropbox,SkyDrive etc). So here is a java code that will resize
all images in the source directory and save them in the destination directory. User can specify the
ratio of resizing.

Resize.java

/*
 * @author: Arabinda Moni
 * This code resizes all images in a directory with a given ratio
 * You are free to use this code & edit this code.
 * */

package image.resize;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class Resize {
 
 private String source; //path of the source directory
 private String dest;   // path of the destination directory
 private ArrayList inclusion;  //this arraylist contains image foramats to be processed 
 private int count=0;
 
 public Resize(String source,String dest,ArrayList inclusion){
  this.source=source;
  this.dest=dest;
  this.inclusion=inclusion;    
 }
 
 
 /*
  * @param ratio is resize ratio
  * For example: if you want to reduce the image to half of its resolution then use ratio=0.5
  * */
 public void resizeAll(double ratio) throws IOException{
  long startTime=System.currentTimeMillis();
  scanDir(new File(source), ratio);
  System.out.println((System.currentTimeMillis()-startTime)+ "  "+count);
 }
 
 /*
  * This method resizes image f 
  * @param f is file to be resized
  * @param ratio is ratio of resizing
  * */
 public void resizeImage(File f,double ratio) throws IOException{
  count++;
  // use BufferedImage to read the image file
  BufferedImage img=ImageIO.read(f);
  //calculation of height & width of the new image
  int width=(int)(img.getWidth()*ratio);
  int height=(int)(img.getHeight()*ratio);
  //Scaling is done here with
  Image newimg=img.getScaledInstance(width, height, Image.SCALE_FAST); 
  BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  
  // here the new image is created
  Graphics2D g2 = bi.createGraphics();
  g2.drawImage(newimg, 0, 0, null);
  g2.dispose();
  
  //write image to file
  ImageIO.write(bi, "jpg",new File(dest+"\\"+f.getName()));
    }
 
 /*
  * This method scans the source directory & sub-directories 
  * for images of the included format
  * */
 public void scanDir(File f,double ratio) throws IOException{
  System.out.println(f.getName());
  if(f.listFiles()!=null){
   for(File files : f.listFiles()){
    scanDir(files,ratio);
   }
  } 
  else if(inclusion.contains(f.getName().substring(f.getName().lastIndexOf('.')+1))){
   resizeImage(f,ratio);
  }
 }
    
}


Here goes the driver program
Run.java

package image.resize;

import java.io.IOException;
import java.util.ArrayList;

public class Run {
   public static void main(String args[]) throws IOException{
    ArrayList inc=new ArrayList<>();
    inc.add("jpg");
    inc.add("JPG");
    inc.add("jpeg");
    inc.add("JPEG");
    inc.add("png");    
    Resize res=new Resize(YourSourceDirectory, YourDestinationDirectory, inc);
    res.resizeAll(0.5);    
   }
}

:) Happy Coding.. :)

Sunday, July 28, 2013

Use Linux Commands in Windows





Do you love Linux shell commands & miss them while using windows?
Now you can use Linux shell commands like touch, cat, grep etc from your windows command prompt. Just install  GNU core utilities  in your windows machine and your command prompt becomes magical.
Follow these simple steps:
  1. Download the core utilities from here   http://ftp.gnu.org/gnu/coreutils/coreutils-8.21.tar.xz
  2. Clicking on this link downloads a tar.xz file. Use 7zip to extract it. http://www.7-zip.org/
  3. Edit the path environment variable and add the path of the bin folder. For example if your bin folder is in C:\Program Files\GnuWin32\ then add C:\Program Files\GnuWin32\bin in the ‘path’ environment variable.  This post shows how to set environment variable in windows 

How to change environment variable in windows

Here are a sequence of images which will guide you in this tour. Images are self descriptive. For any confusion or feedback drop a comment below.




  1.  This is a folder where gnu core utilities are stored. We will create an entry to the 'path' variable so that we can use these commands from command prompt.



  

 



                                                                                                                
   




  





Do not forget to put a semicolon( to separate the new entry in the path variable from the previous) before you add this new entry.

Enjoy!!!!!!!!