博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java图像文件的读写
阅读量:5045 次
发布时间:2019-06-12

本文共 3430 字,大约阅读时间需要 11 分钟。

读取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 );

转载于:https://www.cnblogs.com/xuepei/p/3679755.html

你可能感兴趣的文章
Android开发之监听或获取手机短信内容 分类: Android 消...
查看>>
foreach---集合已修改;可能无法执行枚举操作。
查看>>
BZOJ-3505-数三角形-CQOI2014
查看>>
CentOS 安装 debuginfo-install
查看>>
CSS布局:让页底内容永远固定在底部
查看>>
RichEdit
查看>>
路由器和交换机的区别
查看>>
Coursera台大机器学习课程笔记14 -- Validation
查看>>
易之 - 我是个算命先生(2014年3月23日)
查看>>
C# :面向对象
查看>>
JavaScript中的正则表达式
查看>>
【公众号系列】SAP HANA 平台的优势
查看>>
VMware+Fedora20+secureCRT7.0配置心得
查看>>
20172328《程序设计与数据结构》第五周学习总结
查看>>
memcached完全剖析–1. memcached的基础(转)
查看>>
Eigen vs Numpy时间对比
查看>>
Java学习笔记(七)——获取类中方法的信息,java的LinkedList
查看>>
js修改伪类元素样式
查看>>
SQL常用语句
查看>>
多线程之ReadWriteLock模拟缓存(九)
查看>>