我想从图像创建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

最佳答案

您正在尝试使用第三方实用程序类contrib.ch.randelshofer.quaqua.util.Images,但找不到该类。确保提供该类的库存在in your classpath

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

关于java - 我想从图像创建BufferedImage,但是它有这个异常(java.lang.NoClassDefFoundError),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3951160/

10-09 04:58