关于java中byte[]到Image的转换

来源:互联网 发布:科多大数据 编辑:程序博客网 时间:2024/05/14 13:34

 

我把图像存在了Oracle数据库中,Oracle数据库可以以Blob的数据类型进行存取,但是由于有一个演示版本要拿出去演示,不可能时时连着网络,就存到了Access数据库中,结果Access数据库对Blob这个比较无语,它这个数据库,貌似不存在流的概念了,为了使我写的Model层对象更具有兼容性,我把其Blob的属性换成了Image,这样子类就与数据库无关性更少了,所以我就面临着Access中图片到java中如何得到。

首先用getBinaryStream是肯定可以的,但是我比较懒惰,这还要用一个InputStream接受,还要转换,于是我决定用getbytes直接得到一个byte[]数组,byte数组到Image其实蛮简单的,使用Toolkit.getDefaultToolkit().createImage(byte[] bytes)方法就可以得到一个Image了,但是我想知道到底怎么完成的。

于是:Toolkit是一个接口,那么他的getDefaultToolkit()方法肯定就是一个实现了这个接口的对应方法了,找到这个代码

/**     * Gets the default toolkit.     * <p>     * If a system property named <code>"java.awt.headless"</code> is set     * to <code>true</code> then the headless implementation     * of <code>Toolkit</code> is used.     * <p>     * If there is no <code>"java.awt.headless"</code> or it is set to     * <code>false</code> and there is a system property named     * <code>"awt.toolkit"</code>,     * that property is treated as the name of a class that is a subclass     * of <code>Toolkit</code>;     * otherwise the default platform-specific implementation of     * <code>Toolkit</code> is used.     * <p>     * Also loads additional classes into the VM, using the property     * 'assistive_technologies' specified in the Sun reference     * implementation by a line in the 'accessibility.properties'     * file.  The form is "assistive_technologies=..." where     * the "..." is a comma-separated list of assistive technology     * classes to load.  Each class is loaded in the order given     * and a single instance of each is created using     * Class.forName(class).newInstance().  This is done just after     * the AWT toolkit is created.  All errors are handled via an     * AWTError exception.     * @return    the default toolkit.     * @exception  AWTError  if a toolkit could not be found, or     *                 if one could not be accessed or instantiated.     */    public static synchronized Toolkit getDefaultToolkit() {        if (toolkit == null) {            try {                // We disable the JIT during toolkit initialization.  This                // tends to touch lots of classes that aren't needed again                // later and therefore JITing is counter-productiive.                java.lang.Compiler.disable();                java.security.AccessController.doPrivileged(                        new java.security.PrivilegedAction() {                    public Object run() {                        String nm = null;                        Class cls = null;                        try {                            nm = System.getProperty("awt.toolkit");                            try {                                cls = Class.forName(nm);                            } catch (ClassNotFoundException e) {                                ClassLoader cl = ClassLoader.getSystemClassLoader();                                if (cl != null) {                                    try {                                        cls = cl.loadClass(nm);                                    } catch (ClassNotFoundException ee) {                                        throw new AWTError("Toolkit not found: " + nm);                                    }                                }                            }                            if (cls != null) {                                toolkit = (Toolkit)cls.newInstance();                                if (GraphicsEnvironment.isHeadless()) {                                    toolkit = new HeadlessToolkit(toolkit);                                }                            }                        } catch (InstantiationException e) {                            throw new AWTError("Could not instantiate Toolkit: " + nm);                        } catch (IllegalAccessException e) {                            throw new AWTError("Could not access Toolkit: " + nm);                        }                        return null;                    }                });                loadAssistiveTechnologies();            } finally {                // Make sure to always re-enable the JIT.                java.lang.Compiler.enable();            }        }        return toolkit;    }
原来是rt包中sun.awt下的东西,从
System.getProperty("awt.toolkit");   

可以轻松得到他的类名:sun.awt.windows.WToolkit

但是它并没有实现creatImage方法,于是看他父类sun.awt.SunToolkit,果然找到了其方法。

  public Image createImage(byte[] paramArrayOfByte, int paramInt1, int paramInt2)  {    return createImage(new ByteArrayImageSource(paramArrayOfByte, paramInt1, paramInt2));  }  public Image createImage(ImageProducer paramImageProducer)  {    return new ToolkitImage(paramImageProducer);  }
咦,其中是有实现了,但是和我调用的createImage(byte[])这个不相符啊,再看看Toolkit.getDefaultToolkit().createImage(byte[] bytes)中的声明,啊哈,好吧,原来是这样子来完成的,sun公司的人真的都好厉害啊,不看不知道啊,记录下,以备以后学习这种方式。

 /**     * Creates an image which decodes the image stored in the specified     * byte array.     * <p>     * The data must be in some image format, such as GIF or JPEG,     * that is supported by this toolkit.     * @param     imagedata   an array of bytes, representing     *                         image data in a supported image format.     * @return    an image.     * @since     JDK1.1     */    public Image createImage(byte[] imagedata) {        return createImage(imagedata, 0, imagedata.length);    }

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 不到一米八的身高想扣篮怎么办 鞋胶粘在鞋面上怎么办 鞋子上沾了胶怎么办 休闲鞋号码大了半码怎么办 高跟鞋大了一码怎么办 浅口单鞋买大了怎么办 新鞋子磨大脚趾怎么办 鞋前面磨大脚趾怎么办 白鞋子蹭黑了怎么办 夏天穿皮鞋捂脚怎么办 耐克赤足掉漆怎么办 鞋子买回来小了怎么办 布鞋大了一码怎么办 鞋子买小了一码怎么办 运动鞋小了一码怎么办 帆布鞋小了一码怎么办 脚踝骨韧带断了怎么办 咖啡喝多了失眠怎么办 奥迪q7电瓶没电怎么办 一岁宝宝坐不稳怎么办 2岁宝宝不肯把尿怎么办 踢足球上肢和下肢不协调怎么办 ppt文字放映时重叠怎么办 月子8天腰背疼怎么办 生完五天腰背疼怎么办 生完孩子腰不好怎么办 养了个白眼狼怎么办 孩子学东西很慢怎么办 手不小心碰肿了怎么办 腿中间摩擦的疼怎么办 晚上睡觉时双腿酸胀怎么办 腿擦破皮了怎么办 快速好 宝宝腿擦破皮了怎么办 新生儿睡觉腿喜欢弯曲怎么办 孩子八个月交叉走路怎么办 胫椎引起双腿发热怎么办 婴儿头型睡扁了怎么办 膝盖抻筋了疼怎么办 小腿肚子抻筋了怎么办 后背抻筋了 很疼怎么办 运动膝盖抻着了怎么办