Java image processing skills four
Author: Cherami
Email: cherami@163.net
For some other works, please see: http://www.smiling.com.cn/group/homepage.ecgi? Group_ID = 23141
The SourceImage used in the following code is an already existing image object
Image cut
For an existing image object, you have to get a partial image, you can use the following steps:
// Import java.awt. *;
// Import java.awt.image. *;
Image croppedImage;
ImageFilter Cropfilter;
Cropfilter = New CropImageFilter (25, 30, 75, 75); // The four parameters are image start point coordinates and width, namely CropImageFilter (int X, int y, int width, int hotht), please refer to the API
Croppedimage = Toolkit.getDefaultToolkit (). CreateImage (SourceImage.getsource (), cropfilter);
// If you are used in the subclass of Component, you can remove the Toolkit.GetDefaultToolkit ().
// FilteredImagesource is an ImageProducer object.
Image zoom
For an existing image object, get a zoomable image object to use Image, you can use the getScaledInstance method:
Image scaledImage = sourceImage. GetScaleDinstance (100, 100, image.scale_default); // Get a 100x100 image
Image DoubledImage = SourceImage (SourceImage.Getwidth (this) * 2, sourceimage.getHeight (this) * 2, image.scale_default); // Get twice the image zoom, this program is usually used in a Swing component And class JComponent implements image observer interface ImageOBserver, all of which you can use.
// Please refer to the API if otherwise.
Grayscale transformation
The following procedure uses three methods to gray transformation of a color image, and the effect of the transformation is different. In general, the algorithm for grayscale transform is to get the three color components of the pixel to get the grayscale value, and then assign the value to the red green blue, so that the color achieved is ash Degree. The other is to take the maximum value in the red green blue three colors as the gradation value. The Java core package also has an algorithm, but there is no view of the source code, I don't know what the specific algorithm is like, the effect is different.
/ * 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 (ModeelStyle == 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) {
SURCEMODEL.GETPIXELSIZE ());
This.SourceModel = SourceModel;
ModelStyle = 0;
}
Public graymodel (ColorModel Sourcemodel, Int style) {
SURCEMODEL.GETPIXELSIZE ());
This.SourceModel = SourceModel;
ModelStyle = style;
}
Public void setgrayStyle (int style) {
ModelStyle = style;
}
Protected Int getGrayLevel (int Pixel) {
IF (ModeelStyle == CS_MAX) {
Return Math.max (SourceModel.getred (Pixel), Math.max (SourceModel.getGreen (Pixel), SourceModel.getBlue (Pixel));
}
Else IF (ModelStyle == CS_FLOAT) {
Return (int) (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 = getGrayLVEL (Pixel);
Return (GRAY << 16) (GRAY << 16) Gray;}
}
If you have your own algorithm or want to achieve a special effect, you can modify the method of GrayModel getGRAYLVEL ().
Color transformation
According to the principles above, we can also achieve color transform, so the effect is much. Below 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) {
SURCEMODEL.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 (Pixel) << 24) (GETREEN (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) {
SubstitutionColormodel (CM, New ReverseColormodel (CM));
}
Public int filterRGB (int X, int y, int pixel) {
Return Pixel;
}
}
If you want to get your own effect, you need to modify the three methods in ReverseColorModel.java, getred, getGreen, getBlue.
Here is a total demo program for 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, 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.waitForll ();
} 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.setrioneringHINTS (RHS);
Srcg.drawImage (Source, 0, 0, Null);
Colorspace grayspa = 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);
}
}