i have implemented code below. server receives image client , matlab detects object , save new image face.jpg trying send points client code stucks in ////////// part of client code:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// thread.sleep(15000); log.d("mytag","test message11"); try { points = infromserver.readline(); }catch(exception io){ log.d("mytag","fails read server"); } log.d("mytag",points); sock.close(); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
the whole android client code here.any idea guys? please help.
public class myactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my); } int take_photo_code = 0; public static int count=0; public static string message="sent"; public void button2onclick(final view k){ //connecting server , send picture new thread(new runnable(){ public void run(){ try { int i; fileinputstream fis = new fileinputstream (environment.getexternalstoragepublicdirectory(environment.directory_pictures) + "/picfolder/1.jpg"); socket sock = new socket("192.168.1.91", 6789); dataoutputstream os = new dataoutputstream(sock.getoutputstream()); bufferedreader infromserver = new bufferedreader(new inputstreamreader(sock.getinputstream())); byte[] buffer = new byte[1024]; int bytesread; while ((i = fis.read()) > -1) os.write(i); fis.close(); os.close(); //lhyh apotelesmatos apo server string points; points=""; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// thread.sleep(15000); log.d("mytag","test message11"); try { points = infromserver.readline(); }catch(exception io){ log.d("mytag","fails read server"); } log.d("mytag",points); sock.close(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// file filepath2 = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures) + "/picfolder/1.jpg"); filepath2.delete(); //file filepath2 = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures) + "/picfolder"); //filepath2.delete(); }catch (exception io){ message="not sent"; file filepath = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures) + "/picfolder/1.jpg"); filepath.delete(); file filepath2 = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures) + "/picfolder"); filepath2.delete(); log.d("mytag",message); } } }).start(); button button=(button) k; ((button) k).settext(message); } public void buttononclick(view v){ //here,we making folder named picfolder store pics taken camera using application final string dir = environment.getexternalstoragepublicdirectory(environment.directory_pictures) + "/picfolder/"; file newdir = new file(dir); newdir.mkdirs(); button capture = (button) findviewbyid(r.id.button); capture.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // here,counter incremented each time,and picture taken camera stored 1.jpg,2.jpg , likewise. count++; string file = dir+"1.jpg"; file newfile = new file(file); try { newfile.createnewfile(); } catch (ioexception e) {} uri outputfileuri = uri.fromfile(newfile); intent cameraintent = new intent(mediastore.action_image_capture); cameraintent.putextra(mediastore.extra_output, outputfileuri); startactivityforresult(cameraintent, take_photo_code); } }); } @targetapi(11) protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == take_photo_code && resultcode == result_ok) { log.d("camerademo", "pic saved"); // afou travhksei thn kainouria eikona provaletai sto imageview bitmap bmp = bitmapfactory.decodefile(environment.getexternalstoragepublicdirectory(environment.directory_pictures) + "/picfolder/1.jpg"); imageview image = (imageview) findviewbyid(r.id.imageview2); image.setimagebitmap(bmp); //mage.setrotation(270); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_my, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
}
i sending server code public class imagereceiver {
public static void main(string[] args) { try{ serversocket socket = new serversocket(6789); system.out.println("server on-----waiting image"); while(true) { ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // receive image client // ///////////////////////////////////////////////////////////////////////////////////////////////////////////// socket clientsocket = socket.accept(); file filepath = new file("c:\\imagesreceived\\image.jpg"); filepath.delete(); datainputstream dis = new datainputstream(clientsocket.getinputstream()); fileoutputstream fout = new fileoutputstream("c:\\imagesreceived\\image.jpg"); try{ int i; while ( (i = dis.read()) > -1) { fout.write(i); } system.out.println("image received"); }catch(exception fromclient ){ system.out.println("image wasn't received"); } fout.flush(); fout.close(); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // open matlab connection in order make face detection //// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// try{ matlabproxyfactoryoptions options = new matlabproxyfactoryoptions.builder() .setusepreviouslycontrolledsession(true) .build(); matlabproxyfactory factory = new matlabproxyfactory(options); matlabproxy proxy = factory.getproxy(); proxy.eval("addpath('c:\\users\\theojim')"); proxy.eval("run('firsttry.m')"); proxy.eval("rmpath('c:\\users\\theojim')"); system.out.println("matlab done"); // close connection proxy.disconnect(); } catch(exception matlab){ system.out.println("matlab error"); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // epistrofh shmeiwn proswpou apo text file //// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// try{ string scurrentline; bufferedreader br = null; br = new bufferedreader(new filereader("c:\\imagesreceived\\facepoints.txt")); string points=""; while ((scurrentline = br.readline()) != null) { points+=scurrentline; } system.out.println(points); dataoutputstream outtoclient = new dataoutputstream(clientsocket.getoutputstream()); outtoclient.writebytes(points); system.out.println("points sent android"); }catch(exception points){ system.out.println("points failed sent client"); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // epistrofh eikonas face detected //// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //try{ // int j; // fileinputstream file = new fileinputstream("c:\\imagesreceived\\face.jpg"); //dataoutputstream stream = new dataoutputstream(clientsocket.getoutputstream()); // while ((j = file.read()) > -1) { // system.out.println("while starts"+j); // stream.write(j); //stream.flush(); //system.out.println("while endss"); //} //system.out.println("while exits"); //file.close(); //stream.close(); dis.close(); //} //catch(exception imageface){ //system.out.println("face.jpeg failed sent client"); //} clientsocket.close(); } }catch(exception io){ system.out.println("error"); } }
}
and sending last part of logat file see happens. please help!!!!!
05-26 22:05:59.715 16000-16491/com.example.theojim.myapplication d/mytag﹕ test message11
05-26 22:05:59.716 16000-16491/com.example.theojim.myapplication d/mytag﹕ fails read server
the server reads until end of stream. client need close socket deliver end of stream.
Comments
Post a Comment