在Java Swt&中缩放图像记忆消耗

时间:2013-05-01 16:54:36

标签: java image swt scaling

我有一个java swt应用程序。在容器的resize方法中,我想调整图像大小,使用以下代码显示。然而似乎 - 即使我处置了一切(真的吗?)内存消耗一直在增加...... 我找不到我的错误。这段代码为什么以及在哪里占用了我所有的记忆?

这是Resize-Listener:

tabCover.addListener(SWT.Resize, new Listener() {

        public void handleEvent(Event event) {
            // Set images: cover_front
            int width = tabCover.getSize().x - 30;
            int height = tabCover.getSize().y - 30;
            Image buffer_Coverfront;

            buffer_Coverfront = new Image(Display.getDefault(), filename);
            lblCoverfront.setImage(Helper.ImageScale(buffer_Coverfront, width, height));
            buffer_Coverfront.dispose();
            buffer_Coverfront = null;

        } // handleEvent
    }); // Listener

这里是缩放功能:

public static Image ImageScale(Image image, int width, int height) {


    ImageData data = image.getImageData();

    // Some logic to keep the aspect ratio
    float img_height = data.height;
    float img_width = data.width;
    float container_height = height;
    float container_width = width;

    float dest_height_f = container_height;
    float factor = img_height / dest_height_f;

    int dest_width = (int) Math.floor(img_width / factor );
    int dest_height = (int) dest_height_f;

    if(dest_width > container_width) {
        dest_width = (int) container_width;
        factor = img_width / dest_width;
        dest_height = (int) Math.floor(img_height / factor);

    }

    // Image resize
    data = data.scaledTo(dest_width, dest_height);
    Image scaled = new Image(Display.getDefault(), data);
    image.dispose();
    return scaled;
}

2 个答案:

答案 0 :(得分:2)

据我所知handleEvent(...)方法,您忘记丢弃旧图像。

tabCover.addListener(SWT.Resize, new Listener() {
    public void handleEvent(Event event) {
        // Set images: cover_front
        int width = tabCover.getSize().x - 30;
        int height = tabCover.getSize().y - 30;
        Image buffer_Coverfront;

        buffer_Coverfront = new Image(Display.getDefault(), filename);
        Image old = lblCoverfront.getImage();
        lblCoverfront.setImage(Helper.ImageScale(buffer_Coverfront, width, height));
        buffer_Coverfront.dispose();
        buffer_Coverfront = null;

        if (old != null) old.dispose();

    } // handleEvent
}); // Listener

上面的代码假设控件的图像仅在此方法中 设置。如果您还从其他地方设置了初始图像,则必须改进处置条件......

答案 1 :(得分:2)

由于我无法在评论中发布代码,因此它是:

tabCover.addListener(SWT.Resize, new Listener() {

        public void handleEvent(Event event) {
            // Set images: cover_front
            int width = tabCover.getSize().x - 30;
            int height = tabCover.getSize().y - 30;

                String filename_front = my_collection.getGlobal_path() + "Cover/" + selected_movie.getCover_front();

                try {
                    Image image = new Image(Display.getDefault(), filename_front);
                    Image scaled = Helper.ImageScale(image, width, height);
                    lblCoverfront.setImage(scaled);
                    image.dispose();
                    scaled.dispose();

                } catch (Exception e) {
                } // end try

        } // handleEvent
    }); // Listener

再次感谢Tonny Madsen的有用提示。