java 图片如何让白色变透明

https://blog.csdn.net/yellowd1/article/details/50879296

importjava.awt.Graphics2D;  

importjava.awt.image.BufferedImage;  

importjava.io.File;  

importjava.io.IOException;  

importjava.util.regex.Pattern;  


importjavax.imageio.ImageIO;  

importjavax.swing.ImageIcon;  

importjavax.swing.JOptionPane;  


publicclassPicture {  


    publicstaticvoidconvert(String path) {  

        // TODO Auto-generated constructor stub  

        try{  

            BufferedImage image = ImageIO.read(newFile(path));  

            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);  

                    if(colorInRange(rgb))  

                        alpha = 0;  

                    else

                        alpha = 255;  

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

                    bufferedImage.setRGB(j2, j1, rgb);  

                }  

            }  

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

            // 生成图片为PNG  

            String outFile = path.substring(0, path.lastIndexOf("."));  

            ImageIO.write(bufferedImage, "png", newFile(outFile + ".png"));  

        } catch(IOException e) {  

            // TODO Auto-generated catch block  

            e.printStackTrace();  

        }  

    }  


    publicstaticbooleancolorInRange(intcolor) {  

        intred = (color & 0xff0000) >> 16;  

        intgreen = (color & 0x00ff00) >> 8;  

        intblue = (color & 0x0000ff);  

        if(red >= color_range && green >= color_range && blue >= color_range)  

            returntrue;  

        returnfalse;  

    }  


    publicstaticintcolor_range = 210;  

    publicstaticPattern pattern = Pattern.compile("[0-9]*");  


    publicstaticbooleanisNo(String str) {  

        returnpattern.matcher(str).matches();  

    }  


    /** 

     * @param args 

     */

    publicstaticvoidmain(String[] args) {  

        // TODO Auto-generated method stub  

        String path = JOptionPane.showInputDialog(null, "请输入图片目录");  

        if(path == null|| !newFile(path).isDirectory()) {  

            JOptionPane.showMessageDialog(null, "输入目录有误!");  

            return;  

        }  

        String color = JOptionPane.showInputDialog(null, "请输入色差范围0~255(建议10~50)");  

        if(isNo(color)) {  

            color_range = 255- Integer.parseInt(color);  

            File file = newFile(path);  

            String[] files = file.list();  

            for(inti = 0; i < files.length; i++) {  

                String ext = files[i].substring(files[i].lastIndexOf(".") + 1);  

                if(ext.equals("jpg")) {  

                    convert(path + "//"+ files[i]);  

                }  

            }  

            JOptionPane.showMessageDialog(null, "转换完成!");  

        } else{  

            JOptionPane.showMessageDialog(null, "输入的数字有误!");  

        }  

    }  


}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容