如何通过UDP发送Mat(opencv)

时间:2016-03-25 12:00:52

标签: opencv udp video-streaming

我正在编写一个使用java和openCV进行视频通话的程序。我想发送" Mat"在openCV中通过UDP。

我的代码:

public class Test_2503_video {
static int check_fps = 0;
static NewJFrame frame_to_show = new NewJFrame();
public static void main (String args[]){
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    VideoCapture camera = new VideoCapture(0);
    frame_to_show.setVisible(true);
    DatagramSocket datagramSocket = null;
    try {
        datagramSocket = new DatagramSocket(1995);
    } catch (SocketException ex) {
        Logger.getLogger(Test_2503_video.class.getName()).log(Level.SEVERE, null, ex);
    }
    if(!camera.isOpened()){
        System.out.println("Error");
    }
    else {
        Mat frame = new Mat();
            BufferedImage bf = null;
            fps();
        while(true){
            if (camera.read(frame)){
                    bf = Mat2BufferedImage(frame);
                    Icon icon = new ImageIcon(new ImageIcon(bf).getImage().getScaledInstance(frame.width(), frame.height(), Image.SCALE_DEFAULT));
                    System.out.println(frame.total()*frame.channels());
                    byte[] bs = new byte[10240];
                    frame.get(0, 0, bs);
                        try {
                            DatagramPacket datagramPacket = new DatagramPacket(bs, bs.length,InetAddress.getByName("127.0.0.1"),1996);
                            datagramSocket.send(datagramPacket);
                        } catch (UnknownHostException ex) {
                            Logger.getLogger(Test_2503_video.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(Test_2503_video.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    frame_to_show.video.setIcon(icon);
                check_fps++;
            }
        }   
    }
    camera.release();
}
public static void fps(){
    Thread t = new Thread(() -> {
        while(true){
            frame_to_show.fps.setText(String.valueOf("FPS: "+check_fps));
            check_fps = 0;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Test_2503_video.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
    t.start();
}
public static BufferedImage Mat2BufferedImage(Mat m)
{

    int type = BufferedImage.TYPE_BYTE_GRAY;
    if (m.channels() > 1)
    {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }
    int bufferSize = m.channels() * m.cols() * m.rows();
    byte[] b = new byte[bufferSize];
    m.get(0, 0, b); // get all the pixels
    BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
    final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
    System.arraycopy(b, 0, targetPixels, 0, b.length);
    return img;


}
}   

提供的数据元素编号(10240)应该是Mat通道数(3)的倍数。

我该如何解决?

0 个答案:

没有答案