0

通过 imageIcon 将字符串数据转换为图像时,高度和宽度变为 -1。可能是什么问题呢?

String image = request.getParameter("image");

if (image != null && image.length() > 0) 

   {
        byte[] decoded = Base64.decodeBase64(image.getBytes());

        ImageIcon img = new ImageIcon(decoded);

        if (img.getIconWidth() > -1 && img.getIconHeight() > -1) {
            image1 = img.getImage();
        }
    }

imageIcon 的高度和宽度正在变为 -1

4

1 回答 1

1

这应该有效:

File b64 = new File("loop.png.b64");
FileInputStream fis = new FileInputStream(b64);
byte[] bytes = IOUtils.toByteArray(fis);
byte[] decoded = Base64.decodeBase64(bytes);
ImageIcon img = new ImageIcon(decoded);

if (img.getIconWidth() > -1 && img.getIconHeight() > -1) {
  System.out.println("OK");
}

怀特loop.png.b64是:

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QAAAAAAAD5Q7t/AAAACXBI
WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3AgBFxQTkp15LgAAAbNJREFUOMutUz1LIwEUnDhh/ai2
VIOyTRoRtVERAm4iWggKV9kGrbTI+lGIlfgL1vwC8RdsZSVi5HJypwleciGHrJL1W2y0iai4PCvD
mg8Vcdo3M8x7zAO+AzcLCz02KYek5EnJkZIh5a+q3r5yDgYGrOzUlF7T5Hx83Cw3+D80tAYA+enp
nt+k7JKyPzJienV17yVTgsF1APD5/Xd1LS0OADxsbc3+GR42364Qi+nlK+Ta2gpeTs4w1LSur/0k
JUFKKhrVSwmeMhkDABgIOA2jo6sA0NDbu+416IzH75Surjk2NzsA8JjPG6XhMSk2KZeTk1EAsEMh
y5mf16qtlZ6YiCZI2SYFAPzeoa+pyQGAYDL5A8lk1bu8cj51xM/gjYHc32sfCapyzgcHLZuUo/b2
wuXSklpL/C8WU3cDgUKClF99fVYpgdLdHQcA9+JCe0ilzLPFxQqTnGGoT9ms6V5fawBQ39ERBwCf
t4nFjY1ZF4CvtdVRwuEVaWx0XBE8F4tacWdn+fnqSnMBKJHIav/m5lxFxJOxMdNb5QNS0qTskVKr
yhU4nZnR7VDIKjf48Jm+ihfxk8RykW8L+wAAAABJRU5ErkJggg==
于 2013-05-17T10:20:25.443 回答