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, 0, 0, null);
}
writer.write(new IIOImage(rendImage, null, null));
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));