在纯java

时间:2015-06-23 17:17:12

标签: java image base64

我需要将来自raspberry pi(用python编写)的图片发送到服务器(在java中)。我已经读过,最简单的方法是将图像编码为base64,然后在服务器端进行解码。 现在我从png生成了一个base64 txt文件。 我需要一个程序来解码它。

我有一个文件夹,其中我有我的base64代码(在一行txt文件中64kb)

编码程序,看起来像

import urllib
encoded = urllib.quote(open("/home/wojtek/Desktop/wideo/nazdjecia/test_image1.png", "rb").read().encode("base64"))
print encoded
f = open("path/obr.txt",'w')
f.write(encoded)
f.close()
print "done"

和看起来像这样的程序

public class JavaApplication2 {

    public static BufferedImage decodeToImage(String imageString)
    {
    BufferedImage image = null;
    byte[] imageByte;
    try
    {
        BASE64Decoder decoder = new BASE64Decoder();
        imageByte = decoder.decodeBuffer(imageString);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        image = ImageIO.read(bis);
        bis.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return image;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try {
            BufferedReader in = new BufferedReader(new FileReader("/filepath/obr.txt"));
            String str;
            str = in.readLine();

        NewJFrame gui = new NewJFrame();
        gui.setVisible(true);
        BufferedImage im = decodeToImage(str);
        ImageIcon ico = new ImageIcon(im);
        gui.imgset(ico );
            // str is one line of text; readLine() strips the newline character(s)
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        }

}

当我尝试用java解码它时崩溃

  

选择JAVA_TOOL_OPTIONS:-javaagent:/usr/share/java/jayatanaag.jar   javax.imageio.IIOException:跳过PNG元数据时出错   com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:680)     在   com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1229)     在   com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1577)     在javax.imageio.ImageIO.read(ImageIO.java:1448)at   javax.imageio.ImageIO.read(ImageIO.java:1352)at   javaapplication2.JavaApplication2.decodeToImage(JavaApplication2.java:33)     在javaapplication2.JavaApplication2.main(JavaApplication2.java:55)   引起:java.io.EOFException at   javax.imageio.stream.ImageInputStreamImpl.readInt(ImageInputStreamImpl.java:251)     在   com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:666)     ...还有6个java.lang.NullPointerException   javax.swing.ImageIcon。(ImageIcon.java:228)at   javaapplication2.JavaApplication2.main(JavaApplication2.java:56)

我做错了什么...... :(

0 个答案:

没有答案