如何将图像流应用于gui

时间:2019-04-06 12:25:40

标签: java image dynamic

我正在使用IM客户端,目前我正在尝试使摄像头流媒体通过http协议工作。

服务器从源返回一个包含base64编码的二进制数据的网页。

我尝试了各种实验,通过服务器提供的base64编码图像来设置标签的图标状态

package net.undergroundim.client.gui;

import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Base64;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class tmp {

    private JFrame frame;
    private static JLabel lblNewLabel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    tmp window = new tmp();
                    window.frame.setVisible(true);
                    while (true) {
                        Thread.sleep(5);
                        String src = "";
                        src = URLSrc.getURLSource("http://127.0.0.1:9991/video");
                        byte[] decodedString = Base64.getDecoder().decode(src);
                        if(decodedString.length > 25) {
                        InputStream in = new ByteArrayInputStream(decodedString);

                            BufferedImage bImageFromConvert = ImageIO.read(in);

                            getLblNewLabel().setIcon(new ImageIcon(bImageFromConvert));
                            getLblNewLabel().revalidate();
                            getLblNewLabel().repaint();

                    }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * Create the application.
     */
    public tmp() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        lblNewLabel = new JLabel();
        lblNewLabel.setIcon(new ImageIcon(tmp.class.getResource("/icons/earth-icon.png")));
        lblNewLabel.setBounds(0, 0, 428, 244);
        frame.getContentPane().add(lblNewLabel);
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);

    }
    public static JLabel getLblNewLabel() {
        return lblNewLabel;
    }
}

通过http返回的base64数据应使用setIcon()解码为标签控件上显示的图像,但是当我尝试将解码后的字节解码为标签时,它只是显示为空白

0 个答案:

没有答案