ImageObserver方法中具有Graphics.drawImage()功能。实际操作中最简单的方法是什么?我想ImageIO.read()返回BufferedImage,所以它们都在内存中。如何获取部分加载的图像,以便观察者调用?

最佳答案

这似乎称为图像观察器。如果您正在调用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);
    }

}

07-28 01:38