2016-07-07 531 views
2

我想从一台PC(客户端)发送图像文件到另一台运行MATLAB的服务器(服务器),输出图像空出来。从计算机之间发送图像,从Java到MATLAB

从不同的讨论中,我了解到主要问题是Java和MATLAB之间的一些“图像矩阵不匹配”。但是,我并不完全了解这个问题。

如果您能给我一些建议,我将不胜感激。

客户端的Java代码:

import java.awt.image.BufferedImage; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 

import javax.imageio.ImageIO; 

public class myclientimage 
{ 
    public static void main(String args[]) throws IOException 
    { 
     BufferedImage img = ImageIO.read(new File("D:\\zzz.jpg")); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream();   
     ImageIO.write(img, "jpg", baos); 
     baos.flush(); 
     byte[] buffer = baos.toByteArray(); 

     DatagramSocket clientSocket = new DatagramSocket();  
     InetAddress IPAddress = InetAddress.getByName("192.168.0.102"); 
     System.out.println(buffer.length); 

     DatagramPacket packet = new DatagramPacket(buffer, buffer.length, IPAddress, 9091); 

     clientSocket.send(packet); 

     System.out.println("aaaa"); 
    } 

} 

服务器的MATLAB代码:

udpA=udp('192.168.0.104', 9090,'LocalPort', 9091); 
fopen(udpA); 
A = fread(udpA, 200000); 

du = reshape(A,size(A)); % converting vector du to 3d Image array 
imwrite(uint8(du), 'du.jpg'); %save our du to file du.jpg 
I = imread('du.jpg'); %test if it saved correctly 
imshow(I); 

fclose(udpA); 
+0

这是什么 “不同的讨论”?请提供一个链接。 –

+0

我没有特别的链接。我从matlab论坛和随机博客文章阅读了一些帖子。 – user1850484

+0

A的值是512x1的两倍。这显然不足以绘制图像。实际图像尺寸为307x307x3 uint8。我增加了UDP的buffersize和timeout-interval。还是行不通。 – user1850484

回答

5

好了,所以这里的解决方案。有些东西需要首先澄清,我们将图像作为压缩的jpeg发送,而不是作为独立的像素。因此imwrite不能用于此目的,因为它需要图像输入(3D阵列)。那么你应该使用fwrite来代替。

另一个(次要)问题是,按照您正在操作的方式将BufferedImage读为字节会给您不同的大小,我认为您在打印buffer.length时发现了这一点,并获得与计算机报告的大小不同的大小。对此的解决方案可以在in the second answer of this question找到。然而,这对图像没有影响(可能会降低质量?)传输为我工作,不管链接中提到的解决方案。

正如你在评论中已经提到的那样,你正在接受512双打。所以基本上有三件事情需要做:

  1. 增加您的UDP对象的InputBufferSize(默认512字节)。
  2. 增加UDP对象的InputDatagramPacketSize(默认值为8KB),除非您不希望文件大于此大小,否则您将以大块格式发送文件。
  3. 将双打转换为uint8,因为这是你如何接收它们。

最终的Java代码:

public class SendImageUDP { 
    public static void main(String args[]) throws IOException { 
    BufferedImage img = ImageIO.read(new File("your_pic.jpg")); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ImageIO.write(img, "jpg", baos); 
    baos.flush(); 
    byte[] imageBuffer = baos.toByteArray(); 
    System.out.println(imageBuffer.length); 

    InetAddress IPAddress = InetAddress.getByName("127.0.0.1"); // LocalHost for testing on the same computer 
    DatagramSocket clientSocket = new DatagramSocket(9090, IPAddress); // Specify sending socket 

    DatagramPacket packet = new DatagramPacket(imageBuffer, imageBuffer.length, IPAddress, 9091); 
    clientSocket.send(packet); 

    System.out.println("data sent"); 
    clientSocket.close(); 
    } 
} 

最后的MATLAB代码:

clear 
close all 

%% Define computer-specific variables 

ipSender = '127.0.0.1'; % LocalHost for testing on the same computer 
portSender = 9090; 

ipReceiver = '127.0.0.1'; % LocalHost for testing on the same computer 
portReceiver = 9091; 

%% Create UDP Object 

udpReceiver = udp(ipSender, portSender, 'LocalPort', portReceiver); 
udpReceiver.InputBufferSize = 102400; % 100KB to be safe 
udpReceiver.InputDatagramPacketSize = 65535; % Max possible 

%% Connect to UDP Object 

fopen(udpReceiver); 
[A, count] = fread(udpReceiver, 102400, 'uint8'); % Receiving in proper format, big size just to be safe 
A = uint8(A); % Just making sure it worked correctly 

fileID = fopen('du.jpg','w'); % Save as a JPEG file because it was received this way 
fwrite(fileID, A); 
I = imread('du.jpg'); % Test if it saved correctly 
imshow(I); 

%% Close 

fclose(udpReceiver); 
delete(udpReceiver); 

你可以从MATLAB代码中看到的,就没有必要重塑接收到的数据,因为它是已经压缩了JPEG数据,无论如何重塑它都没有意义。只需将其写入文件。

来源:

+1

你就像天使一样!非常感谢你。 – user1850484

+0

哈哈哈:随时祝你好运! –