Thomas Darimont
Erfahrenes Mitglied
Servus!
Hier ein Beispiel, wie man ein Bild über das Netzwerk versenden kann ...:
Gruß Tom
Hier ein Beispiel, wie man ein Bild über das Netzwerk versenden kann ...:
Code:
//The Server
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.*;
/*
* TheServer.java
*
* Created on 29. Juli 2003, 11:19
*/
/**
*
* @author Darimont
*/
public class TheServer extends javax.swing.JFrame {
private ServerSocket ss = null;
private Socket s = null;
private Thread serverThread = null;
private BufferedImage bimg = null;
private int[] pixarray = null;
private PixelGrabber pg = null;
/** Creates new form TheServer */
public TheServer() {
initComponents();
loadImage();
}
private void startServer(){
if(serverThread == null){
serverThread = new Thread(new myServer());
serverThread.start();
}
}
private void loadImage(){
MediaTracker mt = new MediaTracker(this);
try{
bimg = javax.imageio.ImageIO.read(new File("c:/Beispiel1.jpg"));
}catch(IOException ioe){
ioe.printStackTrace();
}
mt.addImage(bimg,0);
try{
mt.waitForAll();
}catch(InterruptedException ie){
ie.printStackTrace();
}
mt = null;
//Imagepixels to Array -->
//My Imagesize is 100 x 100 Pixels so there are 10000 Pixels
pixarray = new int[10000];
pg = new PixelGrabber((Image)bimg,0,0,bimg.getWidth(),bimg.getHeight(),pixarray,0, 100);
try{
pg.grabPixels();
}catch(InterruptedException ie){ie.printStackTrace();}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
getContentPane().setLayout(new java.awt.GridBagLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jPanel1.setMinimumSize(new java.awt.Dimension(400, 300));
jPanel1.setPreferredSize(new java.awt.Dimension(400, 300));
getContentPane().add(jPanel1, new java.awt.GridBagConstraints());
jPanel2.setMinimumSize(new java.awt.Dimension(400, 50));
jPanel2.setPreferredSize(new java.awt.Dimension(400, 50));
jButton2.setText("Start Server");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton2MousePressed(evt);
}
});
jPanel2.add(jButton2);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
getContentPane().add(jPanel2, gridBagConstraints);
pack();
}
private void jButton2MousePressed(java.awt.event.MouseEvent evt) {
// Add your handling code here:
startServer();
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public void paint(Graphics g){
super.paint(g);
if(bimg !=null)
jPanel1.getGraphics().drawImage(bimg.getScaledInstance(bimg.getWidth(),bimg.getHeight(),Image.SCALE_FAST),0,0,bimg.getWidth(),bimg.getHeight(),this);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TheServer().show();
}
class myServer implements Runnable{
ObjectOutputStream oos = null;
public void run() {
try{
ss = new ServerSocket(8888);
System.out.println("Server started!");
while((s=ss.accept())==null)
Thread.currentThread().sleep(100);
System.out.println("Connected");
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
oos = new ObjectOutputStream(bos);
oos.writeObject(pixarray);
oos.close();
s.close();
s= null;
ss.close();
ss = null;
System.out.println("Connection closed");
}catch(IOException ioe){ ioe.printStackTrace();
}catch(InterruptedException ie) { ie.printStackTrace();
} //catch(SocketException se){ se.printStackTrace(); }
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration
}
// The Client
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
/*
* TheClient.java
*
* Created on 29. Juli 2003, 12:08
*/
/**
*
* @author Darimont
*/
public class TheClient extends javax.swing.JFrame {
private Image img = null;
private Socket s = null;
private int[] imga = null;
private Thread clientThread = null;
/** Creates new form TheClient */
public TheClient() {
initComponents();
}
public void start(){
if(clientThread ==null){
clientThread = new Thread(new theClient());
clientThread.start();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
getContentPane().setLayout(new java.awt.GridBagLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jPanel1.setMinimumSize(new java.awt.Dimension(400, 250));
jPanel1.setPreferredSize(new java.awt.Dimension(400, 250));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
getContentPane().add(jPanel1, gridBagConstraints);
jPanel2.setMinimumSize(new java.awt.Dimension(400, 50));
jPanel2.setPreferredSize(new java.awt.Dimension(400, 50));
jButton1.setText("Get Image");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton1MousePressed(evt);
}
});
jPanel2.add(jButton1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
getContentPane().add(jPanel2, gridBagConstraints);
pack();
}
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
// Add your handling code here:
start();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
}
public void paint(Graphics g){
super.paint(g);
if(img!=null)
jPanel1.getGraphics().drawImage(img,0,0,img.getWidth(null),img.getHeight(null),this);
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TheClient().show();
}
private void resampleImage(int[] imageArray){
img = createImage(new MemoryImageSource(100,100,imageArray,0,100));
}
class theClient implements Runnable{
ObjectInputStream ois = null;
BufferedInputStream bis = null;
public void run() {
try{
s = new Socket("localhost",8888);
if(s != null){
bis = new BufferedInputStream(s.getInputStream());
ois = new ObjectInputStream(bis);
imga = (int[])ois.readObject();
if (imga != null){
resampleImage(imga);
jPanel1.getGraphics().drawImage(img,0,0,img.getWidth(null),img.getHeight(null),jPanel1);
}
}
}catch(IOException ioe){ ioe.printStackTrace();
}catch(ClassNotFoundException cnfe){ cnfe.printStackTrace();
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration
}
Gruß Tom