Java 选择:将图像存储在 JAR 中还是存储为 Base64 序列?

发布于 2025-01-07 01:46:49 字数 14729 浏览 0 评论 0原文

我写了一个扑克模拟器/赔率计算器。在 GUI 上,有一些 JComboBox,用户可以从中选择卡片。

现在,我有了这些卡的图像,并且还有 Base64 格式的图像。我尝试了 3 种不同的方式来显示它们。我为每个程序编写了一个小程序用于演示目的。

以下是我尝试过的3种方法:

1)。将图像存储在 JAR 文件中:

非常简单。图像进入 JAR,Java 代码从那里读取它们。之后,它创建一个包含所有这些的 JComboBox 并显示它:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;

public class ImagesFromJAR
{
    private JFrame mainframe;

    public ImagesFromJAR ()
    {
        ImageIcon[] cardslist = new ImageIcon[52];

        for (int i = 0; i < 52; ++i)
        {
            cardslist[i] = new ImageIcon (getClass (). getResource ((this.getImagePath (i))));
        }

        JComboBox cardsbox = new JComboBox (cardslist);
        cardsbox.setSelectedIndex (0);
        cardsbox.setEditable (false);

        this.mainframe = new JFrame ();
        this.mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        this.mainframe.setSize (150, 150);
        this.mainframe.setLayout (null);

        cardsbox.setBounds (20, 20, 55, 45);
        this.mainframe.add (cardsbox);

        this.mainframe.setVisible (true);
    }

    private String getImagePath (int index)
    {
        switch (index)
        {
            case 0: return "images/2c.gif";
            case 1: return "images/3c.gif";
            case 2: return "images/4c.gif";
            case 3: return "images/5c.gif";
            case 4: return "images/6c.gif";
            case 5: return "images/7c.gif";
            case 6: return "images/8c.gif";
            case 7: return "images/9c.gif";
            case 8: return "images/Tc.gif";
            case 9: return "images/Jc.gif";
            case 10: return "images/Qc.gif";
            case 11: return "images/Kc.gif";
            case 12: return "images/Ac.gif";
            case 13: return "images/2d.gif";
            case 14: return "images/3d.gif";
            case 15: return "images/4d.gif";
            ......................................
            default: return null;
        }
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                ImagesFromJAR t = new ImagesFromJAR ();
            }
        });
    }
}

2)。将 Base64 格式的图像存储在文本文件中:

我将每个图像转换为 Base64 格式,然后存储在文本文件中。 Java 代码读取该文件并通过解码文件中的行来重新创建图像。之后,创建并显示一个 JComboBox

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.IOException;

public class CodeFromFile
{
    private JFrame mainframe;

    public CodeFromFile ()
    {
        ImageIcon[] cardslist = new ImageIcon[52];

        InputStreamReader in = null;
        BufferedReader buff;

        int index = -1;

        try
        {
            in = new InputStreamReader (new FileInputStream ("card.encodings.txt"));
            buff = new BufferedReader (in);

            String line;
            byte[] ba;

            while ((line = buff.readLine ()) != null)
            {
                if (line.length () < 10)
                {
                    continue;
                }

                index++;

                ba = Base64.decode (line);

                cardslist[index] = new ImageIcon (ba);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace ();
        }
        finally
        {
            if (in != null)
            {
                try
                {
                    in.close ();
                }
                catch (IOException e2) {}
            }
        }

        JComboBox cardsbox = new JComboBox (cardslist);
        cardsbox.setSelectedIndex (0);
        cardsbox.setEditable (false);

        this.mainframe = new JFrame ();
        this.mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        this.mainframe.setSize (150, 150);
        this.mainframe.setLayout (null);

        cardsbox.setBounds (20, 20, 55, 45);
        this.mainframe.add (cardsbox);

        this.mainframe.setVisible (true);
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                CodeFromFile t = new CodeFromFile ();
            }
        });
    }
}

card.encodings.txt 的内容如下所示:

2c:
R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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9QJwWAtKonAgw/urAnCnkBwLYhBm/FtZ2mHQwZCMcoxs0w1ZDOIdhan0Al3iMbZeQZEhEAOw==
5c:
R0lGODlhHgAmALMAAAAAAP///+7u7szMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///yH5BAEAAA8ALAAAAAAeACYAAATA8D1Aq734yhm6/2AIApxonh6FrqLKvp2LGg7ADKtsGpYjoDrR4nIAAlY1i8K4ahCZogSgobhUHYbWMYTIYAqhYCfprSTCWxC5MrycR+nP4YIwtCsEtGk+NQgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKJgwVS52hIQQWWaaGj4krAwAOCD+vKFIUDpWQpyEHQz5QL12umywMpbyaCAjCLzfOizgBjrYnbQu7yp+dJdwjG2XiGRIRADs=

.. 并且它适用于每个图像/卡片。

3)。第三种方法是将图像直接以 Base64 String 形式存储在源代码中。像这样:(

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;

public class CodeFromSource
{
    private JFrame mainframe;

    public CodeFromSource ()
    {
        ImageIcon[] cardslist = new ImageIcon[52];

        byte[] ba;

        for (int i = 0; i < 52; ++i)
        {
            ba = Base64.decode (this.getImageCode (i));
            cardslist[i] = new ImageIcon (ba);
        }

        JComboBox cardsbox = new JComboBox (cardslist);
        cardsbox.setSelectedIndex (0);
        cardsbox.setEditable (false);

        this.mainframe = new JFrame ();
        this.mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        this.mainframe.setSize (150, 150);
        this.mainframe.setLayout (null);

        cardsbox.setBounds (20, 20, 55, 45);
        this.mainframe.add (cardsbox);

        this.mainframe.setVisible (true);
    }

    private String getImageCode (int index)
    {
        switch (index)
        {
            case 0: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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aSJLAkHDK9QJwWAtKonAgw/urAnCnkBwLYhBm/FtZ2mHQwZCMcoxs0w1ZDOIdhan0Al3iMbZeQZEhEAOw==";
            case 3: return "R0lGODlhHgAmALMAAAAAAP///+7u7szMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///yH5BAEAAA8ALAAAAAAeACYAAATA8D1Aq734yhm6/2AIApxonh6FrqLKvp2LGg7ADKtsGpYjoDrR4nIAAlY1i8K4ahCZogSgobhUHYbWMYTIYAqhYCfprSTCWxC5MrycR+nP4YIwtCsEtGk+NQgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKJgwVS52hIQQWWaaGj4krAwAOCD+vKFIUDpWQpyEHQz5QL12umywMpbyaCAjCLzfOizgBjrYnbQu7yp+dJdwjG2XiGRIRADs=";
            case 4: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXZIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaNwNiAE+gsTJMDKW3PExpqLgvAw+fmTsLD6MMtr04DwxITA1gNgQAn2KJztbYLky8gDgD3i0GANtqOw0O3w/VNgUAdwQOwNw3Bl5ox6+tNf5mrKhDMIWIEAA7";
            case 5: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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wNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaMghFk5k7CA4DAWJIrbF6aS0OkL44BQClbbqgvy4GyC6DvWrGAH+RANOrMg3QBAwPYDePJLhbxUAGD8vMQAQOn+07A23B4jICJArs969xNf5mrKhDMIWIEAA7";
            case 8: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAX4IAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaLgIJDp8BBA1GSKsvDkWfAw9VCA8DvC8FAJ8LDS4NW4A3BMouwi7Exy7TmMlpLUyJL67bLQYApealQbHa1AHpLvDi7C3kAd0u4NE29gHWLdj2yejHzBm0eTeSAbNGzJhAGGVINKuXi8Guh6/i1Mg4Y0WdjylEhAAAOw==";
            case 9: return "R0lGODlhHgAmALMAAAAAAP///+7u7szMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///yH5BAEAAA8ALAAAAAAeACYAAAS38D1Aq734yhm6/2AIApxonh6FrqLKvp2LGg7ADKtsGpYjoDrR4nIAAlY1i8K4ahCZogSgobhUHYbWMYTIYAqhYCfprSTCWxC5MrycR+nP4YIwtCsEtGk+NQgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKIQM9iS9deaUsczipK6uYoSKvrSizUK4ArLe1ubAvtp2xIQcOL54BVQQIb7TDFEvGwp8xJdMjG2XZGRIRADs=";
            case 10: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu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xonh6FrqLKvp2LGg7ADKtsGpYjoDrR4nIAAlY1i8K4ahCZogSgobhUHYbWMYTIYAqhYCfprSTCWxC5MrycR+nP4YIwtCsEtGk+NQgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKJgMAeYkvVW9QKAIODD6mKwcIc4+QoSENA6wMsCcFgAFVOJ23H4MdBQAIqiKjFw3MIQm1XZWbIKw/HqNLticL3cYAtR+ZSQ4fZLzXnyMl7e4SZfMZEhEAOw==";
            case 13: return "R0lGODlhHgAmAMQAAAAAAP////8AAP8REf8iIv8zM/9ERP9VVf9mZv93d/+IiP+Zmf+qqv+7u//MzP/d3f/u7v///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABEALAAAAAAeACYAAAW/YBQBZGme6CmOQeu+cAwDrGzfLonvss4HiJ9vtxAkeMMbhCAQNHZJm6EpIEBw0ZiC2gzesq8Gl7r4ApTMcfPZO9sOairBBm4t40662yZWK8w7W1wGWHs3U01WhTx3eYs8DAJejzwJV1CGP5iamnWcM5kxD4gGDps4BQwBDgQDl3o4C6YtkX+AmhACtrCaD468PwqElDtLD6c8BmzENwllSKExCbsJx8A2CWNzt9hqk22f0OFQNeMxNCIp6uskIiEAOw==";
            .........
            default: return null;
        }
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                CodeFromSource t = new CodeFromSource ();
            }
        });
    }
}

我不得不删除一些 switch case,因为 StackOverflow 上有 30 K 字符的限制)

这些方法中的每一个都会产生相同的结果,即:

Result

PS:Base64 类只是一些对 Base64 信息进行编码/解码的实用程序类(您可以在下面的链接中找到它)。

如果您想要这方面的所有资源(图像、源代码等),可以从这里获取:资源

现在我们已经弄清楚了...我想知道这 3 种方法中哪一种更好。它们各自的优点和缺点是什么?我应该使用哪一个?为什么 ?我还讨论了 JAR 文件损坏的可能性。

我唯一不关心的是性能。

I wrote a poker simulator / odds calculator. On the GUI, there are some JComboBoxes from which the user can choose the cards.

Now, I have the images for those cards and I also have them in Base64 format. There are 3 different ways I tried to display them. I wrote a small program for each one for demonstration purposes.

Here are the 3 methods I have tried:

1). Store the images in the JAR file:

Pretty straightforward. The images go into the JAR and the Java code reads them from there. After that, it creates a JComboBox with all of them and displays it:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;

public class ImagesFromJAR
{
    private JFrame mainframe;

    public ImagesFromJAR ()
    {
        ImageIcon[] cardslist = new ImageIcon[52];

        for (int i = 0; i < 52; ++i)
        {
            cardslist[i] = new ImageIcon (getClass (). getResource ((this.getImagePath (i))));
        }

        JComboBox cardsbox = new JComboBox (cardslist);
        cardsbox.setSelectedIndex (0);
        cardsbox.setEditable (false);

        this.mainframe = new JFrame ();
        this.mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        this.mainframe.setSize (150, 150);
        this.mainframe.setLayout (null);

        cardsbox.setBounds (20, 20, 55, 45);
        this.mainframe.add (cardsbox);

        this.mainframe.setVisible (true);
    }

    private String getImagePath (int index)
    {
        switch (index)
        {
            case 0: return "images/2c.gif";
            case 1: return "images/3c.gif";
            case 2: return "images/4c.gif";
            case 3: return "images/5c.gif";
            case 4: return "images/6c.gif";
            case 5: return "images/7c.gif";
            case 6: return "images/8c.gif";
            case 7: return "images/9c.gif";
            case 8: return "images/Tc.gif";
            case 9: return "images/Jc.gif";
            case 10: return "images/Qc.gif";
            case 11: return "images/Kc.gif";
            case 12: return "images/Ac.gif";
            case 13: return "images/2d.gif";
            case 14: return "images/3d.gif";
            case 15: return "images/4d.gif";
            ......................................
            default: return null;
        }
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                ImagesFromJAR t = new ImagesFromJAR ();
            }
        });
    }
}

2). Store the images in Base64 format in a text-file:

I converted each image in Base64 format and then stored in a text-file. The Java code reads that file and recreates the images by decoding the lines in the file. After that, a JComboBox is created and displayed:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.IOException;

public class CodeFromFile
{
    private JFrame mainframe;

    public CodeFromFile ()
    {
        ImageIcon[] cardslist = new ImageIcon[52];

        InputStreamReader in = null;
        BufferedReader buff;

        int index = -1;

        try
        {
            in = new InputStreamReader (new FileInputStream ("card.encodings.txt"));
            buff = new BufferedReader (in);

            String line;
            byte[] ba;

            while ((line = buff.readLine ()) != null)
            {
                if (line.length () < 10)
                {
                    continue;
                }

                index++;

                ba = Base64.decode (line);

                cardslist[index] = new ImageIcon (ba);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace ();
        }
        finally
        {
            if (in != null)
            {
                try
                {
                    in.close ();
                }
                catch (IOException e2) {}
            }
        }

        JComboBox cardsbox = new JComboBox (cardslist);
        cardsbox.setSelectedIndex (0);
        cardsbox.setEditable (false);

        this.mainframe = new JFrame ();
        this.mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        this.mainframe.setSize (150, 150);
        this.mainframe.setLayout (null);

        cardsbox.setBounds (20, 20, 55, 45);
        this.mainframe.add (cardsbox);

        this.mainframe.setVisible (true);
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                CodeFromFile t = new CodeFromFile ();
            }
        });
    }
}

The content of the card.encodings.txt is like this:

2c:
R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXKIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4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3d2ZmZlVVVURERDMzMyIiIhEREf///yH5BAEAAA8ALAAAAAAeACYAAATA8D1Aq734yhm6/2AIApxonh6FrqLKvp2LGg7ADKtsGpYjoDrR4nIAAlY1i8K4ahCZogSgobhUHYbWMYTIYAqhYCfprSTCWxC5MrycR+nP4YIwtCsEtGk+NQgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKJgwVS52hIQQWWaaGj4krAwAOCD+vKFIUDpWQpyEHQz5QL12umywMpbyaCAjCLzfOizgBjrYnbQu7yp+dJdwjG2XiGRIRADs=

.. and it goes on for every image/card.

3). The third way is to store the images as Base64 Strings directly in the source code. Like this:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;

public class CodeFromSource
{
    private JFrame mainframe;

    public CodeFromSource ()
    {
        ImageIcon[] cardslist = new ImageIcon[52];

        byte[] ba;

        for (int i = 0; i < 52; ++i)
        {
            ba = Base64.decode (this.getImageCode (i));
            cardslist[i] = new ImageIcon (ba);
        }

        JComboBox cardsbox = new JComboBox (cardslist);
        cardsbox.setSelectedIndex (0);
        cardsbox.setEditable (false);

        this.mainframe = new JFrame ();
        this.mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        this.mainframe.setSize (150, 150);
        this.mainframe.setLayout (null);

        cardsbox.setBounds (20, 20, 55, 45);
        this.mainframe.add (cardsbox);

        this.mainframe.setVisible (true);
    }

    private String getImageCode (int index)
    {
        switch (index)
        {
            case 0: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXKIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaMQN9DEhgN5MBBA5OtzIHtgEGAJ9qpwLEvjsDAImtsTcIkMo3Ag4DmUAMztSbaajQMAmfCdig4aInDt3pfuyvQTXwMyt19ikiIQA7";
            case 1: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXMIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaMowMA5k7B2kEDg22OJ+DT62xNwtbYDsDC5C9OAYkD6Wgw7JFSMKnzp9qqg/aq8gAidc3BQB3j3/jNgINJA3e26+tNfIzK3X4KSIhADs=";
            case 2: return "R0lGODlhHgAmALMAAAAAAP///+7u7szMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///yH5BAEAAA8ALAAAAAAeACYAAATC8D1Aq734yhm6/2AIApxonh6FrqLKvp2LGg7ADKtsGpYjoDrR4nIAAlY1i8K4ahCZogSgobhUHYbWMYTIYAqhYCfprSTCWxC5MrycR+nP4YIwtCsEtGk+NQgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKJwkMmKEiBTalLAINDaSJLAkHDK9QJwWAtKonAgw/urAnCnkBwLYhBm/FtZ2mHQwZCMcoxs0w1ZDOIdhan0Al3iMbZeQZEhEAOw==";
            case 3: return "R0lGODlhHgAmALMAAAAAAP///+7u7szMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///yH5BAEAAA8ALAAAAAAeACYAAATA8D1Aq734yhm6/2AIApxonh6FrqLKvp2LGg7ADKtsGpYjoDrR4nIAAlY1i8K4ahCZogSgobhUHYbWMYTIYAqhYCfprSTCWxC5MrycR+nP4YIwtCsEtGk+NQgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKJgwVS52hIQQWWaaGj4krAwAOCD+vKFIUDpWQpyEHQz5QL12umywMpbyaCAjCLzfOizgBjrYnbQu7yp+dJdwjG2XiGRIRADs=";
            case 4: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXZIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaNwNiAE+gsTJMDKW3PExpqLgvAw+fmTsLD6MMtr04DwxITA1gNgQAn2KJztbYLky8gDgD3i0GANtqOw0O3w/VNgUAdwQOwNw3Bl5ox6+tNf5mrKhDMIWIEAA7";
            case 5: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXGIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaMUwniaCxMIckD2A2j08tCX+2OAO/LQ61w0AFDpmnCgnOQA5IvDsGDai3YZ+t2zEPA9o/hj+uLwtp46+tNewzK3XyKSIhADs=";
            case 6: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXYIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaMowPn6CxMG1wBLRgNw4NLguQrbcvDE4tDAm9S0YDCMPEPIxbmTwGC161ajsGDskA24BdfwHHzDECAOUID+gxCw9wAg3l3DtiJOLjr601/TNW1BmYQkQIADs=";
            case 7: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXZIAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaMghFk5k7CA4DAWJIrbF6aS0OkL44BQClbbqgvy4GyC6DvWrGAH+RANOrMg3QBAwPYDePJLhbxUAGD8vMQAQOn+07A23B4jICJArs969xNf5mrKhDMIWIEAA7";
            case 8: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAX4IAQBZGme6CmOQeu+cAwDrGzfLonvss7/LR/u8AA0CDuh7WB6CHBKGeOEgAJ2RdPCunNQuTIFwLE4lR+H3jWWSKEMsWgr6y4p4msYvTQ93Wd5LwgnCQd9JQV4NoNjBwIMAYwNcIo8ZVVJgTIJhwuYNnIubW6UlUA8oaemqjeprDmaLgIJDp8BBA1GSKsvDkWfAw9VCA8DvC8FAJ8LDS4NW4A3BMouwi7Exy7TmMlpLUyJL67bLQYApealQbHa1AHpLvDi7C3kAd0u4NE29gHWLdj2yejHzBm0eTeSAbNGzJhAGGVINKuXi8Guh6/i1Mg4Y0WdjylEhAAAOw==";
            case 9: return "R0lGODlhHgAmALMAAAAAAP///+7u7szMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///yH5BAEAAA8ALAAAAAAeACYAAAS38D1Aq734yhm6/2AIApxonh6FrqLKvp2LGg7ADKtsGpYjoDrR4nIAAlY1i8K4ahCZogSgobhUHYbWMYTIYAqhYCfprSTCWxC5MrycR+nP4YIwtCsEtGk+NQgWAXwMYHosVUU5cSIIdwqIJmIeXV6EhTAskZeWmieZnCmKIQM9iS9deaUsczipK6uYoSKvrSizUK4ArLe1ubAvtp2xIQcOL54BVQQIb7TDFEvGwp8xJdMjG2XZGRIRADs=";
            case 10: return "R0lGODlhHgAmAMQAAAAAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZlVVVURERDMzMyIiIhEREf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABAALAAAAAAeACYAAAXvIAQBZGme6CmO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mKJgMAeYkvVW9QKAIODD6mKwcIc4+QoSENA6wMsCcFgAFVOJ23H4MdBQAIqiKjFw3MIQm1XZWbIKw/HqNLticL3cYAtR+ZSQ4fZLzXnyMl7e4SZfMZEhEAOw==";
            case 13: return "R0lGODlhHgAmAMQAAAAAAP////8AAP8REf8iIv8zM/9ERP9VVf9mZv93d/+IiP+Zmf+qqv+7u//MzP/d3f/u7v///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABEALAAAAAAeACYAAAW/YBQBZGme6CmOQeu+cAwDrGzfLonvss4HiJ9vtxAkeMMbhCAQNHZJm6EpIEBw0ZiC2gzesq8Gl7r4ApTMcfPZO9sOairBBm4t40662yZWK8w7W1wGWHs3U01WhTx3eYs8DAJejzwJV1CGP5iamnWcM5kxD4gGDps4BQwBDgQDl3o4C6YtkX+AmhACtrCaD468PwqElDtLD6c8BmzENwllSKExCbsJx8A2CWNzt9hqk22f0OFQNeMxNCIp6uskIiEAOw==";
            .........
            default: return null;
        }
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                CodeFromSource t = new CodeFromSource ();
            }
        });
    }
}

(I had to delete some switch cases because there is a 30 K characters limit on StackOverflow)

Each of these methods produce the same result, which is this:

Result

PS: The Base64 class is just some utility class that encodes/decodes Base64 information (you can find it in the link below).

If you want all the resources for this (images, source code etc.), you can get them from here: Resources

Now that we cleared that up... what I want is to know which of these 3 methods is better. What are the upsides and downsides of each of them ? Which one should I use ? Why ? I'm also talking about the possibility of JAR file corruption.

The only thing I don't care about is performance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来是傀儡 2025-01-14 01:46:49
  1. 更好,因为不涉及转换,并且您的代码可以从类路径读取它们。这使得您的应用程序自包含。损坏不是问题,因为这会使整个应用程序变得无用 - 不仅图像文件

  2. 将涉及转换的运行时损失(启动时间和内存),而且还会引入配置文件。

  3. 和 2 一样糟糕,但灵活性也较差(不重新编译就无法更改图像)

  1. is preferable as there is no conversion involved, and your code can read them from classpath. This makes your appliction self contained. Corruption is not an issue, as this would render whole application useless - not only image files

  2. Will involve runtime penalty for conversion ( startup time and memory ), and alsop introduces configuration files.

  3. Is as bad as 2, but is also less flexible ( you can not change images without recompiling)

不爱素颜 2025-01-14 01:46:49

将图像放入 Jar 中是最常见的,因此其他人最容易使用或回答问题(这可能重要也可能不重要)。

我怀疑 2 或 3 会产生更小的最终可分配结果。至于腐败的可能性。这似乎适用于 Jars 中的类或资源,或者纯文本文件。那么问题就变成了“每个人被破坏的可能性有多大?”我认为答案是“字节越多,机会就越大”。

Putting the images in the Jar is most common, and therefore easiest for others to work with or answer questions on (which might or might not be important).

I doubt that 2 or 3 would produce a smaller end distributable. As to the possibility of corruption. That would seem to apply to either classes or resource in Jars, or to plain text files. The issue then becomes "how likely is each to be corrupted?" to which I think the answer is "the more bytes, the greater the chance".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文