读取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图像文件的读写的更多相关文章

  1. JAVA用geotools读写shape格式文件

    转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...

  2. java文件的读写操作

    java文件的读写操作主要是对输入流和输出流的操作,由于流的分类很多,所以概念很容易模糊,基于此,对于流的读写操作做一个小结. 1.根据数据的流向来分: 输出流:是用来写数据的,是由程序(内存)--- ...

  3. java通过dom读写xml文件

    java通过dom读写xml文件 要读的xml文件 <?xml version="1.0" encoding="GB2312"?><学生花名册 ...

  4. java FileReader/FileWriter读写文件

    java FileReader/FileWriter读写字母和数字没问题,但读写汉字就乱码.记录下,后面找到解决方法再补上. public static void main(String[] args ...

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

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

  6. 沉淀再出发:java的文件读写

    沉淀再出发:java的文件读写 一.前言 对于java的文件读写是我们必须使用的一项基本技能,因此了解其中的原理,字节流和字符流的本质有着重要的意义. 二.java中的I/O操作 2.1.文件读写的本 ...

  7. java 文件的读写操作

    java  文件的读写操作 一.读: public String getSetting() { HttpServletRequest request=org.apache.struts2.Servle ...

  8. 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)

    1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...

  9. 《手把手教你》系列技巧篇(六十七)-java+ selenium自动化测试 - 读写excel文件 - 中篇(详细教程)

    1.简介 前面介绍了POI可以操作excel,也简单的提到另一个操作excle的工具,本篇介绍一个其他的可以操作excel的工具,但是这个工具有一个前提,excel文件版本只能是97-2003版本,如 ...

随机推荐

  1. JS判断对象类型

    对于确定JS内置对象类型,JS提供了typeof运算符,该运算符得到的结果为以下6种:number,boolean,string,function,object,undefined.不过对绝大多数对象 ...

  2. Shell数组例子

    Shell数组例子 循环打印数组,并统计数组的个数: [root@slavedb array]# cat a.sh #!/bin/bash array=( freddy freddie tang sh ...

  3. CentOS7|RHEL忘记root密码

    某一服务器长时间不使用,或者由于频繁修改root密码,导致忘记root密码无法登陆系统问题,通过进入单用户修改root密码,CentOS7|RHEL7与6系列有一些区别,不在适用于7. 1.在启动gr ...

  4. APICloud上有关iOS证书的一些问题

    1. 苹果开发者账号及其区别: 苹果的开发者账号分为个人.公司和企业三类. 个人是99$一年,只能个人使用,可以提交应用到AppStore: 公司的也是99$,但是可以邀请其它成员一起使用,可以提交应 ...

  5. 30天,O2O速成攻略【8.22北京站】

    活动概况 时间:2015年8月22日13:30-16:30 地点:车库咖啡(北京市海淀西大街48号鑫鼎宾馆二层) 主办:APICloud.融云.品读者 网址:www.apicloud.com 费用:免 ...

  6. 使用TextWatcher监听EditText变化

    public class MainActivity extends AppCompatActivity { private TextView mTextView; private EditText m ...

  7. Glossary of view transformations

    Glossary of view transformations The following terms are used to define view orientation, i.e. trans ...

  8. OpenGIS Simple feature access

    OGIS规范定义的几何对象定义 Curve:A Curve is a 1-dimensional geometric object usually stored as a sequence of Po ...

  9. python判断类型

    方法 isinstance(obj, type) 示例 >>> print isinstance(, int) True >>> print isinstance( ...

  10. javascript设计模式学习之十二——享元模式

    一.享元模式的定义及使用场景 享元模式是为了解决性能问题而诞生的设计模式,这和大部分设计模式为了提高程序复用性的原因不太一样,如果系统中因为创建了大量类似对象而导致内存占用过高,享元模式就非常有用了. ...