投影仪电子化(E_Projector)

投影仪电子化(E_Projector)
问题的产生:软件公司组织内部培训,有时投影仪(资源)紧缺,培训时没有投影仪,在这种情况下每个人只能打开自己的电脑,边听边翻演讲者提供的PPT或者相关资料。这样可能会导致听众不能及时跟上演讲者的思路,以致培训失败。
需求:将投影仪电子化,大家的屏幕显示与演讲者一致。屏幕数20个左右。
设计:开发语言:java
        IDE:Eclipse
        开发思路:使用socket机制使得演讲者的机器和听众的机器进行通讯。 演讲者的机器作为服务端,听众的机器作为客户端,服务端每隔1秒截屏一次,客户端每个2秒去服务端取一次截屏图片数据并显示。
详细设计:
主程序mainapp.java:
 1  package  cn.com.lz.sharescreen;
 2  import  java.awt.EventQueue;
 3 
 4  import  javax.swing.JFrame;
 5  import  javax.swing.JButton;
 6  import  javax.swing.JSplitPane;
 7  import  javax.swing.JToolBar;
 8  import  javax.swing.JRadioButton;
 9  import  javax.swing.JTextField;
10  import  javax.swing.JLabel;
11  import  javax.swing.event.ChangeListener;
12  import  javax.swing.event.ChangeEvent;
13  import  javax.swing.JDesktopPane;
14  import  java.awt.event.ActionListener;
15  import  java.awt.event.ActionEvent;
16 
17 
18  public   class  mainapp {
19 
20       private  JFrame frmAgreecomm;
21       private  JTextField textField;
22 
23       /**
24       * Launch the application.
25        */
26       public   static   void  main(String[] args) {
27          EventQueue.invokeLater( new  Runnable() {
28               public   void  run() {
29                   try  {
30                      mainapp window  =   new  mainapp();
31                      window.frmAgreecomm.setVisible( true );
32                  }  catch  (Exception e) {
33                      e.printStackTrace();
34                  }
35              }
36          });
37      }
38 
39       /**
40       * Create the application.
41        */
42       public  mainapp() {
43          initialize();
44      }
45 
46       /**
47       * Initialize the contents of the frame.
48        */
49       private   void  initialize() {
50          frmAgreecomm  =   new  JFrame();
51          frmAgreecomm.setTitle( " AGREE_COMM " );
52          frmAgreecomm.setBounds( 100 100 561 245 );
53          frmAgreecomm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
54          frmAgreecomm.getContentPane().setLayout( null );
55          
56          JButton btnRun  =   new  JButton( " \u672C\u673A\u5145\u5F53\u670D\u52A1\u7AEF\uFF0C\u5176\u4ED6\u673A\u5668\u53EF\u4EE5\u8FDE\u63A5\u81F3\u672C\u673A " );
57          btnRun.addActionListener( new  ActionListener() {
58               public   void  actionPerformed(ActionEvent e) {
59                  ServerScreen frame  =   new  ServerScreen();
60                  frame.setVisible( true );
61              }
62          });
63          btnRun.setBounds( 75 45 410 23 );
64          frmAgreecomm.getContentPane().add(btnRun);
65          
66          textField  =   new  JTextField();
67          textField.setBounds( 150 116 124 21 );
68          frmAgreecomm.getContentPane().add(textField);
69          textField.setColumns( 10 );
70          
71          JLabel lblServerip  =   new  JLabel( " ServerIP\uFF1A " );
72          lblServerip.setBounds( 75 119 65 15 );
73          frmAgreecomm.getContentPane().add(lblServerip);
74          
75          JButton btnClose  =   new  JButton( " \u672C\u673A\u8FDE\u63A5\u81F3\u670D\u52A1\u7AEF " );
76          btnClose.addActionListener( new  ActionListener() {
77               public   void  actionPerformed(ActionEvent e) {
78                  ClientScreen frame  =   new  ClientScreen(textField.getText());
79      
80              }
81          });
82          btnClose.setBounds( 313 115 168 23 );
83          frmAgreecomm.getContentPane().add(btnClose);
84      }
85  }
86 
抓图程序:
package  cn.com.lz.sharescreen;

import  java.awt.Container;
import  java.awt.Dimension;
import  java.awt.Graphics2D;
import  java.awt.Image;
import  java.awt.MediaTracker;
import  java.awt.Rectangle;
import  java.awt.RenderingHints;
import  java.awt.Robot;
import  java.awt.Toolkit;
import  java.awt.image.BufferedImage;
import  java.io.BufferedOutputStream;
import  java.io.File;
import  java.io.FileOutputStream;
import  javax.imageio.ImageIO;
import  com.sun.image.codec.jpeg.JPEGCodec;
import  com.sun.image.codec.jpeg.JPEGEncodeParam;
import  com.sun.image.codec.jpeg.JPEGImageEncoder;



public   class  CaptureScreen  implements  Runnable{
    
public   static   void  main ( String [] args )  throws  InterruptedException{
    
// 测试生成缩略图
    createThumbnail( " c:\\555.gif " 200 200 100 " c:\\uuuu.jpg " );
    
// 测试捕捉屏幕,每隔一秒捕获一次
    String path  =   " c:\\screen\\ " ;
    
int  i  =   0 ;
    
for  ( ;; )
    {
        i
++ ;
        String filepath 
=  path  +  i  +   " .jpg " ;
        snatchScreen(filepath);
        Thread.currentThread().sleep(
2000 );
    }

}

/**
* 处理图片,改变起大小,可进行缩略和放大
@param  filename 源文件名称
@param  thumbWidth  处理后宽度
@param  thumbHeight 处理后高度
@param  quality     品质 最高100,最小0
@param  outFilename  输出文件名称
*/

    
public   static   void  createThumbnail ( String filename,  int  thumbWidth,  int  thumbHeight,  int  quality, String outFilename )
    {
        Image image 
=  Toolkit.getDefaultToolkit().getImage(filename);
        MediaTracker mediaTracker 
=   new  MediaTracker( new  Container());
        mediaTracker.addImage(image, 
0 );
        
try
        {
            mediaTracker.waitForID(
0 );
            
double  thumbRatio  =  ( double ) thumbWidth  /  ( double ) thumbHeight;
            
int  imageWidth  =  image.getWidth( null );
            
int  imageHeight  =  image.getHeight( null );
            
double  imageRatio  =  ( double ) imageWidth  /  ( double ) imageHeight;
            
if  ( thumbRatio  <  imageRatio )
            {
                thumbHeight 
=  ( int ) ( thumbWidth  /  imageRatio );
            }
            
else
            {
                thumbWidth 
=  ( int ) ( thumbHeight  *  imageRatio );
            }
            BufferedImage thumbImage 
=   new  BufferedImage(thumbWidth,
            thumbHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D 
=  thumbImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.drawImage(image, 
0 0 , thumbWidth, thumbHeight,  null );
            BufferedOutputStream out 
=   new  BufferedOutputStream( new  FileOutputStream(outFilename));
            JPEGImageEncoder encoder 
=  JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param 
=  encoder.getDefaultJPEGEncodeParam(thumbImage);
            quality 
=  Math.max( 0 , Math.min(quality,  100 ));
            param.setQuality((
float ) quality  /   100.0f false );
            encoder.setJPEGEncodeParam(param);
            encoder.encode(thumbImage);
            out.close();
        }
        
catch  ( Exception e )
        {
            e.printStackTrace();
        }
    }
    
    
/**
    * 抓取电脑屏幕
    * 
@param  path
    
*/
    
public   static   void  snatchScreen ( String path )
    {
        Robot robot;
        
try
        {
            robot 
=   new  Robot();
            Dimension d 
=  Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle rect 
=   new  Rectangle( 0 0 , d.width, d.height);
            BufferedImage image 
=  robot.createScreenCapture(rect);
            ImageIO.write(image, 
" jpg " new  File(path));
        }
        
catch  ( Exception e )
        {
            System.out.println(
" Failed to snatch screen   " );
            e.printStackTrace();
        }
    
    }

    @Override
    
public   void  run() {
        String path 
=   " c:\\screen\\ " ;
        String filepath 
=  path  +   " cs.jpg " ;
        
while ( true ){
            snatchScreen(filepath);
            
            
try  {
                Thread.currentThread().sleep(
2000 );
            } 
catch  (InterruptedException e) {
                
//  TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
    }

}


客户端程序:

package  cn.com.lz.sharescreen;

import  java.awt.BorderLayout;
import  java.awt.EventQueue;
import  java.awt.Graphics;
import  java.awt.Image;
import  java.awt.Toolkit;

import  javax.imageio.stream.MemoryCacheImageInputStream;
import  javax.imageio.stream.MemoryCacheImageOutputStream;
import  javax.swing.ImageIcon;
import  javax.swing.JFrame;
import  javax.swing.JPanel;
import  javax.swing.border.EmptyBorder;
import  javax.swing.JMenuBar;
import  javax.swing.border.LineBorder;
import  java.awt.Color;
import  java.io.DataInputStream;
import  java.io.DataOutputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.ObjectInputStream;
import  java.io.ObjectOutputStream;
import  java.io.OutputStream;
import  java.net.Socket;
import  java.net.UnknownHostException;
import  javax.swing.JButton;



public   class  ClientScreen  extends  JFrame  implements  Runnable{
    
private   String IP;
    Image ima
= Toolkit.getDefaultToolkit().getImage( " C:\\360buy1.jpg " );  

    
// ImageIcon ima = new ImageIcon("C:\\360buy1.jpg");
     public  ClientScreen(String IP) {
        setTitle(
" \u5BA2\u6237\u7AEF " );
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(
100 100 668 433 );
        
this .IP = IP;
        
this .setVisible( true );
        
        
new  Thread( this ).start();
    }
    
public   void  paint(Graphics g){
        
super .paint(g);
        g.drawLine(
10 10 100 100 );
        
        g.drawImage(ima, 
0 0 this );
    }
    
public   void  run(){
        
while ( true ){
            
try  {
                Thread.sleep(
2000 );
                 
            } 
catch  (InterruptedException e) {
                
//  TODO Auto-generated catch block
                e.printStackTrace();
            }
            MyClient mc
= new  MyClient();
            mc.start();
            
// System.out.print(111111111);
            repaint();
        }
            
    }
    
public   class  MyClient  extends  Thread{   
        Socket socket 
=   null ;   
        
public   void  run(){
            
try  {
                socket 
=   new  Socket(IP,  1111 );            
                DataOutputStream doc 
=   new  DataOutputStream(socket.getOutputStream());   
                ObjectInputStream in 
=   new  ObjectInputStream(socket.getInputStream());   
                doc.writeUTF(
" list11111 " ); 
                doc.flush();
                
long  len  =  in.readLong();
                System.out.print(len);
                FileInputStream fis
=   new  FileInputStream((File)in.readObject());
                
byte [] b = new   byte [( int ) len];
                fis.read(b);
                ima
= Toolkit.getDefaultToolkit().createImage(b);
                fis.close();
                doc.close();
                in.close();
                doc.close();   
                in.close();   
                socket.close();   
            } 
catch  (UnknownHostException e) {
                e.printStackTrace();
            } 
catch  (IOException e) {
                e.printStackTrace();
            } 
catch  (ClassNotFoundException e) {
                
//  TODO Auto-generated catch block
                e.printStackTrace();
            } 
finally  {
                
            }   
        }
    } 
}
服务端程序:

package  cn.com.lz.sharescreen;

import  java.awt.BorderLayout;
import  java.awt.EventQueue;
import  java.awt.Image;
import  java.awt.Toolkit;
import  java.io.BufferedInputStream;
import  java.io.DataInputStream;
import  java.io.DataOutputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.IOException;
import  java.io.ObjectOutputStream;
import  java.net.ServerSocket;
import  java.net.Socket;

import  javax.swing.JFrame;
import  javax.swing.JPanel;
import  javax.swing.border.EmptyBorder;
import  javax.swing.JEditorPane;


public   class  ServerScreen  extends  JFrame {

    
private  JPanel contentPane;
    
public  JEditorPane editorPane;

    
public  ServerScreen() {
        setTitle(
" Server " );
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(
100 100 399 446 );
        contentPane 
=   new  JPanel();
        contentPane.setBorder(
new  EmptyBorder( 5 5 5 5 ));
        contentPane.setLayout(
new  BorderLayout( 0 0 ));
        setContentPane(contentPane);
        
        editorPane 
=   new  JEditorPane();
        contentPane.add(editorPane, BorderLayout.CENTER);
        MyServer myserver
= new  MyServer(); 
        myserver.start();
        
new  Thread( new  CaptureScreen()).start();
    }
    
public   class  MyServer  extends  Thread {
        
public  MyServer(){
            
super ();
            editorPane.setText(
" Welcome to use Agree_Comm " + " \n " );
        }
        
public   void  run(){
            doListen();
            
        }
        
public   void  doListen() {   
            ServerSocket server;   
            
try  {   
                server 
=   new  ServerSocket( 1111 );   
                
while  ( true ) {   
                    Socket client 
=  server.accept();   
                    
new  Thread( new  SSocket(client)).start();   
                }   
            } 
catch  (IOException e) {   
                e.printStackTrace();   
            }   
        }   
      
        
// 服务器进程   
         class  SSocket  implements  Runnable {   
      
            Socket client;   
      
            
public  SSocket(Socket client) {   
                
this .client  =  client;   
            }   
      
            
public   void  run() {   
                DataInputStream input;   
                ObjectOutputStream output; 

                
try  {   
                    File img
= new  File( " C:\\screen\\cs.jpg " );
                    
                    input 
=   new  DataInputStream(client.getInputStream());   
                    output 
=   new  ObjectOutputStream(client.getOutputStream());   
                    
//    
                    String listMsg  =  input.readUTF();  
                    Toolkit t
= getToolkit();
         
                    
long  fis_len = img.length();
                    System.out.print(
" fis_len: " + fis_len);
                    output.writeLong(fis_len);
                    output.flush();
                    
                    editorPane.setText(editorPane.getText()
+ " \n " + " Recive:    "   +  listMsg);
                    
// System.out.println("Recive:   " + listMsg);   
                    
// listMsg = input.readUTF();   
                    
// output.writeUTF("Recive Second:  " + listMsg + "    \r\n Thx");  
                    
// editorPane.setText(editorPane.getText()+"\n"+"Recive:   " + listMsg);
                    
// System.out.println("Recive Second:   " + listMsg); 
                    
                    output.writeObject(img);
                    output.flush();
                    input.close();
                    output.close();
                    client.close();
                } 
catch  (IOException e) {   
                    e.printStackTrace();   
                } 
                
finally {
                    
                    
                }

            }   
        }   


    }
}






聚沙成洲,奋发超越。

你可能感兴趣的:(投影仪电子化(E_Projector))