关于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); }
- 关于java中byte[]到Image的转换
- java 中 image 和 byte[] 相互转换
- java 中 image 和 byte[] 相互转换
- java中byte(byte []), int的转换
- Image和byte[]的转换
- C#中Image类与byte[]之间的转换
- Java byte数组到long的转换
- java:int到byte的转换
- byte[]与Image Image与 byte[] 之间的转换
- Java中有关byte和String的转换笔记和byte[]到short、int、long的相互转换
- 【Java】byte 到 unsigned byte (int) 转换
- Java中String与byte[]的转换
- java 中 char 和 byte 的转换
- java中char和byte的转换
- Java中String与byte[]的转换
- java中string与byte[]的转换
- Java中String与byte[]的转换
- java中string与byte[]的转换
- C/C++控制台输出时设置字体及背景颜色
- B. Little Girl and Game
- UVa 10534 - Wavio Sequence LIS
- swc 导入方法
- 正则表达式
- 关于java中byte[]到Image的转换
- PHP安全编程:主机文件目录浏览
- URLRewrite实现URL地址伪静态化
- 单点登录系列之四---使用RDBMS认证
- 有关指针的一些奇特声明
- 策略模式(Strategy)
- 尽量不要使用TerminateThread与SuspendThread
- PHP获取生成一个页面的数据库查询次数
- 腾讯2012笔试题