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. P1417 烹调方案[背包]

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  2. PL/SQL存储过程

    一.概述 过程和函数统称为PL/SQL子程序,他们是被命名的PL/SQL块,均存储于数据库中. 并通过输入.输出和输入输出参数与其调用者交换信息.唯一区别是函数总向调用者返回数据. 二.存储过程详解 ...

  3. JS 仿支付宝input文本输入框放大组件

    input输入的时候可以在后边显示数字放大镜 <!doctype html> <html lang="en"> <head> <meta ...

  4. jQuery理解与运用

    1. 什么是jQuery 它是一个轻量级的javascript类库,别人写好的一个类. 2. jQuery优点 2.1 总是面向集合  2.2 多行操作集于一行      注1:就一个类“jQuery ...

  5. SublimeText 括号插件 Bracket Highlighter高亮设置

    1. ctrl + shift + p,打开命令面板,输入install,在菜单中选择Package Control:Install Package如图 2. 步骤1后弹出的命令输入框中 输入:Bra ...

  6. 微信&QQ中打开网页提示“已停止访问该网页”是怎么回事?

    背景 大家是不是经常会遇到这种情况,分享出去的网页链接在微信里或者QQ里打开会提示“已停止访问该网页”,当大家看到这种的提示的时候就说明你访问的网页已经被腾讯拦截了. 当大家遇到以上这种情况的时候要怎 ...

  7. 45 | 自增id用完怎么办?

    MySQL 里有很多自增的 id,每个自增 id 都是定义了初始值,然后不停地往上加步长.虽然自然数是没有上限的,但是在计算机里,只要定义了表示这个数的字节长度,那它就有上限.比如,无符号整型 (un ...

  8. springboot启动时执行任务CommandLineRunner

    # SpringBoot中CommandLineRunner的作用> 平常开发中有可能需要实现在项目启动后执行的功能,SpringBoot提供的一种简单的实现方案就是添加一个model并实现Co ...

  9. Codeforces Round #586

    目录 Contest Info A. Cards B. Multiplication Table C. Substring Game in the Lesson D. Alex and Julian ...

  10. TensorFlow(十三):模型的保存与载入

    一:保存 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist ...