Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Receiving image Data as Binary Array and Then Display it
Replies: 1   Last Post: May 24, 2013 1:37 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Lavan Kumar

Posts: 2
Registered: 10/9/12
Receiving image Data as Binary Array and Then Display it
Posted: Oct 9, 2012 2:24 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi Guys
My problem is
I am new to Matlab environment
I am sending a image from Android Phone ( Converted to binary Array at the Client ) and to Matlab Server Via Sockets...

I dont know how to recieve the binary array , somehow found the code to recieve data from Client . ( REcieving as ZERos Matrix)
Now the problem is ,after recieving the binary array data of a image , i cannot convert it back to image

Can you please help me guys

Thank you...

Here is my Code Snippets


SERVER.m

% SERVER Write a message over the specified port
%
% Usage - server(output_port, number_of_retries)
% function Ser(output_port, number_of_retries)
function [message] = Ser(output_port, number_of_retries)

import java.net.ServerSocket
import java.io.*
import javax.imageio.*
import javax.imageio.ImageIO
import java.io.File
import java.awt.image.BufferedImage

if(nargin < 2)
number_of_retries = 20; %set to -1 for infinite
end

retry =0;
server_socket = [];
output_socket = [];

while true

retry = retry +1 ;
try
if((number_of_retries >0) && (retry >number_of_retries))
fprintf(1, 'Too many retries\n');
break;
end
fprintf(1, ['Try %d waiting for client to connect to this ' ...
'host on port : %d\n'], retry, output_port);

% wait for 1 second for client to connect server socket
server_socket = ServerSocket(output_port);
server_socket.setSoTimeout(1000);

output_socket = server_socket.accept;

fprintf(1, 'Client connected\n');

% get a buffered data input stream from the socket
input_stream = output_socket.getInputStream;
d_input_stream = DataInputStream(input_stream);


% read data from the socket - wait a short time first
pause(0.5);
bytes_available = input_stream.available;
fprintf(1, 'Reading %d bytes\n', bytes_available);
row= bytes_available;




message = zeros(1,row, 'uint8');

for i = 1:bytes_available
message(i) = d_input_stream.readByte;
end
message;
whos message;



% clean up
server_socket.close;
output_socket.close;
break;
catch ME
if ~isempty(server_socket)
server_socket.close
end

if ~isempty(output_socket)
output_socket.close
end

% pause before retrying
pause(1);

end
end

end

Client.java

package com.uhcl.edu.lavankumar.client;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Home extends Activity {

//Declaring the Variables
Button connect,disconnect, send;
TextView serverstatus, imagestatus;
ImageView image,image2;
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;





@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

//Assigning View components to its Variables

connect = (Button) findViewById(R.id.button1);
disconnect = (Button) findViewById(R.id.button2);
send = (Button) findViewById(R.id.button3);
serverstatus = (TextView) findViewById(R.id.textView2);
imagestatus = (TextView) findViewById(R.id.textView3);
image = (ImageView) findViewById(R.id.imageView3);
image2 = (ImageView) findViewById(R.id.imageView4);


//connect Button

connect.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub


try{
socket = new Socket("192.168.1.93", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
serverstatus.setText("Server connected sucessfully");
image2.setVisibility(View.INVISIBLE);
}
catch(IOException e)
{
e.printStackTrace();
}

}
});


//disconnect Button

disconnect.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
try {

socket.close();
serverstatus.setText("Server Dis Connected sucessfully");
image2.setVisibility(View.INVISIBLE);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

//send Button

send.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub


//Get the Image From the ImageView And Store it on a File ....
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();

image2.setImageBitmap(bitmap);



ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Log.i("lavan kumar",byteArray.toString());

if (socket != null){

//
try {

image2.setVisibility(View.VISIBLE);


//Sending the User Entered String to Server
//Sending Byte Array to Client Via JAVA Sockets
int start =0 ;
int len = byteArray.length;



dataOutputStream.writeInt(len);
if (len > 0) {
dataOutputStream.write(byteArray, start, len);
}



//Setting the SErver Message in the TExtView
imagestatus.setText(dataInputStream.readUTF());


} catch (UnknownHostException e) {
// TODO Auto-generated catch block

Log.i("kumar","Error1");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("kumar","Error2");
imagestatus.setText("Server in not connected.Please connect it");
e.printStackTrace();
}
finally{

try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}







}
});


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
}



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.