簡體   English   中英

Java圖像發送網絡

[英]Java Image Sending Network

我正在嘗試通過以下服務器代碼通過網絡發送圖像。

 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指針異常。

運行此命令時,我在服務器端收到NullPointerException。 getPhoto ImageIO.read返回null ,因為我沒有注冊能夠讀取流的ImageReader

您的類路徑中的任何ImageInputStreamSpi都將自動注冊,所以我想我沒有。 我不知道您的計算機上安裝了什么,但這可能是類似的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM