在通过imageIcon将字符串数据转换为Image时,高度和宽度在java2EE中变为-1

时间:2013-05-17 10:08:42

标签: image java-ee base64 imageicon

通过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

1 个答案:

答案 0 :(得分: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==