<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)航:
     
    主站蜘蛛池模板: 亚洲成?v人片天堂网无码| 无码免费又爽又高潮喷水的视频 | 亚洲欧洲无卡二区视頻| **aaaaa毛片免费| 亚洲国产精品成人综合久久久| 91人人区免费区人人| 亚洲尹人香蕉网在线视颅| 99在线免费观看视频| 亚洲色成人网一二三区| 国色精品卡一卡2卡3卡4卡免费| 亚洲一级大黄大色毛片| 毛片免费观看网站| MM1313亚洲国产精品| 亚洲国产精品成人一区| 国产在线观a免费观看| 亚洲AV电影院在线观看| 国产一卡二卡四卡免费| 亚洲精品无码成人| 亚洲精品99久久久久中文字幕 | 乱爱性全过程免费视频| 亚洲人成人无码网www电影首页| 免费一区二区三区| 亚洲av无码片在线观看| 好吊妞998视频免费观看在线| 久久精品国产亚洲av瑜伽| 日本大片在线看黄a∨免费| 日韩色视频一区二区三区亚洲| 亚洲中文字幕无码爆乳av中文| 成人免费一区二区三区| 亚洲综合色丁香麻豆| 2021精品国产品免费观看| 亚洲AV色吊丝无码| 毛片免费在线播放| 一级中文字幕乱码免费| 亚洲综合免费视频| 免费乱理伦在线播放| 日本免费在线观看| 色窝窝亚洲AV网在线观看| 亚洲91av视频| 永久黄网站色视频免费观看| 爱丫爱丫影院在线观看免费|