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

6 comments:

  1. thank you very much for sharing the information ... god bless you

    ReplyDelete
  2. very nice Mr.Rajeev

    ReplyDelete
  3. Which part of your code effects in the random arrangement of the sub images? I need to get the sub images of the original image as it is. Could you please let me know?

    ReplyDelete
    Replies
    1. Change the position of J and I in this loop :

      for(int j=0;j<3;j++){
      BufferedImage wim=bimg.getSubimage(j*w/3,i*h/3, w/3, h/3);
      Image sc=wim.getScaledInstance(frame.getWidth()/3,


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

      Delete
  4. hey this is awesome work...i needed such a program to present in my seminar..could u plz send me a presentation for this or full explanation of the code so that i cn understand and present it..i will b vry thankful 2 u..my id is-rockrocker1510@gmail.com

    ReplyDelete
  5. hi i have query regarding image processing:-i have image and divided into many equal parts 2)take one image from the divided part 3)compare it with the original image and tell me where it is located in the original image this is mine query ...can u tell me the logic for this one how to do in java
    thanking you

    ReplyDelete

Your comment may wait for moderation....

DMCA.com Protected by Copyscape Online Plagiarism Tool