Java图像文件的读写
读取bmp文件到BufferedImage中
File file2 = new File("c:\\testimages\\tttt" + ".bmp");
// BufferedImage bi
= backstore.getBufferedImage();
try {
output = ImageIO.read(file2);
} catch
(IOException e) {
e.printStackTrace();
}
输出bmp文件
File file2 = new File("c:\\testimages\\tttt" +
".bmp");
ImageIO.write(bi, "bmp", file2);
Byte[]输出到文件
byte[] buf =UtilZip.zipObjectToByte(cache);
File file2 = new File("c:\\testimages\\cache.bin");
FileOutputStream fos =new FileOutputStream(file2);
fos.write(buf);
fos.flush();
fos.close();
读文件到Byte[]中
File file2 = new File("c:\\testimages\\cache.bin");
FileInputStream fis = new FileInputStream(file2);
byte[]
buf =
new byte[(int) file2.length()];
fis.read(buf);
fis.close();
填充颜色到整个画布
BufferedImage bi = backstore.getBufferedImage();
Graphics g2 = bi.getGraphics();
g2.setColor(Color.red);
g2.fillRect(0, 0, Common.width,
Common.height);
图像变灰操作
public finalBufferedImage getGrayPicture(BufferedImage originalPic) {
int imageWidth =
originalPic.getWidth();
int imageHeight =
originalPic.getHeight();
BufferedImage newPic
= new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_3BYTE_BGR);
ColorConvertOp cco = new
ColorConvertOp(ColorSpace
.getInstance(ColorSpace.CS_GRAY),
null);
cco.filter(originalPic,
newPic);
return newPic;
}
ImageIO
javax.imageio.ImageIO lets you save and
restore Images to disk in a platform independent format. It works using plug-in
modules that handle various formats including "gif", "png" and "jpeg" (all lower
case, or all upper case, but not mixed). "jpeg" or "jpg" is acceptable. Use
ImageIO. getWriterFormatNames() to find out which types are supported on your
platform:
import javax.imageio.ImageIO;
public class
Jai
{
public static void main ( String[]
args )
{
String[] names = ImageIO.getWriterFormatNames();
for ( String
name: names )
{
System.out.println( name );
}
}
}
Saving an Image to raw bytes
// BufferedImage to raw bytes
import
javax.imageio.ImageIO;
import
java.awt.image.BufferedImage;
import
java.io.ByteArrayOutputStream;
...
//
O P E N
ByteArrayOutputStream baos
= new ByteArrayOutputStream(
1000 );
// W R
I T
E
ImageIO.write(
aBufferedImage, "jpeg"
,
baos );
// C L O S
E
baos.flush();
byte[]
resultImageAsRawBytes =
baos.toByteArray();
baos.close();
Loading
a BufferedImage from a file
// file to
BufferedImage
import
java.awt.image.
BufferedImage;
import
java.io.File;
import
javax.imageio.ImageIO;
...
BufferedImage
image =
ImageIO.read(
new File( "rabbit.jpg"
) );
Saving a BufferedImage to a
file
// BufferedImage to File
import
javax.imageio.ImageIO;
import
java.awt.image.BufferedImage;
import
java.io.File;
...
ImageIO.write(
aBufferedImage, "jpeg"
,
new File ( "snap.jpg"
)
);
ImageWriteParam is a way of
controlling exactly how the image in encoded. There is currently no PNG support
for it. This is not for injecting meta info.
Loading a
BufferedImage from an URL
// url to
BufferedImage
import
java.awt.image.BufferedImage;
import
javax.imageio.ImageIO;
...
BufferedImage
image =
null;
try
{
image =
ImageIO.read(
url );
}
catch ( IOException
e )
{
System.out.println(
"image missing" );
}
Converting Image to BufferedImage
// Image to
BufferedImage
import
java.awt.image.BufferedImage;
import
java.awt.Image;
...
BufferedImage
bufferedImage = new
BufferedImage (
imageWidth,
imageHeight,
BufferedImage.TYPE_INT_BGR
);
bufferedImage.createGraphics().drawImage(
image, 0,
0, this
);
Java图像文件的读写的更多相关文章
- JAVA用geotools读写shape格式文件
转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...
- java文件的读写操作
java文件的读写操作主要是对输入流和输出流的操作,由于流的分类很多,所以概念很容易模糊,基于此,对于流的读写操作做一个小结. 1.根据数据的流向来分: 输出流:是用来写数据的,是由程序(内存)--- ...
- java通过dom读写xml文件
java通过dom读写xml文件 要读的xml文件 <?xml version="1.0" encoding="GB2312"?><学生花名册 ...
- java FileReader/FileWriter读写文件
java FileReader/FileWriter读写字母和数字没问题,但读写汉字就乱码.记录下,后面找到解决方法再补上. public static void main(String[] args ...
- Java中如何读写cookie (二)
Java中删除cookie Cookie[] cookies=request.getCookies(); //cookies不为空,则清除 if(cookies!=null ...
- 沉淀再出发:java的文件读写
沉淀再出发:java的文件读写 一.前言 对于java的文件读写是我们必须使用的一项基本技能,因此了解其中的原理,字节流和字符流的本质有着重要的意义. 二.java中的I/O操作 2.1.文件读写的本 ...
- java 文件的读写操作
java 文件的读写操作 一.读: public String getSetting() { HttpServletRequest request=org.apache.struts2.Servle ...
- 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)
1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...
- 《手把手教你》系列技巧篇(六十七)-java+ selenium自动化测试 - 读写excel文件 - 中篇(详细教程)
1.简介 前面介绍了POI可以操作excel,也简单的提到另一个操作excle的工具,本篇介绍一个其他的可以操作excel的工具,但是这个工具有一个前提,excel文件版本只能是97-2003版本,如 ...
随机推荐
- Partial Tree---hdu5534(完全背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意:有n个节点,让这n个节点形成一棵树,这棵树的种类有很多种,现在告诉n-1个f[i],表示度 ...
- sell-- Calendar 和 Date- 01,月份不变年份+3或直接到2017
1. 2016/11/24 import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calen ...
- flex box 布局
.box{ display:flex; } .box { display: inline-flex; } .box { display:-webkit-flex; display: flex; } f ...
- Android 使用Okhttp/Retrofit持久化cookie的简便方式
首先cookie是什么就不多说了,还是不知道的话推荐看看这篇文章 Cookie/Session机制详解 深入解析Cookie技术 为什么要持久化cookie也不多说了,你能看到这篇文章代表你有这个需求 ...
- javascript:void(0)和javascript:;的用法
一.JavaScript:void(0) 我们经常会使用到 javascript:void(0) 这样的代码,那么在 JavaScript 中 javascript:void(0) 代表的是什么意思呢 ...
- Linux脚本实现“按任意键继续/Press any key to continue”效果
此代码来自lnmp一键安装包,用于实现“按任意键继续/Press any key to continue”效果: get_char() { SAVEDSTTY=`stty -g` #隐藏终端输入显示 ...
- [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [转] JVM 调优系列 & 高并发Java系列
1.JVM调优总结(1):一些概念:http://www.importnew.com/18694.html 2.JVM调优总结(2):基本垃圾回收算法:http://www.importnew.com ...
- [转]MySQL数据库引擎
经常用MySQL数据库,但是,你在用的时候注意过没有,数据库的存储引擎,可能有注意但是并不清楚什么意思,可能根本没注意过这个问题,使用了默认的数据库引擎,当然我之前属于后者,后来成了前者,然后就有了这 ...
- Linux 系统结构
Linux的系统结构一般由四部分组成 内核 1)内核 操作系统的核心,具有最基本的功能:内存管理.进程管理.设备驱动管理.文件系统管理,网络管理 内核版本(kernel)查看的三种方法 cat /pr ...