我正在尝试使用SDK documentation中所述的“处理”来接收Tello无人机的视频流:
要通过UDP接收视频流,我尝试使用a script by 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 Exception,因为下面的代码行返回null
:
BufferedImage img = ImageIO.read(bais);
所以最后,我的问题是,为什么这一行代码或更具体地说ImageIO.read(bais)
返回null
。但是,如果将脚本与Video Sender script of the Coding Train结合使用,则脚本可以正常工作。那么,这是什么问题?