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:-





No comments:

Post a Comment