Showing posts with label java io examples. Show all posts
Showing posts with label java io examples. Show all posts

October 04, 2013

Java code to get screen shot

How to get screen shot in java



 /**
 * @author Jim Brown
 *
 */

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ScreenShotCapture {
   
    private static final String DIR_NAME = "SouthPark";

    public static void main(String[] shutdown)  {
        ScreenShotCapture captureSS = new ScreenShotCapture();
        captureSS.createDirectory(DIR_NAME);
        try {
            captureSS.getScreen();
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void createDirectory(String dirName) {
        File newDir = new File("D:\\"+dirName);
        if(!newDir.exists()){
            boolean isCreated = newDir.mkdir();
        }
}

    private void getScreen()throws AWTException, IOException {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimensions = toolkit.getScreenSize();
        Robot robot = new Robot();  // Robot class
        BufferedImage screenshot = robot.createScreenCapture(new Rectangle(dimensions));
        ImageIO.write(screenshot, "png", new File("D:\\"+DIR_NAME+"\\Gravity.png"));
   
   }
}

Hi Friends,

         This is a small program named Xkeycode  that enables you to capture system screen. Once you run the code it will capture the screen and save  and png image in your system. so please try it out..


February 27, 2013

Delete File in java

How to delete a file in java



package blackberry;

import java.io.*;
 
public class DeleteFile

{
 
private static void deletefile(String file)
{
 
File f1 = new File(file);

boolean success = f1.delete();

if (!success)
{
 
System.out.println("Deletion failed.");

System.exit(0);
}

else
{
System.out.println("File deleted Successfully.");
}

}
 
public static void main(String[] ran)

{
 
    switch(args.length)
{
 
case 0: System.out.println("File has not mentioned.");
 
          System.exit(0);
 
case 1: deletefile(ran[0]);

         System.exit(0);
 
default : System.out.println("Multiple files are not allow.");
 
            System.exit(0);
  
 }
 
}

}

.ini file


Gutmann algorithm for secure file deletion read here

java blog

January 09, 2013

Image comparison in java using RGB value


How to compare (RGB value) two images using java


This example helps you to compare two images (either jpeg or png) using pixel. first the program check whether two images are of equal breadth and width, then it compare each pixel by pixel and return the output. The program will check whether the two images given resembles each other or not. This is one of the simplest way and pixel based.


package image.analysis.comparison;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/*
*  Image comparison done here is using pixels
*
*/

public class ImageComparison {
   
   
    public boolean compareTwoImages(File fileOne, File  fileTwo) {
        Boolean isTrue = true;
        try{
            Image imgOne = ImageIO.read(fileOne);
            Image imgTwo = ImageIO.read(fileTwo);
            BufferedImage bufImgOne = ImageIO.read(fileOne);
            BufferedImage bufImgTwo = ImageIO.read(fileTwo);
            int imgOneHt = bufImgOne.getHeight();
            int imgTwoHt = bufImgTwo.getHeight();
            int imgOneWt = bufImgOne.getWidth();
            int imgTwoWt = bufImgTwo.getWidth();
            if(imgOneHt!=imgTwoHt ||(imgOneWt!=imgTwoWt)){
                System.out.println(" size are not equal ");
                isTrue = false;
            }
            for(int x =0; x< imgOneHt; x++ ){
                for(int y =0; y <imgOneWt ; y++){
                    if(bufImgOne.getRGB(x, y) != bufImgTwo.getRGB(x, y) ){
                        System.out.println(" size are not equal ");
                        isTrue = false;
                        break;
                    }
                }
            }
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return isTrue;
    }
   
   
    public static void main(String[] args) {
       
        File f = new File("E:\\javaaptitude\\Oracle.jpeg");
        File f2 = new File("E:\\javaaptitude\\Capgemini.jpeg");
        ImageComparison imgComp = new ImageComparison();
        System.out.println(imgComp.compareTwoImages(f, f2));
       
    }
   

}


The above code is for image having same width and height, if the image height and width is different use this code


package Bluetick;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class ImageComparison {
  
  
    public boolean compareTwoImages(File fileOne, File  fileTwo) {
        Boolean isTrue = true;
        try{
            Image imgOne = ImageIO.read(fileOne);
            Image imgTwo = ImageIO.read(fileTwo);
            BufferedImage bufImgOne = ImageIO.read(fileOne);
            BufferedImage bufImgTwo = ImageIO.read(fileTwo);
            int imgOneHt = bufImgOne.getHeight();
            int imgTwoHt = bufImgTwo.getHeight();
            int imgOneWt = bufImgOne.getWidth();
            int imgTwoWt = bufImgTwo.getWidth();
            if(imgOneHt!=imgTwoHt ||(imgOneWt!=imgTwoWt)){
                System.out.println(" size are not equal ");
                isTrue = false;
            }

            for(int x =0; x < imgOneWt; x++ ){ //replace the loop, if needed
                for(int y =0; y < imgOneHt ; y++){
                    if(bufImgOne.getRGB(x, y) != bufImgTwo.getRGB(x, y) ){
                        System.out.println(" rgb are not equal ");
                        isTrue = false;
                        break;
                    }
                }
            }
        }catch (IOException e) {
                        e.printStackTrace();
        }
        return isTrue;
    }
  
  
    public static void main(String[] softwareEngineer) {
      
        File OracleJava = new File("C:\\Image\FingerPrint\analysis.jpg");
        File javaOracle = new File("C:\\Histogram\match\image.jpg");
        ImageComparison imgComp = new ImageComparison();
        System.out.println(imgComp.compareTwoImages( OracleJava , javaOracle));
      
    }
  

}

from comments :-

To work the above code for different dimensional images the for loop has to replaced.

Change the for loop to the following .... for(int x =0; x < imgOneWt; x++ ){ for(int y =0; y < imgOneHt ; y++){ if(bufImgOne.getRGB(x, y) != bufImgTwo.getRGB(x, y) ){ System.out.println(" size are not equal "); isTrue = false; break; } } }


Hope you had tried the above code. Read this for to know how to create a captcha image in java : java be lazy

Thanks all for your valuable feed backs,

+jerin v george





http://javabelazy.blogspot.in/

March 17, 2012

Audio chatting example in java

Audio chatting example in java

The program show a small demonstration of how Audio chat works in java. Click on the link to get the code. Two or more user can audio chat with the application. The program is developed using networking and java packages.
 

Program


Please try audio chatting program... dont forget to post your feed back.

did you ever tried two way chatting using java code. Here we use socket communication, thread for this program. click the below link to download the code. javablzy

Thanking you....


you can mail me to belazy1987atgmaildotcom.

Facebook comments