java 读写图像_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();

}

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,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,

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,this

);

相关文章

总结

以上是编程之家为你收集整理的Java图像文件的读写全部内容,希望文章能够帮你解决Java图像文件的读写所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值