java中快速读写图片到BufferedImage对象
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;
}
}
}
参考来源:
java中快速读写图片到BufferedImage对象的更多相关文章
- Java中直接输出一个类的对象
例如 package com.atguigu.java.fanshe; public class Person { String name; private int age; public Strin ...
- Java中如何读写cookie (二)
Java中删除cookie Cookie[] cookies=request.getCookies(); //cookies不为空,则清除 if(cookies!=null ...
- java中如何把图片转换成二进制流的代码
在学习期间,把开发过程经常用到的一些代码段做个备份,下边代码内容是关于java中如何把图片转换成二进制流的代码,应该能对各朋友也有用处. public byte[] SetImageToByteArr ...
- (转) JAVA中如何设置图片(图标)自适应Jlable等组件的大小
一.问题: 一个程序,组件上设置某个图片作为图标,因为的label(应该说是组件)已经设定了固定大小, 所以再打开一些大图片时,超过组件大小的部分没显示出来,而小图片又没填充完整个组件 二.解决这个问 ...
- JAVA中pdf转图片的方法
JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用. 一.icepdf.有收费版和开源版,几种方法里最推荐的.转换的效果比较好,能识别我手头文件中的中文, ...
- EXCLE中快速插入图片
在excle中怎么快速插入图片呢,一张一张点实在比较麻烦 解决办法: <table><img src="D:\A.png" width="60" ...
- Java中如何创建一个新的对象的/Creating Objects/
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't ...
- java中synchronized 用在实例方法和对象方法上面的区别
https://bijian1013.iteye.com/blog/1836575 在Java中,synchronized 是用来表示同步的,我们可以synchronized 来修饰一个方法.也可以s ...
- Java中数据类型转换&基本类型变量和对象型变量
1.Java的数据类型分为三大类 布尔型,字符型和数值型 其中数值型又分为整型和浮点型 2.Java的变量类型 布尔型 boolean 字符型 char 整型 byte,short,int,lo ...
随机推荐
- P2746 P2812 [USACO5.3]校园网Network of Schools[SCC缩点]
题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作"接受学校").注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学 ...
- Session&Cookie&localStorage浅谈
Session&Cookie&localStorage 领导让我开发一个有两张信息表单需要提交页面的网站,我作为一名开发人员,这个需求太简单了,和领导说直接存session sessi ...
- python中字符串离散化的例子
''' 问题:1.假设DataFrame中有一列名为type,其字段中内容为a,b,c 等用,隔开的值,如: type a,b,c a,f,x b,c,e ...统计type中每个类型出现的次数 并绘 ...
- ES6中构造函数内super关键字的使用
super关键字用于访问和调用一个对象的父对象上的函数. super.prop和super[expr]表达式在类和对象字面量任何方法定义中都是有效的. 语法 super([arguments]); / ...
- win10 注册DLL
昨天用c++写了一个ocx插件,注册就死活注册不上,折腾了半天1.打开C:\Windows\SysWOW64 文件夹 找到cmd 右键管理员运行 2.将你的插件或者dll放到此目录下3.regsvr ...
- go常量的定义和枚举类型
const a,b int = 1,2 const a,b = 1,2 const ( a = "hello" b,c =3,4 ) 常量数值可作为各种类型使用 枚举类型的 ...
- html与HTML5的区别
文档的类型声明不同 html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.or ...
- php MySQL 选择数据库
在你连接到 MySQL 数据库后,可能有多个可以操作的数据库,所以你需要选择你要操作的数据库. 从命令提示窗口中选择MySQL数据库 在 mysql> 提示窗口中可以很简单的选择特定的数据库.你 ...
- Mysql查看所有表的数据量
##查看所有表信息 SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'pcms-zgh20190327' ##查看各个表数据量 ...
- Crontab 定时任务格式参数
crontab 定时执行 任务 格式: * * * * *-) 小时(-) 日(-) 月份(-) 星期(-6) 星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条 ...