ImageObserver
方法中有Graphics.drawImage()
种能力。看到这个行动的最简单方法是什么?我猜ImageIO.read()
会返回BufferedImage
所以它全部都在内存中。如何获取部分加载的图像,以便观察者调用?
答案 0 :(得分:3)
这似乎叫图像观察者。如果您正在调用drawImage
方法且图片仍在处理中,则会开始使用ImageObserver
。缩放是一个通常会导致图像处理的例子。
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageObserverDemo extends Box{
Image bi;
Image bi2;
Dimension d = new Dimension(300,200);
public ImageObserverDemo() {
super(BoxLayout.Y_AXIS);
try {
bi = ImageIO.read(new URL("http://apod.nasa.gov/apod/image/0304/bluemarble2k_big.jpg"));
bi = bi.getScaledInstance(100, 100, BufferedImage.SCALE_SMOOTH);
bi2 = new BufferedImage(bi.getWidth(this), bi.getHeight(this), BufferedImage.TYPE_INT_ARGB);
bi2.getGraphics().drawImage(bi, 0, 0, this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean imageUpdate(final Image img, int infoflags, int x, int y,
int width, int height) {
boolean result = true;
if((infoflags | ImageObserver.FRAMEBITS) == ImageObserver.FRAMEBITS){
result = false;
} else if((infoflags | ImageObserver.ALLBITS) == ImageObserver.ALLBITS){
result = false;
}
if(result){
System.out.println("Image incomplete");
} else{
System.out.println("Complete");
}
return result;
}
@Override
public Dimension getPreferredSize(){
return d;
}
public static void main(String[] args){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ImageObserverDemo());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}