[Python] Buffered image is null

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to send frames to java app from python app. Firstly I create a blank image on python side with numpy array and then wanna send it to java app and display it on the java side. But the bufferedimage on java side is null here is python code;

    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()


    and here is java side;

    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);
    }

    }


    Can you see the problem, Why is it null ?

    Continue reading...

Compartilhe esta Página