How can i send image using UDP in Matlab code ?

I want to create UDP communication between Java and Matlab. Matlab is us a server and while Java is the client. The client asks to the server to image. How can I write a code in Matlab to send the image to the client?

 采纳的回答

If you have Instrument Control Toolbox, you can use this functionality:
- Sebastian

14 个评论

I used this code to send the image but there is an error. How can I fix the error? fwrite(u, image,'uint8')
If you do not have the instrument control toolbox then there is a file exchange contribution for tcp and udp.
Error using icinterface/fwrite (line 187)
Instrument object OBJ is an invalid object. this is the error
%this is my code to send the image in Matlab
Sender = 'localhost'; % LocalHost for testing on the same computer
portSender = 33781;
ipReceiver = 'localhost'; % LocalHost for testing on the same computer
portReceiver = 33780;
udpReceiver = udp(Sender,portSender, 'LocalPort', portReceiver);
image =imread('/Users/eritreatsegay/Downloads/UDP/0.jpg');
fwrite(udpReceiver , image,'uint8')
//this java class code to receive the image and display
public static void main(String args[]) throws IOException {
InetAddress IPAddress = InetAddress.getByName("localhost"); // LocalHost for testing on the same computer
DatagramSocket clientSocket = new DatagramSocket(33781, IPAddress);
byte[] imageBuffer = new byte[1024 ] ;
DatagramPacket p=new
DatagramPacket(imageBuffer,imageBuffer.length);
System.out.println("UDP S: Receiving...");
clientSocket.receive(p);
byte[] buffer = p.getData();
BufferedImage img =ImageIO.read(new ByteArrayInputStream(buffer));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(200,300);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
You need to fopen the udp object after configuring it.
%this is my code to send the image in Matlab
Sender = 'localhost'; % LocalHost for testing on the same computer
portSender = 33781;
ipReceiver = 'localhost'; % LocalHost for testing on the same computer
portReceiver = 33780;
udpReceiver = udp(Sender,portSender, 'LocalPort', portReceiver);
fopen(udpReceiver);
image =imread('/Users/eritreatsegay/Downloads/UDP/0.jpg');
fwrite(udpReceiver , image,'uint8')
fclose(udpReceiver);
delete(udpReceiver);
% even I add fopen(u) still there is the error
At the moment I do not know what is wrong with the udp object. I am not at my desk to be able to test it.
If we are able to solve the udp problem, then I think your code needs modification. You are sending a pixel array to the Java code and expecting that ImageIO.read will turn ut into a Java image. But you have not sent any information about the size over, so the Java side cannot know the proper aspect ratio (and there are questions about whether both sides are using the same pixel component order.)
My suspicion is that ImageIO.read is expecting the raw content of an image file, such as if you had fopen/fread/fclose the jpg file itself instead of imread to get the pixel array.
Changing this would be independent of the udp issue.
%this is the error description in Matlab
Error using icinterface/fwrite (line 187)
An error occurred during writing.
%this means the error is in Matlab fwrite before it sent
I understand that. I am not at my desk so I cannot test udp at this time.
However, I can scan your code to see if there are other problems as well as the udp problem. When I do that I see a problem with the way the Java code would decode the array it receives (once we get to the point where it is able to receive the array.) I think you will need to send the jpg file content instead of the byte array.
Your Java side is expecting to read a single datagram of maximum length 1024. Your image content is likely to be larger than that. On the MATLAB side you are not creating any output buffer so you will get the default which is 512. The maximum output buffer size for udp objects is 4096.
Are you expecting to send very small images? If not then you need to create a protocol on top of udp to send all of the bytes in multiple packets, taking into account that the packets could arrive out of order. https://www.mathworks.com/help/instrument/outputbuffersize.html

请先登录,再进行评论。

更多回答(1 个)

runudp.m :
%this is my code to send the image in Matlab
Sender = 'localhost'; % LocalHost for testing on the same computer
portSender = 33781;
ipReceiver = 'localhost'; % LocalHost for testing on the same computer
portReceiver = 33780;
BS = 1024;
udpReceiver = udp(Sender,portSender, 'LocalPort', portReceiver, 'OutputBufferSize', BS);
fopen(udpReceiver);
image = imread('peppers.png');
fwrite(udpReceiver , image(1:BS), 'uint8')
fclose(udpReceiver);
delete(udpReceiver);
xferudp.java :
import java.io.IOException;
import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.image.BufferedImage;
import java.awt.FlowLayout;
import java.io.ByteArrayInputStream;
import javax.swing.ImageIcon;
class xferudp {
//this java class code to receive the image and display
public static void main(String args[]) throws IOException {
InetAddress IPAddress = InetAddress.getByName("localhost"); // LocalHost for testing on the same computer
DatagramSocket clientSocket = new DatagramSocket(33781, IPAddress);
byte[] imageBuffer = new byte[1024 ] ;
DatagramPacket p=new
DatagramPacket(imageBuffer,imageBuffer.length);
System.out.println("UDP S: Receiving...");
clientSocket.receive(p);
byte[] buffer = p.getData();
BufferedImage img =ImageIO.read(new ByteArrayInputStream(buffer));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(200,300);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
To use,
!javac -d . -classpath . xferudp.java
!sudo java xferudp
This gets as far as
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
at xferudp.main(xferudp.java:35)
which might have something to do with the fact that we did not a valid image over to be decoded.
The write error was definitely tied to writing too much for the socket. Remember, there is no automatic segmentation for UDP, and no automatic reconstruction by order -- every UDP packet is independent of each other.

5 个评论

So what is the solution for this error?
Have MATLAB transmit a complete image file, not the result of imread. The complete file must fit within BS bytes. BS must not be set beyond 4096, and it is possible that the largest you can actually use is 1492 or so (total of 1506 bytes in the udp packet including headers.)
If the file to send might be more than fits into one packet then you have a lot more work to do.
I got it to work for a png file (but not for all image files.) Code is attached. Rename the xferudp.java.txt to xferudp.java and invoke the java compiler, and then (in a window different than the MATLAB window) sudo to run the code as shown above. Then in MATLAB, execute runudp . It will send over the coins.png sample image and the java side will display it.
If you need to send images that are more than 65535 bytes for the file, then you will need to send as multiple packets, and you will need to deal with reassembly taking into account that packets might not get delivered.
Thank you very much it is working now

Can you please guide me about sending a file of more than 65KB from Client to Server in UDP communication in Matlab?I am unable to read the bytes of loaded image and have no idea about sending it from client to server!!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Java Package Integration 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by