日期:2014-05-16 浏览次数:20562 次
package util; import java.awt.Graphics; import java.awt.Image; import java.awt.Label; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; /** * 这个工具类用于向数据库存入图片,以及从数据库得到图片。 */ public class DatabaseRead_GetImage { /** * 转换Image数据为byte数组 * * @param image * Image对象 * @param format * image格式字符串.如 "jpeg ", "png " * @return byte数组 */ public static byte[] imageToBytes(Image image, String format) { BufferedImage bImage = new BufferedImage(image.getWidth(null), image .getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics bg = bImage.getGraphics(); bg.drawImage(image, 0, 0, null); bg.dispose(); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(bImage, format, out); } catch (IOException e) { e.printStackTrace(); } return out.toByteArray(); } /** * 转换byte数组为Image * * @param bytes * Image的bytes数据数组 * @return Image */ public static Image bytesToImage(byte[] bytes) { Image image = Toolkit.getDefaultToolkit().createImage(bytes); try { MediaTracker mt = new MediaTracker(new Label()); mt.addImage(image, 0); mt.waitForAll(); } catch (InterruptedException e) { e.printStackTrace(); } return image; } }