Как получить видеопоток дрона Tello, используя Processing и UDP - PullRequest
1 голос
/ 05 июля 2019

Я пытаюсь получить видеопоток моего беспилотника Tello, используя обработку, как указано в документации SDK : enter image description here

Для получения видеопотока через UDP я попытался использовать скрипт от The Coding Train :

import java.awt.image.*; 
import javax.imageio.*;
import java.net.*;
import java.io.*;


// Port we are receiving.
int port = 11111; 
DatagramSocket ds; 
// A byte array to read into (max size of 65536, could be smaller)
byte[] buffer = new byte[65536]; 

PImage video;

void setup() {
  size(400,300);
  try {
    ds = new DatagramSocket(port);
  } catch (SocketException e) {
    e.printStackTrace();
  } 
  video = createImage(320,240,RGB);
}

 void draw() {
  // checkForImage() is blocking, stay tuned for threaded example!
  checkForImage();

  // Draw the image
  background(0);
  imageMode(CENTER);
  image(video,width/2,height/2);
}

void checkForImage() {
  DatagramPacket p = new DatagramPacket(buffer, buffer.length); 
  try {
    ds.receive(p);
  } catch (IOException e) {
    e.printStackTrace();
  } 
  byte[] data = p.getData();

  println("Received datagram with " + data.length + " bytes." );

  // Read incoming data into a ByteArrayInputStream
  ByteArrayInputStream bais = new ByteArrayInputStream( data );

  // We need to unpack JPG and put it in the PImage video
  video.loadPixels();
  try {
    // Make a BufferedImage out of the incoming bytes
    BufferedImage img = ImageIO.read(bais);
    // Put the pixels into the video PImage
    img.getRGB(0, 0, video.width, video.height, video.pixels, 0, video.width);
  } catch (Exception e) {
    e.printStackTrace();
  }
  // Update the PImage pixels
  video.updatePixels();
}

Единственное, что я изменил в скрипте, это переменная port, поскольку в документации SDK (см. Выше) указано, что она должна быть 11111.

Когда я запускаю скрипт (после успешной отправки command и streamon дрону), я получаю сообщение Received datagram with 65536 bytes. и исключение нулевого указателя. Я обнаружил, что исключение Null Pointer показано, потому что следующая строка кода возвращает null:

BufferedImage img = ImageIO.read(bais);

Итак, в конце мой вопрос: почему эта строка кода или, более конкретно, ImageIO.read(bais) возвращает null. Однако скрипт работает корректно, если вы используете его в сочетании со скриптом Video Sender из Coding Train . Итак, в чем здесь проблема?

...