java实现从服务器取图片
时间:2005-10-21 来源:ilcj
Java实现的从服务器取图片
1.SerializableImage.java(图象序列化)
package test;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
*
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SerializableImage implements Serializable
{
protected int[] rgbs;
protected int width,height;
transient BufferedImage bimg;
public Image getImage()
{
if(bimg==null)
{
bimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
bimg.setRGB(0,0,width,height,rgbs,0,width);
}
return bimg;
}
public SerializableImage(Image img)
{
JFrame comp=new JFrame();
comp.addNotify();
MediaTracker mt=new MediaTracker(comp);
mt.addImage(img,0);
try
{
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
width=img.getWidth(comp);
height=img.getHeight(comp);
rgbs=new int[width*height];
BufferedImage timg=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics g=timg.createGraphics();
g.drawImage(img,0,0,null);
g.dispose();
timg.getRGB(0,0,width,height,rgbs,0,width);
}
public SerializableImage(InputStream in)
{
BufferedImage timg=null;
try
{
timg = ImageIO.read(in);
}
catch (IOException e)
{
e.printStackTrace();
return;
}
width=timg.getWidth();
height=timg.getHeight();
rgbs=new int[width*height];
timg.getRGB(0,0,width,height,rgbs,0,width);
}
public SerializableImage()
{
}
}
2.ImageServer.java
package test;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ImageServer
{
boolean stoped;
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void start()
{
ServerSocket ss=null;
try
{
ss=new ServerSocket(9090);
}
catch(Exception e)
{
e.printStackTrace();
stoped=true;
}
System.out.println("Server Started!");
while(!stoped)
{
Socket client=null;
try
{
client=ss.accept();
new PClient(client).start();
}
catch(Exception e)
{
continue;
}
}
System.out.println("Server Stoped!");
}
class PClient extends Thread
{
Socket s;
public PClient(Socket so)
{
s=so;
}
public void run()
{
try
{
//this also work!
//Image img=Toolkit.getDefaultToolkit().getImage(getClass().getResource("picture.jpg"));
// SerializableImage simg=new SerializableImage(img);
SerializableImage simg=new SerializableImage(getClass().getResourceAsStream("1.jpg"));
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
oos.writeObject(simg);
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
protected void stop()
{
stoped=true;
}
public static void main(String[] args)
{
final ImageServer is=new ImageServer();
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
is.stop();
}
});
is.start();
}
}
3.TestImage.java
package test;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ObjectInputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
*
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestImage extends JFrame
{
private Canvas canvas;
private JButton ok;
public TestImage ()
{
super("Test Image");
canvas=new Canvas();
ok=new JButton("Got Image From Server!");
ok.addActionListener(new MAL());
Container con=getContentPane();
con.add(canvas,BorderLayout.CENTER);
con.add(ok,BorderLayout.SOUTH);
}
public static void main(String[] args)
{
final TestImage frame=new TestImage();
frame.setSize(500,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MAL implements ActionListener
{
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
Graphics g=canvas.getGraphics();
int w=canvas.getWidth();
int h=canvas.getHeight();
g.setColor(Color.BLACK);
g.fillRect(0,0,w,h);
String msg="wait...";
Font f=g.getFont();
f=f.deriveFont(Font.BOLD,44);
g.setColor(Color.RED);
g.setFont(f);
g.drawString(msg,(w/2-25),h/2);
g.dispose();
Thread t=new Thread()
{
public void run()
{
Socket so=null;
try
{
so=new Socket("localhost",9090);
ObjectInputStream oin=new ObjectInputStream(so.getInputStream());
SerializableImage sim=(SerializableImage)oin.readObject();
so.close();
Graphics g=canvas.getGraphics();
int w=canvas.getWidth();
int h=canvas.getHeight();
g.setColor(Color.BLACK);
g.fillRect(0,0,w,h);
g.drawImage(sim.getImage(),0,0,null);
g.dispose();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
t.start();
}
}
}
1.SerializableImage.java(图象序列化)
package test;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
*
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SerializableImage implements Serializable
{
protected int[] rgbs;
protected int width,height;
transient BufferedImage bimg;
public Image getImage()
{
if(bimg==null)
{
bimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
bimg.setRGB(0,0,width,height,rgbs,0,width);
}
return bimg;
}
public SerializableImage(Image img)
{
JFrame comp=new JFrame();
comp.addNotify();
MediaTracker mt=new MediaTracker(comp);
mt.addImage(img,0);
try
{
mt.waitForID(0);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
width=img.getWidth(comp);
height=img.getHeight(comp);
rgbs=new int[width*height];
BufferedImage timg=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics g=timg.createGraphics();
g.drawImage(img,0,0,null);
g.dispose();
timg.getRGB(0,0,width,height,rgbs,0,width);
}
public SerializableImage(InputStream in)
{
BufferedImage timg=null;
try
{
timg = ImageIO.read(in);
}
catch (IOException e)
{
e.printStackTrace();
return;
}
width=timg.getWidth();
height=timg.getHeight();
rgbs=new int[width*height];
timg.getRGB(0,0,width,height,rgbs,0,width);
}
public SerializableImage()
{
}
}
2.ImageServer.java
package test;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ImageServer
{
boolean stoped;
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void start()
{
ServerSocket ss=null;
try
{
ss=new ServerSocket(9090);
}
catch(Exception e)
{
e.printStackTrace();
stoped=true;
}
System.out.println("Server Started!");
while(!stoped)
{
Socket client=null;
try
{
client=ss.accept();
new PClient(client).start();
}
catch(Exception e)
{
continue;
}
}
System.out.println("Server Stoped!");
}
class PClient extends Thread
{
Socket s;
public PClient(Socket so)
{
s=so;
}
public void run()
{
try
{
//this also work!
//Image img=Toolkit.getDefaultToolkit().getImage(getClass().getResource("picture.jpg"));
// SerializableImage simg=new SerializableImage(img);
SerializableImage simg=new SerializableImage(getClass().getResourceAsStream("1.jpg"));
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
oos.writeObject(simg);
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
protected void stop()
{
stoped=true;
}
public static void main(String[] args)
{
final ImageServer is=new ImageServer();
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
is.stop();
}
});
is.start();
}
}
3.TestImage.java
package test;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ObjectInputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
*
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestImage extends JFrame
{
private Canvas canvas;
private JButton ok;
public TestImage ()
{
super("Test Image");
canvas=new Canvas();
ok=new JButton("Got Image From Server!");
ok.addActionListener(new MAL());
Container con=getContentPane();
con.add(canvas,BorderLayout.CENTER);
con.add(ok,BorderLayout.SOUTH);
}
public static void main(String[] args)
{
final TestImage frame=new TestImage();
frame.setSize(500,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MAL implements ActionListener
{
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
Graphics g=canvas.getGraphics();
int w=canvas.getWidth();
int h=canvas.getHeight();
g.setColor(Color.BLACK);
g.fillRect(0,0,w,h);
String msg="wait...";
Font f=g.getFont();
f=f.deriveFont(Font.BOLD,44);
g.setColor(Color.RED);
g.setFont(f);
g.drawString(msg,(w/2-25),h/2);
g.dispose();
Thread t=new Thread()
{
public void run()
{
Socket so=null;
try
{
so=new Socket("localhost",9090);
ObjectInputStream oin=new ObjectInputStream(so.getInputStream());
SerializableImage sim=(SerializableImage)oin.readObject();
so.close();
Graphics g=canvas.getGraphics();
int w=canvas.getWidth();
int h=canvas.getHeight();
g.setColor(Color.BLACK);
g.fillRect(0,0,w,h);
g.drawImage(sim.getImage(),0,0,null);
g.dispose();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
t.start();
}
}
}
相关阅读 更多 +