如何在C ++中读取图像并将其绘制为加速图像?

时间:2012-04-18 04:53:19

标签: c++ image-processing

我想从我的电脑上读取一张图像并将其存储在我的c ++程序中。我有一些java图像处理的经验,但完全不知道如何在c ++中做到这一点。我想实现以下java代码在c ++中的作用:

BufferedImage sourceImage = null;

 try {
        // The ClassLoader.getResource() ensures we get the sprite

        // from the appropriate place, this helps with deploying the game

        // with things like webstart. You could equally do a file look

        // up here.

        URL url = this.getClass().getClassLoader().getResource(ref);

        if (url == null) {
            fail("Can't find ref: "+ref);
        }

        // use ImageIO to read the image in

        sourceImage = ImageIO.read(url);
    } catch (IOException e) {
        fail("Failed to load: "+ref);
    }

    // create an accelerated image of the right size to store our sprite in

    GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);

    // draw our source image into the accelerated image

    image.getGraphics().drawImage(sourceImage,0,0,null);

    // create a sprite, add it the cache then return it

    Sprite sprite = new Sprite(image);
    sprites.put(ref,sprite);

    return sprite;

任何人都可以给我一个暗示吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

如果您希望将图像用作矩阵,则可以使用OpenCV库。 该库提供了一个简单的图像加载和存储界面,但它的主要应用是图像处理和计算机视觉。所以它可以是一种“反对鸟类的炮兵”。