<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    java技術(shù)研究

    統(tǒng)計(jì)

    留言簿(3)

    閱讀排行榜

    評(píng)論排行榜

    將白色背景圖片變透明(轉(zhuǎn)自csdn)

    [java:showcolumns] view plaincopy
    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    1. package com.picture;  
    2.   
    3. import java.awt.Graphics2D;  
    4. import java.awt.image.BufferedImage;  
    5. import java.io.File;  
    6. import java.io.IOException;  
    7. import java.util.regex.Pattern;  
    8.   
    9. import javax.imageio.ImageIO;  
    10. import javax.swing.ImageIcon;  
    11. import javax.swing.JOptionPane;  
    12.   
    13. public class Picture {  
    14.   
    15.     public static void convert(String path) {  
    16.         // TODO Auto-generated constructor stub  
    17.         try {  
    18.             BufferedImage image = ImageIO.read(new File(path));  
    19.             ImageIcon imageIcon = new ImageIcon(image);  
    20.             BufferedImage bufferedImage = new BufferedImage(  
    21.                     imageIcon.getIconWidth(), imageIcon.getIconHeight(),  
    22.                     BufferedImage.TYPE_4BYTE_ABGR);  
    23.             Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();  
    24.             g2D.drawImage(imageIcon.getImage(), 00,  
    25.                     imageIcon.getImageObserver());  
    26.             int alpha = 0;  
    27.             for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage  
    28.                     .getHeight(); j1++) {  
    29.                 for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage  
    30.                         .getWidth(); j2++) {  
    31.                     int rgb = bufferedImage.getRGB(j2, j1);  
    32.                     if (colorInRange(rgb))  
    33.                         alpha = 0;  
    34.                     else  
    35.                         alpha = 255;  
    36.                     rgb = (alpha << 24) | (rgb & 0x00ffffff);  
    37.                     bufferedImage.setRGB(j2, j1, rgb);  
    38.                 }  
    39.             }  
    40.             g2D.drawImage(bufferedImage, 00, imageIcon.getImageObserver());  
    41.             // 生成圖片為PNG  
    42.             String outFile = path.substring(0, path.lastIndexOf("."));  
    43.             ImageIO.write(bufferedImage, "png"new File(outFile + ".png"));  
    44.         } catch (IOException e) {  
    45.             // TODO Auto-generated catch block  
    46.             e.printStackTrace();  
    47.         }  
    48.     }  
    49.   
    50.     public static boolean colorInRange(int color) {  
    51.         int red = (color & 0xff0000) >> 16;  
    52.         int green = (color & 0x00ff00) >> 8;  
    53.         int blue = (color & 0x0000ff);  
    54.         if (red >= color_range && green >= color_range && blue >= color_range)  
    55.             return true;  
    56.         return false;  
    57.     }  
    58.   
    59.     public static int color_range = 210;  
    60.     public static Pattern pattern = Pattern.compile("[0-9]*");  
    61.   
    62.     public static boolean isNo(String str) {  
    63.         return pattern.matcher(str).matches();  
    64.     }  
    65.   
    66.     /** 
    67.      * @param args 
    68.      */  
    69.     public static void main(String[] args) {  
    70.         // TODO Auto-generated method stub  
    71.         String path = JOptionPane.showInputDialog(null"請(qǐng)輸入圖片目錄");  
    72.         if (path == null || !new File(path).isDirectory()) {  
    73.             JOptionPane.showMessageDialog(null"輸入目錄有誤!");  
    74.             return;  
    75.         }  
    76.         String color = JOptionPane.showInputDialog(null"請(qǐng)輸入色差范圍0~255(建議10~50)");  
    77.         if (isNo(color)) {  
    78.             color_range = 255 - Integer.parseInt(color);  
    79.             File file = new File(path);  
    80.             String[] files = file.list();  
    81.             for (int i = 0; i < files.length; i++) {  
    82.                 String ext = files[i].substring(files[i].lastIndexOf(".") + 1);  
    83.                 if (ext.equals("jpg")) {  
    84.                     convert(path + "http://" + files[i]);  
    85.                 }  
    86.             }  
    87.             JOptionPane.showMessageDialog(null"轉(zhuǎn)換完成!");  
    88.         } else {  
    89.             JOptionPane.showMessageDialog(null"輸入的數(shù)字有誤!");  
    90.         }  
    91.     }  
    92.   
    93. }  

    posted on 2012-04-28 09:50 小秦 閱讀(1402) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲中文字幕无码一区| 亚洲AV色欲色欲WWW| 人人揉揉香蕉大免费不卡| 亚洲人成人网站在线观看| 人人狠狠综合久久亚洲| 91在线视频免费91| 亚洲欧洲日本国产| 久久久久久毛片免费播放| 亚洲AV无码久久精品蜜桃| 最近免费中文字幕中文高清| 亚洲日本中文字幕一区二区三区| 美美女高清毛片视频黄的一免费 | 四虎免费大片aⅴ入口| 亚洲国产成人无码av在线播放| 97青青草原国产免费观看| 亚洲av无码专区在线播放| 成人自慰女黄网站免费大全| 自拍偷自拍亚洲精品情侣| 亚洲免费一区二区| 久久精品国产亚洲AV不卡| 九九综合VA免费看| 久久亚洲国产成人影院网站| 人妻巨大乳hd免费看| 精品国产香蕉伊思人在线在线亚洲一区二区 | 吃奶摸下高潮60分钟免费视频| 亚洲国产无线乱码在线观看| 午夜私人影院免费体验区| 亚洲中文字幕无码久久2020| 大香人蕉免费视频75| 亚洲欧美日韩一区二区三区 | 亚洲日本在线免费观看| 天天影视色香欲综合免费| 亚洲人成人77777在线播放| 99爱在线精品免费观看| 亚洲一日韩欧美中文字幕在线| a级毛片无码免费真人| 亚洲国产精品成人综合色在线| 免费大香伊蕉在人线国产 | 日韩免费在线观看视频| 亚洲视频一区调教| 国产成人精品免费视频动漫|