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 ().