title: Transforming of images in Java. Transferring of images through sockets
body: <p>I have a laptop and Microcontroller Evaluation Board MCB1700 with microcontroller NXP1768.
  MCB1700 has Ethernet port, LCD (320 x 240) etc. I have IDE µVision V4.03q for developing and downloading C-programs on MCB1700. Also I have ULINK-pro device for downloading C-programs on MCB1700.</p>
  
  <p>My task is: </p>
  
  <ol>
  <li>to implement Java- client for laptop that reads image from laptop, converts this image to RGB16 (LCD of MCB1700 supports such image representation) and sends it to MCB1700 through socket connection </li>
  <li>to implement server on MCB1700 that receives images from laptop through Socket-connection and displays it on LCD. </li>
  <li>Actually there is a lot of other tasks to do and laptop must also send to MCB1700 the name of command to perform. But the question is about connection between laptop and MCB1700.</li>
  </ol>
  
  <p>I have almost done the server side. My current version of server creates Passive socket connection and waits for client. It also can send and receive data through Ethernet.
  My Java-client connects to the server successfully. I tested it by sending and receiving of short lines. Now I can develop it for sending to the server the Command’s name (for example “SHOW ON LCD:”) and RGB16-image.</p>
  
  <p>The question is:
  What is the best way to send the name of Command and RGB16-image from Java-client to MCB1700-server (implemented on C) through socket-connection for convenient casting of data in server C-program? It would be great to send all data in form of byte-array: </p>
  
  <p>MCB1700-server must recieve data in form of bytes sequence</p>
  
  <pre><code>char* "SHOW ON LCD:" - bytes 0 - 11
  unsigned short**
  RGB16-Pixel[0][0] = 09 AE - bytes 12 - 13
  RGB16-Pixel[0][1] =  14 1C    -   bytes 14 - 15
  RGB16-Pixel[319][239] =  78 9F     -   bytes 76810 - 76811      
  </code></pre>
  
  <p>(Sorry for quality of this illustration)</p>
  
  <p>What is the best way to read an image of any format in Java-client and send it to MCB1700 in form of bytes- array (like on picture above)? What functions, classes should I use?
  Actually, I have some ideas:</p>
  
  <p>1)  I can put image in some way (I don’t know yet how) in Object of type  <code>BufferedImage</code>  (it supports needed format of image):</p>
  
  <pre><code>BufferedImage image =
      new BufferedImage(320, 240, TYPE_USHORT_565_RGB);
  </code></pre>
  
  <p>2)  Read every pixel of it in loop.</p>
  
  <pre><code>int oldRGB = image.getRGB(250, 180);
  </code></pre>
  
  <p>3)  And send each pixel of image sequentially byte after byte</p>
  
  <pre><code>Socket connectionToDevice;  //Socket
   OutputStream outToServerStream;
   outToServerStream = connectionToDevice.getOutputStream();
   outToServerStream.write(    bytes of pixel to send   );
  </code></pre>
  
  <p>Is there a more graceful way to perform this task? 
  How should I read and transform image in Java-client?</p>
  
  <p>4)  I have found such interesting way of image-transmission through socket using of such <code>ImageIO</code> method:
  <a href="https://stackoverflow.com/questions/6973848/sending-a-screenshot-bufferedimage-over-a-socket-in-java">Sending a screenshot (bufferedImage) over a socket in java</a></p>
  
  <pre><code>public static boolean write(RenderedImage im,
                              String formatName,
                              OutputStream output)
  
  
  BufferedImage image = ....;
  ImageIO.write(image, "PNG", socket.getOutputStream());
  </code></pre>
  
  <p>But I am not sure that it is suitable in my case. How does    <code>String formatName</code>  influence on stream? I haven’t any idea. May be it adds some extra bytes... </p>