Posts which you will also like.

Thursday, August 30, 2012

Java program to divide an image into multiple equal parts



In this post I am going to demonstrate how to divide an image into equal parts.Here I have used simple Java image api to manipulate the source image via techcresendo.com
In this java program for image dividing ,I have first divided the image into smaller equal size parts and placed them randomly on the buttons on the frame using Grid Layout .
Input Image:
img

This program can be used further to develop sliding game puzzle in java.
package com.techy.rajeev.divideImage;

import java.awt.EventQueue;
import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class MainWin {

    private JFrame frame;
    private JButton []jb=new JButton[9];
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWin window = new MainWin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MainWin() {
        try {
            initialize();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Initialize the contents of the frame.
     * @throws IOException 
     */
    private void initialize() throws IOException {
        frame = new JFrame();
        frame.setBounds(100, 100, 612, 519);
        setImage();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(3, 3));
        for(int i=0;i<9;i++){
            frame.add(jb[i]);
        }
    }

    public void setImage() throws IOException{
        URL img=MainWin.class.getResource("/img/img.jpg");
        BufferedImage bimg=ImageIO.read(img);
        int w=bimg.getWidth();
        int h=bimg.getHeight();
        int count=0;
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                BufferedImage wim=bimg.getSubimage(i*w/3,j*h/3, w/3, h/3);
                Image sc=wim.getScaledInstance(frame.getWidth()/3,


               frame.getHeight()/3, Image.SCALE_AREA_AVERAGING);
                setupImage(count++,sc);
            }
        }
    }

    private void setupImage(int i,Image wim) {
        jb[i]=new JButton(new ImageIcon(wim));
    }

}


Output:


imgdivide

Read More ->>

Tuesday, August 28, 2012

Extracting text from images




In this blog post we are going to discuss about the tools and api for extracting the text from the image.Some of these tools and apis are free and open source but some of them are not free but still you can send request to the company to get developer’s version for free.
Java OCR:
Java OCR is a simple and efficient java based open source library for image processing and optical character recognition.This library can also be used in the development of android based project.
265598
If you want to test it just click here to download it and give it a try.
Tesseract-OCR:
This is the one of the best optical character recognition open source library available .It was originally developed by HP Labs but now supported by Google.It is the one of the most accurate OCR engine. It can read wide variety of image formats and text read can be converted into different languages also.As of now It supports more than 40 languages and new languages are being implemented.
It is developed in c++ and if you are a java developer then don’t worry you may  be able find some JNI wrapper for it.
Tess4j is one of the available java JNI wrapper of tesseract-OCR.
There are some graphical front end already available.If you want  to check the functionality of Tesseract-OCR you can download gImageReader,VietOCR .Other than them there are many more GUI front end are available on the web.
blg
Asprise OCR for Java:  
Asprise Lab provides Asprise OCR SDK for development of OCR in java.It also provides the web based solution for optical character recongition.It enables us to equip our Java applications (Java applets, web applications, standard applications, J2EE enterprise applications) with optical character recognition (OCR) ability.
bc
But this SDK is not free, for commercial usage you need to buy the License.
Via - techcresendo.com
Read More ->>

Saturday, August 18, 2012

Java Tutorial:Sorting list using Comparator


In this java tutorial we are going to understand the sorting of a List with the help of Comparator interface.

So we will use sort(arrayToSort,Comparator) of Collections and Arrays to sort the list.Now we have to create a class which implements the comparator interface but before that we should have the class for which sorting is done.So lets create that first.

package com.techy.rajeev;

public class Employee{
    private String ename;
    private int eid;
    private double salary;

    public String getEname() {
        return ename;
    }

    public Employee(String ename, int eid, double salary) {
        super();
        this.ename = ename;
        this.eid = eid;
        this.salary = salary;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee [ename=" + ename + ", eid=" + eid + ", salary="
                + salary + "]";
    }

}


Now lets create the Sorter class.


package com.techy.rajeev;

import java.util.Comparator;

public class SortByName implements Comparator<Employee> {

    @Override
    public int compare(Employee o1, Employee o2) {
        return o1.getEname().compareTo(o2.getEname());
    }

}


Now lets do the sorting


package com.techy.rajeev;

import java.util.*;

public class TestSorting {
    public static void main(String[] args) {
        ArrayList<Employee>al=new ArrayList<Employee>();
        al.add(new Employee("Anonymous",1,12345.00));
        al.add(new Employee("Hacker",2,32423));
        al.add(new Employee("Cba",3,23434));
        System.out.println("Before:"+al);
        SortByName sbn=new SortByName();
        Collections.sort(al,sbn);//passing the sorter to sort
        System.out.println("After:"+al);
        
    }

}

Output:

Before:[Employee [ename=Anonymous, eid=1, salary=12345.0], Employee [ename=Hacker, eid=2, salary=32423.0], Employee [ename=Cba, eid=3, salary=23434.0]]

After:[Employee [ename=Anonymous, eid=1, salary=12345.0], Employee [ename=Cba, eid=3, salary=23434.0], Employee [ename=Hacker, eid=2, salary=32423.0]]

Now I hope you know how to sort the list using comparator.Keep reading.Thumbs up
Read More ->>

JAVA Tutorial:Sorting list using comparator and comparable interface


In this Java Tutorial We are going to discuss about the ways in which we can do the sort the objects of our own classes using comparator and comparable interfaces.

Some times we come across scenarios in which we needs to sort a list of non primitive  object.Suppose you created a class Employee and now you want to sort the list of employees based on their salary or say based on their name or in tons of other parameter.
Java provides us a very simple and efficient way to do it ,using utility classes(inside util packages).
So we can take the help of methods of Collections or Arrays utility class.Now see the signature of the sort methods provided by these two classes to get to know about Comparable and Comparator interfaces.

1.Collections sort method:
  • void sort(List<T> list): This says that hey! pass me the List containing the objects to sort but ensure that objects must also tell me the attribute based on which sorting needs to be done.This condition can be fulfilled by implementing the interface Comparable and overriding its compareTo() method by the class whose objects we want to sort.Problem with this way of sorting is that we can sort using only one way since a class can implement a interface only one. 
  • void sort(List<T>list,Comparator<? super T> comp):This provides the solution to previous drawback.Just visualize what sort method says in this case.It says: I need the list of object to be sorted plus an implementation of Comparator interface.So we have to create an extra class which implements the Comparator interface and overrides the compare() method.You may ask what is the benefit of creating the extra class.Yes there is the benefit now we can do sorting based on multiple attributes(how?). I hope you got the answerSmile.Make sure to write import java.util.Comparator; to import Comparator.

2.Arrays sort method:
  • Arrays sort method supports only 2nd version i.e void sort(List<T>list,Comparator<? super T> comp) for objects.
  • It has lots of variations of sort methods for primitives.
Note: These sort methods are based on the merge sort algorithm.So if you need time and space complexity better than this you need to implement your own algorithm of sorting.
So lets proceed by creating our Employee class.In this example we will implement the Comparable interface.


package com.techy.rajeev;
public class Employee implements Comparable{
    private String ename;
    private int eid;
    private double salary;
    public Employee(String ename, int eid, double salary) {
        super();
        this.ename = ename;
        this.eid = eid;
        this.salary = salary;
    }
    public String getEname() { return ename; } 
    public void setEname(String ename) {
        this.ename = ename;
    }

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "Employee [ename=" + ename + ", eid=" + eid + ", salary="
                + salary + "]";
    }




@Override public int compareTo(Employee o) { //We are sorting based on the employee name return ename.compareTo(o.getEname()); } }



Now lets do the sorting and displaying it.

javapackage com.techy.rajeev;
import java.util.*;
public class TestSorting {
    public static void main(String[] args) {
        ArrayList<Employee>al=new ArrayList<Employee>();
        al.add(new Employee("Anonymous",1,12345.00));
        al.add(new Employee("Hacker",2,32423));
        al.add(new Employee("cba",3,23434));
        al.add(new Employee("ebc",4,32432));
        System.out.println("Before:"+al);
        Collections.sort(al);
        System.out.println("After:"+al);
        
    }

}


Output:


Before:[Employee [ename=Anonymous, eid=1, salary=12345.0], Employee [ename=Hacker, eid=2, salary=32423.0], Employee [ename=Cba, eid=3, salary=23434.0], Employee [ename=Ebc, eid=4, salary=32432.0]]

After:[Employee [ename=Anonymous, eid=1, salary=12345.0], Employee [ename=Cba, eid=3, salary=23434.0], Employee [ename=Ebc, eid=4, salary=32432.0], Employee [ename=Hacker, eid=2, salary=32423.0]]




This was the sorting using Comparable interface.Now you can try the sorting using Comparator or keep reading,I will provide the implementation in next Java Tutorial on using Comparator .
Read More ->>

Sunday, August 12, 2012

Demonoid shuts down !



One of the most popular Bitttorrent  tracker site Demonoid.com was shut down by Ukrainian authorities on August 6,2012 from the Ukraine's largest data center ColoCall after 9 years of its operation (launched in 2003).
demonoid-logo-techyrajeev

Ukrainian officials did that to show that they are taking U.S intellectual property rights very seriously.According to sources server was taken down by massive distributed denial of service(DDoS) attack.
In last few days site was inaccessible and was redirecting to some random sites.You might have noticed following error message:


Server too busy
The action you requested could not be completed because the server is too busy.
Please try again in a few minutes
Do not click reload - Use the following links
Clicking reload will get you this page again
Click here to return to the homepage or Click here to go back

In the past Demonoid frequently changed its location to avoid of being into too much focus.Also its registration were done through invitation or on some particular days of month.Demonoid’s current Alexa rank  was 986 (Aug,2012).It’s major earning source was through advertisements.
Demonoid_screenshot
Demonoid servers may have been shut down but ,it’s administrator has promised its users that Demonoid would be back very soon.Hope for the best Thumbs up.
Read More ->>

Friday, August 3, 2012

Passing Command Line Argument in Eclipse


Passing command line arguments in Eclipse is very easy.We will see step by step procedure to pass command line arguments to a java program using eclipse.

package com.techy.rajeev;

class BadFoodException extends Exception {

}
class Myexception{
    public static void checkfood(String food)throws BadFoodException{
        if(food.equals("Veg")){
            System.out.println("Good food :)");
        }else{
            throw new BadFoodException();
        }
    }
    public static void main(String args[]){
        for(String s: args){
            try {
                checkfood(s);
            } catch (BadFoodException e) {
                e.printStackTrace();
            }
        }
        System.out.println("survived");
    }
}


This is the java program for which we want to pass command line arguments.

Write this program in eclipse IDE and save it.

Now click on the small dropdown icon beside run icon as shown in image:

eclipse1

A drop down menu will appear.Now click on Run Configurations.


eclipse


Now a new window will appear.This window provide us the settings for the run time configurations of a program.


In this window click on the arguments tab and supply the arguments needed by your program separated via space as delimiter.


eclipse3

Thats it.

More on this at techcresendo.com
Read More ->>
DMCA.com Protected by Copyscape Online Plagiarism Tool