java7读取文件到BufferedImage对象

BufferedImage bufferedImage = ImageIO.read(Files.newInputStream(Paths.get(basePath + imageSource)));

java7写入文件到图片对象

ImageIO.write(bufferedImage, "jpg", Files.newOutputStream(Paths.get(fullPath)));

The call to Files.newInputStream will return a ChannelInputStream which (AFAIK) is not buffered. You'll want to wrap it

new BufferedInputStream(Files.newInputStream(...));

So that there are less IO calls to disk, depending on how you use it.

意译:据我所知,调用Files.newInputStream将会返回一些ChannelInputStream对象。如果你想对他进行封装,使用以下代码
new BufferedInputStream(File.newInputStream(Paths.get(fullPath)));
如此一来,磁盘IO的调用频次将会降低,具体看你怎么用了。

工具类:

import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths; @Slf4j
public final class GraphUtil { /**
* Encode Image to Base64 String
* @param image
* @param type
* @return
*/
public static String encodeToString(BufferedImage image, String type) { String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray(); BASE64Encoder encoder = new BASE64Encoder();
imageString = encoder.encode(imageBytes); bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
} /***
* Decode Base64 String to Image
* @param imageString
* @return
*/
public static BufferedImage decodeToImage(String imageString) { BufferedImage image = null;
byte[] imageByte;
try {
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return image;
} public static BufferedImage getBufferedImage(String basePath, String imageSource){ try {
return ImageIO.read(new BufferedInputStream(Files.newInputStream(Paths.get(basePath, imageSource))));
} catch (IOException e) {
log.error("读取图片出错:{}",e);
return null;
}
}
}

参考来源:

https://stackoverflow.com/questions/18522398/fastest-way-to-read-write-images-from-a-file-into-a-bufferedimage

java中快速读写图片到BufferedImage对象的更多相关文章

  1. Java中直接输出一个类的对象

    例如 package com.atguigu.java.fanshe; public class Person { String name; private int age; public Strin ...

  2. Java中如何读写cookie (二)

    Java中删除cookie Cookie[]   cookies=request.getCookies();       //cookies不为空,则清除       if(cookies!=null ...

  3. java中如何把图片转换成二进制流的代码

    在学习期间,把开发过程经常用到的一些代码段做个备份,下边代码内容是关于java中如何把图片转换成二进制流的代码,应该能对各朋友也有用处. public byte[] SetImageToByteArr ...

  4. (转) JAVA中如何设置图片(图标)自适应Jlable等组件的大小

    一.问题: 一个程序,组件上设置某个图片作为图标,因为的label(应该说是组件)已经设定了固定大小, 所以再打开一些大图片时,超过组件大小的部分没显示出来,而小图片又没填充完整个组件 二.解决这个问 ...

  5. JAVA中pdf转图片的方法

    JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用. 一.icepdf.有收费版和开源版,几种方法里最推荐的.转换的效果比较好,能识别我手头文件中的中文, ...

  6. EXCLE中快速插入图片

    在excle中怎么快速插入图片呢,一张一张点实在比较麻烦 解决办法: <table><img src="D:\A.png" width="60" ...

  7. Java中如何创建一个新的对象的/Creating Objects/

    The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't ...

  8. java中synchronized 用在实例方法和对象方法上面的区别

    https://bijian1013.iteye.com/blog/1836575 在Java中,synchronized 是用来表示同步的,我们可以synchronized 来修饰一个方法.也可以s ...

  9. Java中数据类型转换&基本类型变量和对象型变量

    1.Java的数据类型分为三大类 布尔型,字符型和数值型 其中数值型又分为整型和浮点型 2.Java的变量类型 布尔型 boolean 字符型 char 整型    byte,short,int,lo ...

随机推荐

  1. React 之 高阶组件的理解

    1.基本概念 高阶组件是参数为组件,返回值为新组件的函数. 2.举例说明 ① 装饰工厂模式 组件是 react 中的基本单元,组件中通常有一些逻辑(非渲染)需要复用处理.这里我们可以用高阶组件对组件内 ...

  2. wsgiref 与 Django框架初识

    前戏: Web框架的本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端,这样我们就可以自己实现Web框架 软件开发架构: c/s架构 客 ...

  3. 关于LinkedList for OpenJDK

    概述      LinkedList采用底层采用双向链表结构,与ArrayList的数组结构不一样.LinkedList因数据结构不一样,不需要申请连续内存,可以利用碎片内存.元素保存数据内容外还需要 ...

  4. HDU 2454 Degree Sequence of Graph G——可简单图化&&Heavel定理

    题意 给你一个度序列,问能否构成一个简单图. 分析 对于可图化,只要满足度数之和是偶数,即满足握手定理. 对于可简单图化,就是Heavel定理了. Heavel定理:把度序列排成不增序,即 $deg[ ...

  5. list获取所有上级

    /// <summary> /// 获取所有上级 /// </summary> /// <param name="list"></para ...

  6. 树状数组例题-数星星,简单题easy,校门外的树2,清点人数

    [例1]数星星 天空中有一些星星,这些星星都在不同的位置,每个星星都有个坐标,如果一个星星的左下方(包括正左和正下)有k颗星星,就说这颗星星是k级的. 比如,上图中,星星5是3级的(1,2,4在其左下 ...

  7. white-space

    white-space 语法: white-space:normal | pre | nowrap | pre-wrap | pre-line 默认值:normal 适用于:所有元素 继承性:有 动画 ...

  8. React应用数据传递的方式

    1. props属性 典型的React应用,数据通过props按照自上而下(父->子)的顺序传递数据. 2. Context传值 1. 应用场景 对于一些应用中全局性的属性(如UI主题.语言.登 ...

  9. MongoDB 分片管理(三)服务器管理

    MongoDB 分片管理(三)服务器管理

  10. 012_Python3 斐波纳契数列 + end 关键字

    1.个斐波纳契数列. #!/usr/bin/python3   # Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 a, b = 0, 1 while b < ...