java中bufferedimage_java中image对象转化为bufferedimage对象的方法

2013-03-19 06:30:02

阅读( 745 )

在java中Image对象不能当作BufferedImage对象使用,这对我们进行ImageIO操作有很多不便,

下面这个方法可以将Image转化为BufferedImage使我们能方便的进行图像处理。

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.awt.image.ImageObserver;

/**

* @author Anthony Eden

*/

public class BufferedImageBuilder {

private static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;

public BufferedImage bufferImage(Image image) {

return bufferImage(image, DEFAULT_IMAGE_TYPE);

}

public BufferedImage bufferImage(Image image, int type) {

BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);

Graphics2D g = bufferedImage.createGraphics();

g.drawImage(image, null, null);

waitForImage(bufferedImage);

return bufferedImage;

}

private void waitForImage(BufferedImage bufferedImage) {

final ImageLoadStatus imageLoadStatus = new ImageLoadStatus();

bufferedImage.getHeight(new ImageObserver() {

public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {

if (infoflags == ALLBITS) {

imageLoadStatus.heightDone = true;

return true;

}

return false;

}

});

bufferedImage.getWidth(new ImageObserver() {

public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {

if (infoflags == ALLBITS) {

imageLoadStatus.widthDone = true;

return true;

}

return false;

}

});

while (!imageLoadStatus.widthDone && !imageLoadStatus.heightDone) {

try {

Thread.sleep(300);

} catch (InterruptedException e) {

}

}

}

class ImageLoadStatus {

public boolean widthDone = false;

public boolean heightDone = false;

}

}

分享给朋友:

亲~ 如果您有更好的答案 可在评论区发表您独到的见解。

您想查看更多的信息:

面试题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值