Wednesday, April 8, 2009
Convert Colored Images to Black And white in java
Tuesday, March 31, 2009
Dashed line drawing in java graphics.
Friday, February 13, 2009
Converting Image to BufferedImage in JAVA
Hi,
- For opening image i have to use java.awt.Image class.
- For saving drawn image to jpg i have to use ImageIo class.
- So for passing ImageIO.write() method i need image object to bufferedImage object.
/*
* 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
/*
* 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);
}
}
}

Slide Show in java
Hi,
/*
* 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
/*
* 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);
}
}
}