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版本,如 ...
随机推荐
- VS2013搭建wxWidgets开发环境
一.安装 前往官网下载最新wxWidgets 3.0.0. https://sourceforge.net/projects/wxwindows/files/3.0.0/wxMSW-3.0.0-Set ...
- css3背景色渐变
<style> .test { width: 200px; height: 200px; background: -moz-linear-gradient(top, #8fa1ff, #3 ...
- Hibernate中in语句ids
Query query=session.createQuery("from User where id in (:ids)"); query.setParameterList(&q ...
- java中 set,list,array(集合与数组)相互转换
public static Object[] List2Array(List<Object> oList) { Object[] oArray = oList.toArray(new ...
- saltstack故障解决
[root@saltstack_s ~]# salt '*' test.pingSalt request timed out. The master is not responding. If thi ...
- 1、JavaScript入门篇
一.你知道,为什么JavaScript非常值得我们学习吗? 1. 所有主流浏览器都支持JavaScript. 2. 目前,全世界大部分网页都使用JavaScript. 3. 它可以让网页呈现各种动态效 ...
- hibernate中@Entity和@Table的区别
Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...
- Keepalived 安装与配置
下载:http://www.keepalived.org/ what is keepalived? Keepalived is a routing software written in C. The ...
- PostgreSQL 系统的基本体系结构
PostgreSQL 使用客户机/服务器(C/S)的模式提供服务,一个PostgreSQL会话由下列相关的进程(程序)组成: (1)一个服务器端进程.该进程管理数据库文件,接受客户端与数据库的连接,且 ...
- Lintcode: Update Bits
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits be ...