Showing posts with label java imaging. Show all posts
Showing posts with label java imaging. Show all posts

Wednesday, April 8, 2009

Convert Colored Images to Black And white in java


Hi,

Sometime you want to convert colored images to black and white.
Here i am giving you the simple method to do so.
Just see below.

//example for coverting colored image to black and white
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;

class BlackAndWhite
{
          public static void main(String asr[])
          {
                   try
                   {
                             //colored image path
                             BufferedImage image = ImageIO.read(new  
                                                File("D:\\MyProgram\\output.jpg"));

                             //getting width and height of image
                             double image_width = image.getWidth();
                             double image_height = image.getHeight();

                             BufferedImage bimg = null;
                             BufferedImage img = image;

                             //drawing a new image      
                             bimg = new BufferedImage((int)image_width, (int)image_height,
                                                                    BufferedImage.TYPE_BYTE_GRAY);
                             Graphics2D gg = bimg.createGraphics();
                             gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

                             //saving black and white image onto drive
                             String temp = "blackAndwhite.jpeg";
                             File fi = new File("D:\\My Program\\" + temp);
                             ImageIO.write(bimg, "jpg", fi);
                   }
                   catch (Exception e)
                   {
                             System.out.println(e);
                   }
          }
}

Friday, February 13, 2009

Converting Image to BufferedImage in JAVA

Hi,


While working on some project i was bound in some situation.....
  1. For opening image i have to use java.awt.Image class.
  2. For saving drawn image to jpg i have to use ImageIo class.
  3. So for passing ImageIO.write() method i need image object to bufferedImage object.
It is not only one situation when you need this, Sometime when reading
image java.awt.Image class is very useful for opening images from
particular path.

So their are number of tricks to do so.
I am posting here my favorite way of conversion.
Just go through it.

First i loaded image in Image object via Toolkit class.
Then i created a blank BufferedImage object of same dimension.
Then i just draw Image object into BufferedImage object.
Now your BufferedImage object consist the same image.

For full source code follow the link:-

/*

* Program for Converting Image Object to BufferedImage Object

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

*/

import java.awt.*;

import java.awt.image.*;

class Convert

{

public static void main(String as[])

{

////opening image in Image Object

Image img = Toolkit.getDefaultToolkit().getImage("test.jpeg");

////taking dimension of image

int width = img.getWidth(null);

int height = img.getHeight(null);

////creating a bufferedImage object of same dimension as image

BufferedImage bimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

////taking graphics object from BufferedImage

Graphics2D gg = bimg.createGraphics();

////drawing Image objects image into graphics of buffered image object

gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

///disposing graphics.

gg.dispose();

}

}

Thursday, February 12, 2009

Highlight a selected portion of image in JAVA

Hi,

Here i am crerating a small application which just highlight a
area over image.

I am highlight a rectangular area.You can change as you required.
I am setting a color(255,255,0) for highlight.
You can change it according to your requrement.
Also if you want to change beta value of highlight area then change
the value of transparancy variable.

See the below screenshot where i am highlighted in yellow shed with
transparency value 80.

For full source code follow this link:-

/*

* Program for Highlighting a particular portion of image in any color

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

*/

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

class HighlightImage

{

public static void main(String a[])

{

try

{

////pass image file path open for drawing

BufferedImage image = ImageIO.read(new File("1.jpg"));

////taking graphics from image

Graphics g = image.getGraphics();

////just change the transparency value for changing beta values of image

int transparency = 50;

/////drawing string on graphics of opened image

g.drawString("We are highlighting a selected portion of image", 10, 10);

Color color = new Color(255, 255, 0, 255 * transparency / 100);

g.setColor(color);

g.fillRect(30, 30, 100, 100);

////saving image by name output.jpg

ImageIO.write(image, "jpg", new File("output.jpg"));

}

catch (Exception e)

{

System.out.println(e);

}

}

}

ScreenShot:-





Slide Show in java

Hi,


I am posting here a slide show demo in java.
Just change the path of folder containing images.

It is same as xp slide demo.You can just navigate all images by
next and previous button.
You have to just set the path containing lots of images in folder.

For the source code follow the link below

/*

* Program for implementing a Slide show in java

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*/

import javax.swing.*;

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.swing.*;

import java.awt.event.*;

class JSlideShow extends JFrame implements ActionListener

{

JLabel image;

File [] files;

int imageNo;

public JSlideShow()

{

super("SliderDemo by prashant chandrakar");

///for setting folder path containing images

setImageArray();

if(files.length > 0)

image = new JLabel(new ImageIcon(files[0].toString()));

////buttons for navigating images

JButton pre = new JButton("Prev");

pre.setActionCommand("pre");

pre.addActionListener(this);

JButton next = new JButton("Next");

next.setActionCommand("next");

next.addActionListener(this);

JPanel jp = new JPanel();

jp.add(pre);

jp.add(next);

this.setLayout(new BorderLayout());

this.getContentPane().add("North", image);

this.getContentPane().add("Center", jp);

this.setSize(1024, 768);

this.setVisible(true);

this.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowListener we)

{

System.exit(0);

}

});

}

public void setImageArray()

{

////just change the folder path containing images

File path = new File("d:/nondicom/images/");

files = path.listFiles();

}

public void setImage(int number)

{

image.setIcon(new ImageIcon(files[number].toString()));

}

public void actionPerformed(ActionEvent e)

{

if (e.getActionCommand().equals("pre"))

{

if (imageNo == 0)

{

imageNo = files.length-1;

}

else

{

imageNo--;

}

setImage(imageNo);

}

else if (e.getActionCommand().equals("next"))

{

if (imageNo == files.length-1)

{

imageNo = 0;

}

else

{

imageNo++;

}

setImage(imageNo);

}

}

public static void main(String a[])

{

new JSlideShow();

}

}

Wednesday, February 11, 2009

Drawing String on image

Hi,

Suppose you want to edit any image and want to write some text permanently
on image.

Here i posting code for do so.

It opens a image name test.jpg and write a text and then save on same place with name "output.jpg".

You just change the name of files as you required.

For source code see below

/*

* Program for drawing string or text over a image and save with that

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

*/

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

class DrawString

{

public static void main(String a[])

{

try

{

////pass image file path open for drawing

BufferedImage image = ImageIO.read(new File("test.JPEG"));

////taking graphics from image

Graphics g = image.getGraphics();

/////drawing string on graphics of opened image

g.drawString("I am a string drawn on image pixel", 10, image.getHeight() - 10);

////saving image by name output.jpg

ImageIO.write(image, "jpg", new File("output.jpg"));

}

catch (Exception e)

{

System.out.println(e);

}

}

}