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 小秦 閱讀(1410) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 贡嘎县| 黑河市| 桐梓县| 扶风县| 沙洋县| 龙山县| 通化市| 怀来县| 玛纳斯县| 江口县| 桂阳县| 施秉县| 贞丰县| 桦南县| 绥阳县| 汶川县| 安顺市| 石景山区| 延长县| 滦平县| 乌鲁木齐市| 漳平市| 仪征市| 北安市| 彩票| 沿河| 福贡县| 孝感市| 休宁县| 正阳县| 遂平县| 沁源县| 遵义县| 磴口县| 达日县| 商城县| 含山县| 肇庆市| 区。| 静乐县| 东阿县|