Java Image Processing Techniques

Source: Internet
Author: User
Java Image Processing Techniques
Java Image Processing Techniques
The sourceimage used in the following code is an existing image object.
Image Cutting
To obtain a local image of an existing image object, follow these steps:
// Import java. AWT .*;
// Import java. AWT. image .*;
Image croppedimage;
Imagefilter cropfilter;
Cropfilter = new cropimagefilter (, 75); // The four parameters are the coordinates of the image start point and width and height, that is, cropimagefilter (int x, int y, int width, int height ), for details, refer to API
Croppedimage = toolkit. getdefatooltoolkit (). createimage (New filteredimagesource (sourceimage. getsource (), cropfilter ));
// If it is used in the component subclass, you can remove the toolkit. getdefaulttoolkit () above.
// Filteredimagesource is an imageproducer object.
Image Scaling
For an existing image object, you can use the getscaledinstance method of the image to obtain a scaled image object:
Image scaledimage = sourceimage. getscaledinstance (100,100, image. scale_default); // obtain an image of X.
Image doubledimage = sourceimage. getscaledinstance (sourceimage. getwidth (this) * 2, sourceimage. getheight (this) * 2, image. scale_default); // get an image that is zoomed twice. This program is generally used in a swing component, and jcomponent class implements the image observer interface imageobserver, which can be used by all.
// For other information, see API.

Grayscale conversion
The following program uses three methods to perform gray-scale transformation on a color image, with different effects. In general, the gray scale conversion algorithm is to use R * 0.3 + G * 0.59 + B * 0.11 for the three color components of a pixel to obtain the gray scale value, and then assign it to red, green, and blue, in this way, the color effect is grayscale. The other is to take the maximum value in red, green, and blue as the gray value. The Java core package also has an algorithm, but it does not look at the source code and does not know what the specific algorithm is like. The effect is different from the above.
/* Grayfilter. Java */
/* @ Author: cherami */
/* Email: cherami@163.net */
Import java. AWT. image .*;

Public class grayfilter extends rgbimagefilter {
Int modelstyle;
Public grayfilter (){
Modelstyle = graymodel. cs_max;
Canfilterindexcolormodel = true;
}
Public grayfilter (INT style ){
Modelstyle = style;
Canfilterindexcolormodel = true;
}
Public void setcolormodel (colormodel cm ){
If (modelstyle = graymodel. cs_max ){
Substitutecolormodel (CM, new graymodel (CM ));
}
Else if (modelstyle = graymodel. cs_float ){
Substitutecolormodel (CM, new graymodel (CM, modelstyle ));
}
}
Public int filterrgb (int x, int y, int pixel ){
Return pixel;
}
}

/* Graymodel. Java */
/* @ Author: cherami */
/* Email: cherami@163.net */

Import java. AWT. image .*;

Public class graymodel extends colormodel {
Public static final int cs_max = 0;
Public static final int cs_float = 1;
Colormodel sourcemodel;
Int modelstyle;
Public graymodel (colormodel sourcemodel ){
Super (sourcemodel. getpixelsize ());
This. sourcemodel = sourcemodel;
Modelstyle = 0;
}
Public graymodel (colormodel sourcemodel, int style ){
Super (sourcemodel. getpixelsize ());
This. sourcemodel = sourcemodel;
Modelstyle = style;
}
Public void setgraystyle (INT style ){
Modelstyle = style;
}
Protected int getgraylevel (INT pixel ){
If (modelstyle = cs_max ){
Return math. Max (sourcemodel. getred (pixel), math. Max (sourcemodel. getgreen (pixel), sourcemodel. getblue (pixel )));
}
Else if (modelstyle = cs_float ){
Return (INT) (sourcemodel. getred (pixel) * 0.3 + sourcemodel. getgreen (pixel) * 0.59 + sourcemodel. getblue (pixel) * 0.11 );
}
Else {
Return 0;
}
}
Public int getalpha (INT pixel ){
Return sourcemodel. getalpha (pixel );
}
Public int getred (INT pixel ){
Return getgraylevel (pixel );
}
Public int getgreen (INT pixel ){
Return getgraylevel (pixel );
}
Public int getblue (INT pixel ){
Return getgraylevel (pixel );
}
Public int getrgb (INT pixel ){
Int gray = getgraylevel (pixel );
Return (getalpha (pixel) <24) + (gray <16) + (gray <8) + gray;
}
}
If you have your own algorithms or want to achieve special results, you can modify the graymodel-like method getgraylevel ().

Color Conversion
Based on the above principle, we can also achieve color conversion, which has a lot of effect. The following is an example of Reverse Transformation:
/* Reversecolormodel. Java */
/* @ Author: cherami */
/* Email: cherami@163.net */
Import java. AWT. image .*;

Public class reversecolormodel extends colormodel {
Colormodel sourcemodel;
Public reversecolormodel (colormodel sourcemodel ){
Super (sourcemodel. getpixelsize ());
This. sourcemodel = sourcemodel;
}
Public int getalpha (INT pixel ){
Return sourcemodel. getalpha (pixel );
}
Public int getred (INT pixel ){
Return ~ Sourcemodel. getred (pixel );
}
Public int getgreen (INT pixel ){
Return ~ Sourcemodel. getgreen (pixel );
}
Public int getblue (INT pixel ){
Return ~ Sourcemodel. getblue (pixel );
}
Public int getrgb (INT pixel ){
Return (getalpha (pixel) <24) + (getred (pixel) <16) + (getgreen (pixel) <8) + getblue (pixel );
}
}
/* Reversecolormodel. Java */
/* @ Author: cherami */
/* Email: cherami@163.net */

Import java. AWT. image .*;

Public class reversefilter extends rgbimagefilter {
Public reversefilter (){
Canfilterindexcolormodel = true;
}
Public void setcolormodel (colormodel cm ){
Substitutecolormodel (CM, new reversecolormodel (CM ));
}
Public int filterrgb (int x, int y, int pixel ){
Return pixel;
}
}
To achieve your own results, you need to modify the three methods in reversecolormodel. Java, getred, getgreen, and getblue.
The following is a general demo of the above effect.
/* Grayimage. Java */
/* @ Author: cherami */
/* Email: cherami@163.net */
Import java. AWT .*;
Import java. AWT. image .*;
Import javax. Swing .*;
Import java. AWT. color .*;

Public class grayimage extends jframe {
Image Source, gray, gray3, clip, bigimg;
Bufferedimage bimg, gray2;
Grayfilter filter, filter2;
Imageicon II;
Imagefilter cropfilter;
Int IW, ih;
Public grayimage (){
II = new imageicon ("images/11.gif ");
Source = II. getimage ();
IW = source. getwidth (this );
IH = source. getheight (this );
Filter = new grayfilter ();
Filter2 = new grayfilter (graymodel. cs_float );
Gray = createimage (New filteredimagesource (source. getsource (), filter ));
Gray3 = createimage (New filteredimagesource (source. getsource (), filter2 ));
Cropfilter = new cropimagefilter (5, iw-5, ih-5 );
Clip = createimage (New filteredimagesource (source. getsource (), cropfilter ));
Bigimg = source. getscaledinstance (IW * 2, ih * 2, image. scale_default );
Mediatracker Mt = new mediatracker (this );
Mt. addimage (Gray, 0 );
Try {
Mt. waitforall ();
} Catch (exception e ){
}
}
Public void paint (Graphics g ){
Graphics2d g2 = (graphics2d) g;
Bimg = new bufferedimage (IW, IH, bufferedimage. type_int_rgb );
Graphics2d srcg = bimg. creategraphics ();
Renderinghints RHS = g2.getrenderinghints ();
Srcg. setrenderinghints (RHs );
Srcg. drawimage (source, 0, 0, null );
Colorspace grayspace = colorspace. getinstance (colorspace. cs_gray );
Colorconvertop op = new colorconvertop (grayspace, RHS );
Gray2 = new bufferedimage (IW, IH, bufferedimage. type_int_rgb );
Op. Filter (bimg, gray2 );
G2.drawimage (source, 40, 40, this );
G2.drawimage (Gray, 80, 40, this );
G2.drawimage (gray2, 120,40, this );
G2.drawimage (gray3, 160,40, this );
G2.drawimage (clip, 40, 80, this );
G2.drawimage (bigimg, 80, 80, this );
}
Public void Update (Graphics g ){
Paint (g );
}
Public static void main (string ARGs []) {
Grayimage M = new grayimage ();
M. setsize (400,400 );
M. setvisible (true );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.