Vikings

java.awt.Image 的几样经验

1. 从文件/或者Url中获得Image.
java.awt.image包下的

Toolkit.getDefaultToolkit().createImage(Url)
Toolkit.getDefaultToolkit().createImage(File);

返回是java.awt.Image

javax.imageio包下的:

ImageIO.read(Url);

返回是BufferedImage

2. Image to bytes;

   Iterator iter = ImageIO.getImageWritersByMIMEType("image/jpeg");
            ImageWriter writer 
= iter.hasNext() ? (ImageWriter) iter.next() : null;
            ByteArrayOutputStream baos 
= new ByteArrayOutputStream();
            ImageOutputStream ios 
= ImageIO.createImageOutputStream(baos);
            writer.setOutput(ios);
            BufferedImage rendImage 
= null;
            
if (image instanceof BufferedImage) {
                rendImage 
= (BufferedImage) image;
            }
 else {
                Frame frame 
= new Frame();
                frame.addNotify();
                MediaTracker tracker 
= new MediaTracker(frame);                
                tracker.addImage(image, 
0);
                tracker.waitForAll();
                rendImage 
= new BufferedImage(image.getWidth(null),
                        image.getHeight(
null), 1);
                Graphics g 
= rendImage.createGraphics();
                g.drawImage(image, 
00null);
            }

            writer.write(
new IIOImage(rendImage, nullnull));
            writer.dispose();
            
return baos.toByteArray();

3. 剪裁图片
起作用是ImageFilter

    ImageFilter smallCropFilter =new CropImageFilter(0,0,20,20);
    java.awt.Image smallImage
= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),smallCropFilter));                
                          




 

posted on 2006-02-16 15:22 Vikings 阅读(2334) 评论(1)  编辑  收藏

Feedback

# re: java.awt.Image 的几样经验 2006-07-31 17:42 jd

谢谢!刚好做图片这一部分。  回复  更多评论   



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


网站导航: