0

我想从图像创建一个 BufferedImage 但它没有运行。这是什么异常?

代码:

BufferedImage src = toBufferedImage(image1);

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();
    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = contrib.ch.randelshofer.quaqua.util.Images.hasAlpha(image);
    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }
        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }
    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }
    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

例外:

Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: contrib/ch/randelshofer/quaqua/util/Images
        at LoadImageAndScale.toBufferedImage(LoadImageAndScale.java:87)
        at LoadImageAndScale.paint(LoadImageAndScale.java:59)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.ClassNotFoundException: contrib.ch.randelshofer.quaqua.util.Images
        at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:210)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:143)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        ... 14 more

类路径中的包 contrib.ch.randelshofer.quaqua.util.Images 但如果删除此部分,则会出现另一个异常:

boolean hasAlpha = contrib.ch.randelshofer.quaqua.util.Images.hasAlpha(image); 

更改为:

boolean hasAlpha = hasAlpha(image);

它无法编译并显示此消息:

E:\3nd stage\java\test\LoadImageAndScale.java:87: cannot find symbol
symbol  : method hasAlpha(java.awt.Image)
location: class LoadImageAndScale
    boolean hasAlpha = hasAlpha(image);
                       ^
1 error
4

3 回答 3

2

您正在尝试使用第三方实用程序类contrib.ch.randelshofer.quaqua.util.Images,但未找到。确保提供该类的库存在于您的类路径中。

关于您的编辑:,当您收到 NoClassDefFoundError 时,该库在您的类路径中不正确。您无法通过简单地删除完全限定的类名来解决该问题。您必须指出在哪个类中可以找到 hasAlpha() 方法并使该类在类路径中可用。

于 2010-10-16T22:56:14.920 回答
0

Images 类不在您的类路径中。

于 2010-10-16T22:54:59.993 回答
0

讨厌这么说,但图像不在你的类路径中。它可能在编译时,但类加载器在运行时找不到它。检查它是否在您的 Eclipse 运行配置、启动脚本、命令行参数中……如果不知道您是如何启动它的,那就无能为力了。

于 2010-10-16T23:27:57.713 回答