I. Due to "multi-screen interaction", I am trying various solutions. II. The general idea of this method is:
1. Establish a socket connection based on the C/S mode;
2. on the server side, start the thread, take screenshots continuously, and write the image stream information to dataoutputstream;
3. The client keeps reading datainputstream, then bitmapfactory. decodebytearray, creates bitmap, and notifies handler of an attempt to update;
3. Disadvantages: screenshots show that the Read and Write processes of information flows are time-consuming. Iv. Source Code
1. Server Source Code (PC)
Server. Java:
Public class server {/*** write a program opened by the server * @ Param port: Port */Public void startserver (INT port) {try {// create a serverc object java.net. serversocket SC = new java.net. serversocket (port); system. out. println ("server created successfully"); // starts to continuously receive data while (true) {// creates a join object java.net. socket Client = SC. accept (); userthread ust = new userthread (client); ust. init (); ust. start () ;}} catch (exception ef) {EF. printstacktrace () ;}} public static void main (string ARGs []) {server Cr = new server (); CR. startserver (9090 );}}
Userthread. Java:
Public class userthread extends thread {// defines the constructor and passes in the clientprivate java.net. socket Client; private Java. io. inputstream INS; private Java. io. outputstream ous; Public userthread (java.net. socket Client) {This. client = client;} // initialize public void Init () {try {ins = client. getinputstream (); ous = client. getoutputstream ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}// write the run method public void run () {try {sendimg ();} catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public void sendimg () throws exception {dataoutputstream dous = new dataoutputstream (ous); robot RO = new robot (); dimension scrsize = toolkit. getdefatooltoolkit (). getscreensize (); rectangle r = new rectangle (scrsize. width, scrsize. height); system. out. println ("Size:" + scrsize. height + "" + scrsize. width); While (true) {bufferedimage Bui = Ro. createscreencapture (r); bytearrayoutputstream imagestream = new bytearrayoutputstream (); Boolean resultwrite = ImageIO. write (Bui, "jpg", imagestream); byte [] taginfo = imagestream. tobytearray (); // system. out. println (taginfo. length); // system. out. println (taginfo [0] + "" + taginfo [1]); // image IMG = toolkit. getdefatooltoolkit (). createimage (taginfo, 0, // taginfo. length); dous. writeint (taginfo. length + 5); dous. writebyte (byte) 4); dous. write (taginfo); // thread. sleep (100 );}}}
2. Client (Android)
Deskshowactivity. Java:
Public class extends showactivity extends activity {// defines various streamprivate Java. io. inputstream INS; private Java. io. outputstream ous; private Java. io. datainputstream dins; // define each component private imageview IMG; private textview TVW; // define a bitmap to store each image of imageview private bitmap BMM; // put the private byte [] data array of the received data; // initialize public void Init (string IP) {try {system. out. println (IP); java.net. socket SOC = new java.net. socket (IP, 9090 ); Ins = Soc. getinputstream (); dins = new datainputstream (INS); ous = Soc. getoutputstream (); system. Out. println ("created successfully! ");} Catch (exception ef) {EF. printstacktrace () ;}}// internal class, myhandlerclass myhandler extends handler {public myhandler () {} public myhandler (Looper loler) {super (Looper );} public void handlemessage (Message MSG) {IMG. setimagebitmap (Bitmap) MSG. OBJ) ;}// oncreatepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. mainfunctionactivity); // obtain the IMG = (im Ageview) findviewbyid (R. id. imgview); // TVW = (textview) findviewbyid (R. id. exittextview); // textview add listener // TVW. setonclicklistener (New onclicklistener () {// public void onclick (view v) {// system. exit (0); //}); Init ("192.168.1.179"); logoff = logoff. mylooper (); // you do not need to set Looper here, because handler uses the looper final myhandler = new myhandler (loler) of the current thread by default; new thread () {public void run () {While (true) {try {DATA = new byte [dins. readint ()-5]; dins. readbyte (); // note that readfully dins is used here. readfully (data); // Note: BMM needs to be recycled here; otherwise, an error is returned if (BMM! = NULL) {BMM. recycle ();} BMM = bitmapfactory. decodebytearray (data, 0, Data. length); system. out. println ("decode after:" + system. currenttimemillis (); // execute (adjustable) every one hundred Ms // thread. sleep (100); // create a message object and assign the obtained weather information to the message object message = message. obtain (); message. OBJ = (Bitmap) BMM; myhandler. sendmessage (Message);} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}}. start ();}}
V. Running effect: