サーバーコードに従ってネットワーク経由でイメージを送信しようとしています。クライアントコードはクライアント側のJava NULLポインタ例外を私に与えているJavaイメージ送信ネットワーク
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
}
}
次
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
}
}
と。
ありがとうございました
可能な限りお手伝いください。私は仕事をしていたし、このサイトをたくさん探していたが、私はJavaの初心者だったので、本当に有用なものは何も見つかりませんでした。
コメントで出力ストリームを使用して申し訳ありません。それが働いているかどうかをチェックしていただけです。とにかく、できるだけ早く書き込んでください。ありがとう –