简体   繁体   English

旋转小程序。jtogglebutton的大小是6mb。 我如何缩小尺寸

[英]swing applet.. jtogglebutton size is 6mb. how can i reduce the size

I am working on an application which takes images in a folder and shows them as thumbnails for further selection and operation individually. 我正在开发一个应用程序,该应用程序在文件夹中拍摄图像,并将其显示为缩略图,以便分别进行进一步选择和操作。 below is the code that adds them to Jtogglebuttons. 下面是将它们添加到Jtogglebuttons的代码。

toglBtn=new JToggleButton(""+i,new ImageIcon(ImageIO.read(new File(listOfFiles[i].getAbsolutePath())).getScaledInstance(139, 163, BufferedImage.SCALE_SMOOTH)));

My original images in the folder are <100kb but the Jtogglebutton size is 6mb. 我在该文件夹中的原始图像小于100kb,但Jtogglebutton的大小为6mb。 Is there any way to reduce the size of these? 有什么办法可以减小这些尺寸? currently its taking up all my heap space and giving me an out of memory error when ever there are more than 40 files. 当前,它占用了我所有的堆空间,并且当有40多个文件时,出现了内存不足错误。 I already increased the heap size to 512MB but after analyzing heap dumps with MAT, i figured i need to reduce the thumbnailsizes to solve this error. 我已经将堆大小增加到512MB,但是在使用MAT分析堆转储之后,我认为我需要减小缩略图大小以解决此错误。

Is there any way i can force the thumbnail to be a jpeg? 有什么办法可以强制缩略图为jpeg? or any other way of adding thumbnails to jtogglebuttons? 或以其他方式向jtogglebuttons添加缩略图?

Final Update: I used thumbnailator to get the size of the button down to a few kb 最终更新:我使用缩略图器将按钮的大小减小到了几kb

try{
                            BufferedImage originalImage = ImageIO.read(new File(listOfFiles[i].getAbsolutePath()));                             
                            Image image =Thumbnails.of(originalImage).size(138, 163).asBufferedImage();
                            toglBtn=new JToggleButton(""+i,new ImageIcon(image));
                            }

Perhaps your one Java line is keeping the original image, the scaled image, and the image icon in memory. 也许您的一条Java行正在将原始图像,缩放图像和图像图标保留在内存中。

Breaking up the code by doing it this way ensures that the original image and scaled image are dropped for garbage collection. 通过这样做来破坏代码,可以确保删除原始图像和缩放后的图像以进行垃圾回收。

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;

    String text = "" + i;
    File imageFile = new File(listOfFiles[i].getAbsolutePath());
    BufferedImage image = ImageIO.read(imageFile);
    Image scaledImage = image.getScaledInstance(139, 163, 
            BufferedImage.SCALE_SMOOTH);
    ImageIcon imageIcon = new ImageIcon(scaledImage);
    toglBtn = new JToggleButton(text, imageIcon);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM