January 29, 2013

Thread Enumerator Example

Java example to set condition in Thread 


package yahoofinance;

public class ThreadExamples implements Runnable {
   
    public enum Traffic { NORTH, SOUTH, EAST, WEST, NOTHING }
    static Traffic isOn = Traffic.NOTHING;
    @Override
    public void run() {
        while(true){
            switch (isOn) {
            case NORTH:
                System.out.println(" north is on ");
                break;
            case SOUTH:
                System.out.println(" south is on ");
                break;
            case EAST:
                System.out.println(" east is on ");
                break;
            case WEST:
                System.out.println(" west is on ");
                break;
            case NOTHING:
                System.out.println(" default is on ");
                break;
            default:
                System.out.println(" default is on ");
                break;
            }
        }
       
    }
   
    public static void main(String[] election) throws InterruptedException {
         ThreadExamples t = new ThreadExamples();
         Thread th = new Thread(t);
         th.start();
         th.sleep(5000);
         isOn = Traffic.NORTH;
         th.sleep(1000);
         isOn = Traffic.WEST;
    }

}

How to manipulate/control a running thread from another class.



http://belazy.blog.com/

January 25, 2013

How to create/add facebook share button to your website

How to add a facebook share button to your blog/website

Description : add the following html tag to your website/blog and change the url


<a  class="free facebook apps" shareLink id="faceBookshare" href="http://www.facebook.com/sharer.php?u=http://javabelazy.blogspot.in/" title="share this post to your facebook timeline" >Facebook like box</a>


the above html code add a social networking link to your website

read more

facebook

Java Facebook API Documentations
Most used Java Facebook API

Description : To add google share button to your blog/website add the following html code and change the url


<div class="g-plus" data-action="share" data-annotation="bubble" data-href="http://javabelazy.blogspot.in/"></div>


http://belazy.blog.com/

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/

January 04, 2013

JSTL : Java Standard Tag Library

Alternate Solution for JSP tags in JSTL




Jstl equivalent for ('<%=request.getParameter("id")%>') :: ${param.id} and ${param.["id"]}

Jstl equivalent for  session :: ${sessionScope.Object.variableName}


You need to add the jstl jar to your project, Before using the above codes

Download link

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

To know How to use jstl inside your web application click here

http://belazy.blog.com/

Facebook comments