【问题标题】:Java Image Sending NetworkJava 图像发送网络
【发布时间】:2012-02-26 06:06:59
【问题描述】:

我正在尝试按照服务器代码通过网络发送图像。

 import java.net.*;
 import java.awt.BorderLayout;
 import java.awt.Image;
 import java.awt.image.BufferedImage;
 import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Server {
    static private BufferedImage bi;
    static Socket socket;
    static ServerSocket serverSocket;
    DataInputStream dis=null;

    public static void connect() throws IOException
    {
        serverSocket = new ServerSocket(9632);
        System.out.println("i am server & listening...");
        socket = serverSocket.accept();
        System.out.println("Connected");

    }
  public static void main (String [] args ) throws IOException {

      connect();
      receiveimage();
      showimage();



      }
 public static void receiveimage()
 {
     byte[] data;
     while (true){
         try{
     System.out.println("Reading Image");
     InputStream in=socket.getInputStream();

     data=new byte[socket.getReceiveBufferSize()];

     in.read(data, 0, socket.getReceiveBufferSize());

     Image image = getPhoto(data);
     bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
     File outputfile = new File("saved.png");
     ImageIO.write(bi, "png", outputfile);

 }
         catch(Exception ex)
            {
                System.out.println("error: "  + ex.toString());
            }

     }
 }
 public static void showimage() throws IOException
 {
     File file = new File("saved.png");
      Image  image = ImageIO.read(file);
     JFrame frame = new JFrame();
     JLabel label = new JLabel(new ImageIcon(image));
     frame.getContentPane().add(label, BorderLayout.CENTER);
     frame.pack();
     frame.setVisible(true);
 }

  public static Image getPhoto(byte[] bytePhoto) throws IOException {  
      return ImageIO.read(new ByteArrayInputStream(bytePhoto)); //bytePhoto is the byte array  
} 
    }

以及下面的客户端代码

import java.net.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;

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

public class client{
      public static byte[] bytePhoto;
      public static Socket sock;
      public static void connect() throws IOException
        {
            Socket sock = new Socket("172.16.0.143",9632);
            System.out.println("Connected");

        }



  public static void main (String [] args ) throws IOException, ClassNotFoundException {
     connect();
    sendphoto();

  }

  public static void sendphoto() throws IOException
  {
            InputStream is = new BufferedInputStream(new FileInputStream("c:\\ziki.png"));
            Image image = ImageIO.read(is);
            byte[] data=setPhoto(image);
           // OutputStream output = sock.getOutputStream();
            //output.write(data);

  }
  public static byte[] setPhoto(Image img) throws IOException {  
      ImageIcon icon = new ImageIcon(img);  
      BufferedImage bImg = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),     BufferedImage.TYPE_INT_RGB);  
      Graphics g = bImg.getGraphics();  
      g.drawImage(img, 0, 0, null);  
      g.dispose();  
      ByteArrayOutputStream writer = new ByteArrayOutputStream();  
      ImageIO.write(bImg, "jpg", writer);  
      return bytePhoto = writer.toByteArray();  //bytePhoto is a byte array  
} 
}

它在客户端给我 Java Null 指针异常。

【问题讨论】:

  • 很抱歉在 cmets 中使用了输出流。它只是在检查它是否工作。无论如何,请尽快写。谢谢

标签: java image networking


【解决方案1】:

当我运行它时,我在服务器端收到 NullPointerException。 getPhoto 中的 ImageIO.read 正在返回 null,因为我没有注册能够读取流的 ImageReader

类路径中的任何ImageInputStreamSpis 都会自动注册,所以我想我没有。我不知道你的机器上安装了什么,但它可能是一个类似的问题。

【讨论】:

  • 感谢泰蒙的回答。我正在研究这个,我对java真的很陌生。我只想将图像从一台服务器传输到客户端,但我无法完成。我不知道有什么问题。我做了一切,尝试了每个博客。 :(
猜你喜欢
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 2013-08-06
  • 1970-01-01
相关资源
最近更新 更多