0

I have a client application on Android and server on C#, they communicate through a socket. On request server send an image converted to byte[], but how to decode it again on my device into image?

I've already found this code:

Bitmap bmp=BitmapFactory.decodeByteArray(b,0,b.length);                     
image.setImageBitmap(bmp);

And here's part of client's code:

private void connectSocket(String a){ 

    try { 
        InetAddress serverAddr = InetAddress.getByName("192.168.1.2"); 
        Socket socket = new Socket(serverAddr, 4444); 

        BufferedReader in = null;

        try { 
            out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

            out.println(message);

            String text = "";

            image = (ImageView)findViewById(R.id.imageView1);
            while ((text = in.readLine()) != null) {
                byte[] b = in.readLine().getBytes();
                Bitmap bmp=BitmapFactory.decodeByteArray(b,0,b.length);                     
                image.setImageBitmap(bmp);
            }

        } catch(Exception e) { 
            Log.e("TCP", "S: Error", e); 
        } finally { 
            socket.close(); 
        } 

    } catch (UnknownHostException e) { 
        Log.e("TCP", "C: UnknownHostException", e); 
        e.printStackTrace(); 
    } catch (IOException e) {  
        Log.e("TCP", "C: IOException", e); 
        e.printStackTrace(); 
    }       
} 

So how to get the exact recieved bytes to convert them in an image?

3Gee
  • 1,094
  • 2
  • 14
  • 23

2 Answers2

2

Readers are for text. For the bytes in an image, you probably don't want to use readers at all, but should be working from the input stream.

For help doing this see the answers to this SO question.

To convert a byte array to an image, see this link.

Community
  • 1
  • 1
Don Roby
  • 40,677
  • 6
  • 91
  • 113
  • @3Gee I am looking for a similar solution. Can you point me to your solution? – zeeshan Sep 09 '16 at 19:48
  • @zeeshan It was very long ago, I've done some digging in that old code, I believe this was the solution: `byte[] buffer = new byte[16200]; // extract image from image array int size = is.read(buffer); byte[] buffer2 = new byte[size]; for(int g = 0; g < size; g++){ buffer2[g] = buffer[g]; } image = (ImageView) findViewById(R.id.imageView1); Bitmap bmp = BitmapFactory.decodeByteArray(buffer2, 0, buffer2.length); image.setImageBitmap(bmp);` – 3Gee Sep 21 '16 at 11:25
  • @3Gee thanks for posting your code. I had found a way to complete my task and it is working now. I'll however compare your code with my current code to see if I can improve it. The problem was that there were hardly any complete examples of reading bytes, all the examples which are out there (most of which are copy/paste of the same thing) are for reading text. Your answer gave me the first clue that I had to avoid reading text and avoid readers. – zeeshan Sep 21 '16 at 12:59
  • @zeeshan, glad I've been able to help. I've posted that code as a reply for easier reading – 3Gee Sep 22 '16 at 12:06
1

Here's the solution, that I eventually came up with. I don't consider it a right or wrong, but it worked for my project back in 2011. Unfortunately, I can hardly remember any details regarding the code. Hope it helps.

String serverIP = "192.168.0.2";

InetAddress server = InetAddress.getByName(serverIP);
Socket socket = new Socket(server, 4444);
InputStream inStream = socket.getInputStream();

byte[] buffer = new byte[16200]; // extract image from image array 
int size = inStream.read(buffer);
byte[] buffer2 = new byte[size];
for(int g = 0; g < size; g++){ 
    buffer2[g] = buffer[g];
}
image = (ImageView) findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeByteArray(buffer2, 0, buffer2.length); 
image.setImageBitmap(bmp);
3Gee
  • 1,094
  • 2
  • 14
  • 23