注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

独立观察员·网易

分享万岁

 
 
 

日志

 
 

Java 去除图片黑、白背景类  

2013-08-06 22:02:00|  分类: 原新浪博客的 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

原始代码来源于网路,做了些整理。

package com.dlgcy.ui;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GraphicsConfiguration;

import java.awt.GraphicsDevice;

import java.awt.GraphicsEnvironment;

import java.awt.HeadlessException;

import java.awt.Image;

import java.awt.Transparency;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

 

public class Alpha {

 

    public static byte[] transferAlpha(Image image) {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try {

            ImageIcon imageIcon = new ImageIcon(image);

            BufferedImage bufferedImage = new BufferedImage(imageIcon

                    .getIconWidth(), imageIcon.getIconHeight(),

                    BufferedImage.TYPE_4BYTE_ABGR);

            Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();

            g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon

                    .getImageObserver());

            int alpha = 0;

            for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage

                    .getHeight(); j1++) {

                for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage

                        .getWidth(); j2++) {

                    int rgb = bufferedImage.getRGB(j2, j1);

 

                    int R =(rgb & 0xff0000 ) >> 16 ;

                int G= (rgb & 0xff00 ) >> 8 ;

                int B= (rgb & 0xff );

                //if(((255-R)<30) && ((255-G)<30) && ((255-B)<30)){ //去除白色背景;

                if(((255-R)>160) && ((255-G)>160) && ((255-B)>160)){//去除黑色背景——dlgcy;

                    rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);

                }

                bufferedImage.setRGB(j2, j1, rgb);

                }

            }

            g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());

            //ImageIO.write(bufferedImage, "png", new File("d:/test.png.png"));

            ImageIO.write(bufferedImage, "png", byteArrayOutputStream);

        } catch (Exception e) {

        }finally{

        }

 

        return byteArrayOutputStream.toByteArray();

    }

 

    

 

    //byte[] ------>BufferedImage

    public static BufferedImage ByteToBufferedImage(byte[] byteImage) throws IOException{

        ByteArrayInputStream in = new ByteArrayInputStream(byteImage);

        BufferedImage buffImage = ImageIO.read(in);    

        return buffImage;

    }

 

    //Image转换为BufferedImage;

    public static BufferedImage toBufferedImage(Image image) {

        if (image instanceof BufferedImage) {

            return (BufferedImage) image;

        }

        image = new ImageIcon(image).getImage();

        boolean hasAlpha = false;

        BufferedImage bimage = null;

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

        try {

            int transparency = Transparency.OPAQUE;

            if (hasAlpha) {

                transparency = Transparency.BITMASK;

            }

            GraphicsDevice gs = ge.getDefaultScreenDevice();

            GraphicsConfiguration gc = gs.getDefaultConfiguration();

            bimage = gc.createCompatibleImage(image.getWidth(null),

                    image.getHeight(null), transparency);

        } catch (HeadlessException e) {

        }

        if (bimage == null) {

            int type = BufferedImage.TYPE_INT_RGB;

            if (hasAlpha) {

                type = BufferedImage.TYPE_INT_ARGB;

            }

            bimage = new BufferedImage(image.getWidth(null),

                    image.getHeight(null), type);

        }

        Graphics g = bimage.createGraphics();

        g.drawImage(image, 0, 0, null);

        g.dispose();

        return bimage;

    }

 

}


  评论这张
 
阅读(47)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018