Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 缓冲图像为空_Java_Python_Numpy - Fatal编程技术网

Java 缓冲图像为空

Java 缓冲图像为空,java,python,numpy,Java,Python,Numpy,我正在尝试从python应用程序向java应用程序发送帧。首先,我使用numpy数组在python端创建一个空白图像,然后将其发送到java应用程序并在java端显示。但是java端的BuffereImage是空的,这里是python代码 import socket import sys import cv2 import numpy as np import base64 import json from lib2to3.pytree import Leaf from lib2to3.fixe

我正在尝试从python应用程序向java应用程序发送帧。首先,我使用numpy数组在python端创建一个空白图像,然后将其发送到java应用程序并在java端显示。但是java端的BuffereImage是空的,这里是python代码

import socket
import sys
import cv2
import numpy as np
import base64
import json
from lib2to3.pytree import Leaf
from lib2to3.fixer_util import String

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = "localhost"
PORT = 5555
try:
    socket.connect((HOST, PORT))
except:
    print "We cannot find the server !!!!"
    print "Terminating the program . . ."
    #exit(0)

img = np.zeros((300, 300, 3), np.uint8) 
obj = NumpyEncoder()   
outjson = {}
outjson['img'] = base64.b64encode(img)
outjson['leaf'] = "leaf"
json_data = json.dumps(outjson)
socket.sendall(json_data

socket.close()
这里是java端

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Base64;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.json.JSONException;
import org.json.JSONObject;

import com.sun.javafx.iio.ImageFormatDescription;
import com.sun.prism.Image;

import java.awt.Color;
import java.awt.Dimension;

public class ServerFrame extends JFrame {

private JPanel contentPane;
public static BufferedImage bufferedImage;

/**
 * Launch the application.
 * @throws IOException 
 * @throws JSONException 
 */
public static void main(String[] args) throws IOException, JSONException {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ServerFrame frame = new ServerFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    ServerSocket serverSocket = null;
    Socket clientSocket = null;


    try {
        serverSocket = new ServerSocket(5555);
    } catch(IOException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }

    System.out.println("Server Socket Has Been Started . . .");
    try {
        clientSocket = serverSocket.accept();
        System.out.println("User Connected :" + clientSocket.getLocalAddress().toString());

    } catch(IOException e) {
        System.out.println(e.getMessage());
    }

    StringBuilder sb = new StringBuilder();
    InputStream in = clientSocket.getInputStream();
    if(in == null) System.exit(1);
    BufferedReader br = new BufferedReader(new    InputStreamReader(clientSocket.getInputStream()));
    String line;
    while ((line = br.readLine()) != null) {
           sb.append(line);
        }
    //System.out.println(sb.toString());

    JSONObject json = new JSONObject(sb.toString());
    String leaf_name = json.getString("leaf");
    String mat_string = json.getString("img");
    byte[] raw_data = Base64.getDecoder().decode(mat_string);
    bufferedImage = ImageIO.read(new ByteArrayInputStream(raw_data));
    if(bufferedImage == null) System.out.println("warning");
    System.out.println(raw_data.length);
    FileOutputStream fos = new FileOutputStream("image.jpg");
    try {
        fos.write(raw_data);
    }
    finally {
        fos.close();
    }
    /*JPanel panel = new JPanel();
    panel.setBackground(Color.RED);
    Dimension dim = new Dimension(50,50);
    panel.setSize(dim);
    panel.setMinimumSize(dim);
    panel.setMaximumSize(dim);
    panel.setPreferredSize(dim);
    JLabel label = new JLabel("hello");
    label.setSize(label.getPreferredSize());
    panel.add(label);
    panel.setVisible(true);
    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bufferedImage)));*/
    br.close();
    clientSocket.close();
}

/**
 * Create the frame.
 */
public ServerFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
}

}
您能看到问题吗?为什么为空?

更改为:

outjson['img'] = base64.b64encode(img.tobytes())

当没有
ImageReader
能够解码流时,read返回null。我怀疑该格式与标准java Libraries不兼容。numpy数组不是
ImageReader
可以读取的二进制图像。Base64编码一个numpy数组并不能使它成为一个图像。您能建议一个解决方案吗?