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 + "//" + 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 小秦 阅读(1392) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: