Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. package Com.Company;
  2.  
  3. /**
  4. * Created by Thomas on 18-11-2015.
  5. */
  6. import javax.imageio.ImageIO;
  7. import javax.swing.*;
  8. import java.awt.image.BufferedImage;
  9. import java.io.*;
  10. import java.net.Socket;
  11. import java.nio.ByteBuffer;
  12.  
  13. public class EchoClient {
  14. private String hostName;
  15. private int portNumber;
  16.  
  17.  
  18. public static void main(String[] args) throws IOException {
  19. new EchoClient();
  20. }
  21.  
  22. public EchoClient() {
  23. this.hostName = "localhost";
  24. this.portNumber = 1234;
  25. try {
  26. client(new Socket(hostName, portNumber));
  27. }
  28. catch (IOException e) {
  29. System.err.println(e.getMessage());
  30. }
  31. }
  32.  
  33. public void client(Socket s) {
  34. OutputStream out;
  35. InputStream in = null;
  36. try {
  37. //Getting IO's
  38. out = s.getOutputStream();
  39. in = s.getInputStream();
  40. System.out.println("Got image IO connection to server..");
  41.  
  42. //Getting image:
  43. JFileChooser jfc = new JFileChooser();
  44. jfc.setDialogTitle("Select image file..");
  45. int action = jfc.showOpenDialog(null);
  46. if(action != JFileChooser.APPROVE_OPTION)
  47. System.exit(0); // canceled by user
  48.  
  49. File f = jfc.getSelectedFile();
  50.  
  51. //Feedback
  52. System.out.println("Got file " + f.getName());
  53.  
  54. //Storing image as bufferedimage
  55. BufferedImage bi = ImageIO.read(f);
  56.  
  57. //Creating byte array that represent the image
  58. ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
  59. ImageIO.write(bi, "jpg", byteOut);
  60.  
  61. byte[] size = ByteBuffer.allocate(4).putInt(byteOut.size()).array();
  62. System.out.println(byteOut.size());
  63. System.out.println("Sending image to server.");
  64. //Sends the image
  65. out.write(size);
  66. out.write(byteOut.toByteArray());
  67. out.flush();
  68.  
  69. System.out.println("Waiting for converted image.");
  70. //Reading the bytes from server
  71. byte[] sizeAr = new byte[4];
  72. in.read(sizeAr);
  73. int cSize = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
  74.  
  75. byte[]imageAr = new byte[cSize];
  76. int sizerecv = 0;
  77. int sizerecv2 = 0;
  78.  
  79. byte[] imageAr2 = new byte[cSize];
  80. int placement = 0;
  81. while(sizerecv < cSize){
  82. sizerecv2 = in.read(imageAr);
  83. for(int i = 0; i < sizerecv2; i++)
  84. {
  85. imageAr2[i + placement] = imageAr[i];
  86.  
  87. }
  88. placement = placement + sizerecv2;
  89. System.out.println(imageAr[1]);
  90. System.out.println(imageAr2[1]);
  91. sizerecv = sizerecv + sizerecv2;
  92. }
  93. //in.read(imageAr);
  94. //int sizerecv = 0;
  95. //while(sizerecv == in.read(imageAr));
  96. //in.read(imageAr);
  97. System.out.println("Got converted image data from server, starting conversion to image.");
  98.  
  99. //Converting bytes to the image
  100. BufferedImage convertedBI = null;
  101. try {
  102. convertedBI = ImageIO.read(new ByteArrayInputStream(imageAr2));
  103. }
  104. catch(EOFException e) {
  105. System.out.println("Finished reading the image.");
  106. }
  107. System.out.println("Writing file to output.jpg.");
  108.  
  109. //Saving the converted image
  110. ImageIO.write(convertedBI, "jpg", new File("output.jpg"));
  111. }
  112. catch (IOException e) {
  113. e.printStackTrace();
  114. }
  115. finally {
  116. try {
  117. in.close();
  118. }
  119. catch (IOException e) {
  120. e.printStackTrace();
  121. }
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement