Java converts white background images to colorless

Source: Internet
Author: User

 PackageCom.cxf.dao;ImportJava.awt.Graphics2D;ImportJava.awt.Image;ImportJava.awt.image.BufferedImage;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJavax.imageio.ImageIO;ImportJavax.swing.ImageIcon; Public classAlpha { Public Static voidMain (string[] args) {transferalpha (); } Public Static byte[] Transferalpha () {Bytearrayoutputstream Bytearrayoutputstream=NewBytearrayoutputstream (); File File=NewFile ("E:/07.png");   InputStream is; Try{ is=Newfileinputstream (file); BufferedImage Bi=Imageio.read (IS); Image Image=(Image) bi; ImageIcon ImageIcon=NewImageIcon (image); BufferedImage BufferedImage=Newbufferedimage (ImageIcon. Geticonwidth (), Imageicon.geticonheight (), BUFFEREDIMAGE.TYPE_4BYTE_ABGR); Graphics2D g2d=(graphics2d) bufferedimage.getgraphics (); G2d.drawimage (Imageicon.getimage (),0, 0, ImageIcon. Getimageobserver ()); intAlpha = 0;  for(intJ1 = Bufferedimage.getminy (); J1 <bufferedimage. GetHeight (); J1++) {      for(intJ2 = Bufferedimage.getminx (); J2 <bufferedimage. getwidth (); J2++) {      intRGB =Bufferedimage.getrgb (J2, J1); intR = (RGB & 0xff0000) >> 16 ; intg= (RGB & 0xff00) >> 8 ; intb= (RGB & 0xFF ); if((255-r) <30) && ((255-g) <30) && ((255-b) <30) ) {RGB= ((alpha + 1) << 24) | (RGB & 0X00FFFFFF);     } bufferedimage.setrgb (J2, J1, RGB); }} g2d.drawimage (BufferedImage,0, 0, Imageicon.getimageobserver ()); Imageio.write (BufferedImage,"PNG",NewFile ("E:/67.png") );//Direct Output file//Imageio.write (bufferedimage, "PNG", bytearrayoutputstream);//Convert to byte array}Catch(Exception e) {e.printstacktrace (); }finally{      }   returnBytearrayoutputstream.tobytearray ();}}

Java converts white background images to colorless

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.