缓冲图像为空

问题描述 投票:0回答:2

我正在尝试从 python 应用程序发送帧到 java 应用程序。首先,我使用 numpy 数组在 python 端创建一个空白图像,然后想将其发送到 java 应用程序并在 java 端显示它。但是java端的bufferedimage是null这里是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);
}

}

你能看到问题吗,为什么它是空的?

java python numpy
2个回答
0
投票

更改为:

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

0
投票

数据库中的 Base64 属性为空。要解决此问题,请手动打开数据库,或者以其他方式将 base64 值插入到导致 null 返回的属性中。我在初始化时遇到了同样的问题: WebImage 图像 = new WebImage(i.ImageData);

突出显示的 ImageData 为空,因此出现错误。解决方案是插入一个 Base64 值 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.