Java image processing skills four

zhaozj2021-02-16  81

The SourceImage used in the following code is an existing Image object image cut for an existing image object, to get a partial image, you can use the following steps: // Import java.awt. *; // Import java.awt.image. *; image croppedimage; imagefilter = new cropImagefilter (25, 30, 75, 75); // The four parameters are image starting coordinates and width, namely CropImageFilter (int x, int x, int x, int x, int x, int y, int width, int height), details, see API CroppedImage = Toolkit.getDefaultToolkit) () createImage (new FilteredImageSource (sourceImage.getSource (), cropFilter.); // if it is used in a subclass of Component can Toolkit.getDefaultToolkit () will be removed. // FilteredImagesource is an ImageProducer object. Image object image scaling for an existing give it a scaled Image Image object can use methods of getScaledInstance: Image scaledImage = sourceImage getScaledInstance (100,100, Image.SCALE_DEFAULT); // get a 100X100 image Image doubledImage = sourceImage. GetscaledInstance (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 Implementing image observer interface ImageOBserver, all you can use this. // Please refer to the API if otherwise. The gradation transformation The following programs use three methods to gray transform 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 (modelStyle == GrayModel else if (modelStyle == GrayModel} public int FilterRGB (int X, int y, int pixel) {return pixel;}} / * graymodel.java * / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / *; 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; model}} public graymodel, int style {super (SourceModel.getpixelsize ()); this.SourceModel = SourceModel; ModelStyle = style;} pu blic 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 (model == cs_float) {return (int) * 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 algorithm or want to get a special effect, you can modify the method of graymodel getGRAYLVEL (). The color transform is based on the principles above, we can also achieve color transformation, so the effect is much. Here is an example of reverse transformation: / * ReverseColorModel.java * / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / ((((((((((/) NAIDE, PUBLIC CLORMODEL { 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) (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 Reverseco Lormodel (cm));} public int filterRGB (int X, int y, int pixel) {return Pixel;}} To achieve 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.

/ * @ Author: Cherami * / / / / / / / / / / / / / / / / / / / / / /! 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 (Graphi cs 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 G2.DrawImage (CLIP, 40, 80, this); g2.drawimage (Bigimg, 80, 80, this);

转载请注明原文地址:https://www.9cbs.com/read-11723.html

New Post(0)