继往开来 吐故纳新
日历
网志分类
· 所有网志 (990)
· 个人作品 (62)
· 软件设计 (33)
· 面向对象编程 (22)
· JavaAPI (39)
· Java开源工具 (31)
· Swing (34)
· Java语法细节 (39)
· 样式表CSS (12)
· XML (10)
· J2EE(JavaEE) (23)
· 算法数据结构 (64)
· 正则表达式 (4)
· 软件知识 (6)
· Java线程 (9)
· Web开发.Jsp/Servlet/Struts (20)
· 程序随想录 (7)
· Spring (5)
· Hibernate (7)
· J2SE 高级 (2)
· J2SE 高级 (0)
· Web开发.Ajax (16)
· Web开发.JavaScript (43)
· DB4O (2)
· Web开发.CSS/Html (22)
· C# (20)
· ERP (4)
· JDBC (1)
· 编程资源 (16)
· 编程感悟 (29)
· DB/Sql (13)
· VB (29)
· VC (2)
· 桌面脚本 (3)
· 新兴软件 (3)
· 英语学习 (21)
· 网文转载 (159)
· 职场风云 (39)
· 诗词歌赋 (32)
· 生活感言 (77)
· 奇文共赏 (13)
· 财经纵横 (6)
· 未分类 (11)
站内搜索
友情链接
· 歪酷博客
· 我的歪酷 非非共享界
· 偶要雷锋
· 豆瓣
· nczonline
· 当当网
· easyjf中文站
· Donews
· 天极Java文章列表
· W3CSchool
· taiten的BLOG
· Dojo中国
· Dojo
· Extjs.com
· Lifehack中文网志
· JaveEye的一个AS专题
· Banq's JDon
· Java 中文网址大全
· 梦想Java
· 360Doc个人图书馆
· java开源大全
· 我在硅谷动力的软件下载站
· 站长中国
· 随意贴
· CSS教学素材站
· java 参考中文站
· 面向构件与SOA社区
· 彩字生成
· 派派小说论坛
· 如坐春风
· 英语学习网
· BBC CHina
· www.dlbang.com
· 古文竖排格式在线转化工具
· 免费家谱
· 图片上传基地
· 风景壁纸
· 和风细雨
· MyC#BlogInCsdn

订阅 RSS

0207373

歪酷博客

开此博一为经验积累,二为资料收集,三为同道交流,四为资源共享.
« 上一篇: 程序设计常用英语列表 下一篇: [Java]<整理>使用iText在PDF文件中输出中文 »
Junglesong @ 2006-08-18 12:15

它能处理网页上大多数图片,bmp除外,本着共享的原则,大家如果觉得有用就拿去吧.
package utils;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import utils.bean.Size;
public class PictureUtil {
 // This method returns a buffered image with the contents of an image
 public static BufferedImage toBufferedImage(Image image) {
   if (image instanceof BufferedImage) {
     return (BufferedImage) image;
   }
   // This code ensures that all the pixels in the image are loaded
   image = new ImageIcon(image).getImage();
   // Determine if the image has transparent pixels; for this method's
   // implementation, see e661 Determining If an Image Has Transparent
   // Pixels
   boolean hasAlpha = hasAlpha(image);
   // Create a buffered image with a format that's compatible with the
   // screen
   BufferedImage bimage = null;
   GraphicsEnvironment ge = GraphicsEnvironment
       .getLocalGraphicsEnvironment();
   try {
     // Determine the type of transparency of the new buffered image
     int transparency = Transparency.OPAQUE;
     if (hasAlpha) {
       transparency = Transparency.BITMASK;
     }
     // Create the buffered image
     GraphicsDevice gs = ge.getDefaultScreenDevice();
     GraphicsConfiguration gc = gs.getDefaultConfiguration();
     bimage = gc.createCompatibleImage(image.getWidth(null), image
         .getHeight(null), transparency);
   } catch (HeadlessException e) {
     // The system does not have a screen
   }
   if (bimage == null) {
     // Create a buffered image using the default color model
     int type = BufferedImage.TYPE_INT_RGB;
     if (hasAlpha) {
       type = BufferedImage.TYPE_INT_ARGB;
     }
     bimage = new BufferedImage(image.getWidth(null), image
         .getHeight(null), type);
   }
   // Copy image to buffered image
   Graphics g = bimage.createGraphics();
   // Paint the image onto the buffered image
   g.drawImage(image, 0, 0, null);
   g.dispose();
   return bimage;
 }
 public static boolean hasAlpha(Image image) {
   // If buffered image, the color model is readily available
   if (image instanceof BufferedImage) {
     BufferedImage bimage = (BufferedImage) image;
     return bimage.getColorModel().hasAlpha();
   }
   // Use a pixel grabber to retrieve the image's color model;
   // grabbing a single pixel is usually sufficient
   PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
   try {
     pg.grabPixels();
   } catch (InterruptedException e) {
   }
   // Get the image's color model
   ColorModel cm = pg.getColorModel();
   return cm.hasAlpha();
 }
// 取得文件更新时间
 public static String getLastModifyTime(long modifiedTime) {
   Calendar cal = new GregorianCalendar();
   cal.setTimeInMillis(modifiedTime);
   SimpleDateFormat fromatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String lastModifyTime = fromatter.format(cal.getTime());
   return lastModifyTime;
 }
// 取得文件扩展名
 public static String getFileExtension(String fileName) {
   return fileName.substring(fileName.lastIndexOf(".") + 1, fileName
       .length());
 }
// 取得图片大小,Size类很简单,自己写吧.
 public static Size getPictureSize(String fileName) throws Exception{
   File file=new File(fileName);
   BufferedImage bufferIamge=PictureUtil.toBufferedImage(ImageIO.read(file));
   return new Size(bufferIamge.getWidth(),bufferIamge.getHeight());
 }
}


评论 / 个人网页 / 扔小纸条
* 昵称

已经注册过? 请登录

新用户请先注册 以便能显示头像及追踪评论回复

Email
网址
* 评论
表情
 


 

分类小组论坛
杂谈 , 娱乐、八卦 , 文学、艺术 , 体育 , 旅游、同城 , 象牙塔 , 情感 , 时尚、生活 , 星座 , 科技

请注意遵守中华人民共和国法律法规, 如威胁到本站生存, 将依法向有关部门报告, 同时本站的相关记录可能成为对您不利的证据.

相关法律法规
全国人大常委会关于维护互联网安全的决定
中华人民共和国计算机信息系统安全保护条例
中华人民共和国计算机信息网络国际联网管理暂行规定
计算机信息网络国际联网安全保护管理办法
计算机信息系统国际联网保密管理规定