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

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

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

    java技術研究

    統計

    留言簿(3)

    閱讀排行榜

    評論排行榜

    將白色背景圖片變透明(轉自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"請輸入圖片目錄");  
    72.         if (path == null || !new File(path).isDirectory()) {  
    73.             JOptionPane.showMessageDialog(null"輸入目錄有誤!");  
    74.             return;  
    75.         }  
    76.         String color = JOptionPane.showInputDialog(null"請輸入色差范圍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"轉換完成!");  
    88.         } else {  
    89.             JOptionPane.showMessageDialog(null"輸入的數字有誤!");  
    90.         }  
    91.     }  
    92.   
    93. }  

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


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 99在线热播精品免费99热| 中文字幕在线免费看| 亚洲AV无码一区二区三区国产| a高清免费毛片久久| 亚洲av永久无码精品国产精品| av无码久久久久不卡免费网站| 自拍偷区亚洲国内自拍| 亚洲精品无码激情AV| 无码人妻久久一区二区三区免费| avtt天堂网手机版亚洲| 亚洲中文字幕伊人久久无码| 久久久久久夜精品精品免费啦| 亚洲欧美日韩中文无线码| 亚洲色自偷自拍另类小说| 日本成年免费网站| 国产无限免费观看黄网站| 亚洲福利秒拍一区二区| 亚洲男人的天堂一区二区| 免费福利在线播放| 精品人妻系列无码人妻免费视频| 久久亚洲AV无码精品色午夜麻豆| 国产乱弄免费视频| 亚州免费一级毛片| a级日本高清免费看| 国产青草亚洲香蕉精品久久| 久久久久亚洲av无码专区导航| 亚洲精品国产福利一二区| 最近新韩国日本免费观看| 一级特黄a免费大片| 亚洲大成色www永久网址| 国产亚洲A∨片在线观看| 麻豆国产VA免费精品高清在线| 久久国产精品免费观看| gogo免费在线观看| 亚洲av无码偷拍在线观看| 亚洲色偷偷av男人的天堂| 中文字幕一精品亚洲无线一区| 在线精品免费视频无码的| 在线看片免费人成视久网| 99精品视频在线观看免费| 污污的视频在线免费观看|