JAVA Image Transparent Processing __java

Source: Internet
Author: User

public static void Main (string[] args) {try {bufferedimage image = ImageIO. Read (New FileInputStream ("f://1.jpg")); Take icon bufferedimage Imagebiao = Imageio.read (New FileInputStream ("F://3.png")); Graphics2D g = image.creategraphics (); G.setcolor (Color.yellow); G.setfont (New Font ("XXFarEastFont-Arial, Font.layout_left_to_right, 48)"; g.DrawString ("Image synthesis Example", image.getheight ()-400); Write icon ImageFilter IMGF = new Myfilter (255); FilteredImageSource fis = new FilteredImageSource (Imagebiao. GetSource (), IMGF); Image im = Toolkit.getdefaulttoolkit (). createimage (FIS); G.drawimage (IM, 0, 0, imagebiao.getwidth (null), Imagebiao. GetHeight (NULL), NULL); G.dispose (); FileOutputStream out = new FileOutputStream ("f://icon text synthesis 3.gif"); JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); Encoder.encode (image); Out.close (); Add the above artwork and the icon after the image} catch (Exception e) {e.printstacktrace ();}} Class Myfilter extends Rgbimagefilter {//Abstract classes Rgbimagefilter is a subclass of ImageFilter,//inherits it to implement image ARGB processing int AlphA = 0; public myfilter (int alpha) {//constructor, used to receive dimensions that require filtering of images, and transparency This.canfilterindexcolormodel = true; The Transparentimagefilter class inherits from Rgbimagefilter, and its constructors require the width and height of the original image to be passed in. This class implements the Filterrgb abstract function//, by default, the function passes the ARGB value of the pixel identified by the x,y, and the programmer returns the new ARGB value of the pixel This.alpha = alpha after a certain program logic is processed; public int Filterrgb (int x, int y, int rgb) {DirectColorModel DCM = (DirectColorModel) colormodel.getrgbdefault ();//D The Irectcolormodel class is used to separate the ARGB values out of the int red = dcm.getred (RGB); int green = Dcm.getgreen (RGB); int blue = Dcm.getblue (RGB); int Alp=dcm.getalpha (RGB); if (red==255&&blue==255&&green==255) {//If the pixel is white, make it transparent alpha = 0;} else{alpha=255}//if (alp==0) {//png and GIF format the transparent part of the picture is still transparent//alpha = 0;//}else{//alpha = 255;//} return Alpha << 24 | Red << 16 | Green << 8 | blue;//for standard ARGB output to achieve image filtering} if that's the case, it can be achieved. But white when the other color junction of the old Sawtooth, is not distortion ah later found some PNG and GIF pictures will be the middle of the white clasp off, thought that direct synthesis on it, but no, Plus a judgment on Alpha.//if (alp==0) {//png and GIF format the transparent part of the picture is still transparent//alpha = 0;//}else{//alpha = 255;//} But the effect is not good, butWhite transparent than before the effect is much better, so or the picture first processed before calling the program these are parts of the picture transparent, if you want all together transparent, we recommend the use of Alphacomposite class getinstance (int rule, float alpha) method to set the value of Alpha the original source: http://hi.baidu.com/berry_0712/blog/item/6a28190f7ce02c2d6059f3e2.html

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.