在计算机之间发送图像,从Java到MATLAB

时间:2016-07-07 21:12:58

标签: java matlab udp client-server file-transfer

我正在尝试将图像文件从一台PC(客户端)发送到运行MATLAB的另一台PC(服务器),输出图像为空。

从另一个讨论中,我了解到主要问题是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);

1 个答案:

答案 0 :(得分: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,因为这是您接收它们的方式。
  4. 最终的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数据,无论如何重塑它都没有意义。只需将其写入文件即可。

    来源: