Javadrone - 无法从 Parrot AR.Drone 2.0 获取图像
Posted
技术标签:
【中文标题】Javadrone - 无法从 Parrot AR.Drone 2.0 获取图像【英文标题】:Javadrone - Unable to get image from Parrot AR.Drone 2.0 【发布时间】:2015-07-23 16:18:42 【问题描述】:我正在开发一个应用程序来控制 Parrot Ar.Drone 2.0
使用 Javadrone API
和库。
我能够连接到无人机并使其成功起飞/降落。有 2 个 java 文件:DroneTestVideo.java
、VideoPanel.java
。
DroneTestVideo.java
文件负责连接无人机并获取数据,VideoPanel.java
文件作为设置图像。
Javadrone API
可以下载here。
但是,我不知道为什么我无法从无人机获取实时图像。它只是给我一个黑屏,上面写着:
“没有视频连接”
这是我的代码:
DroneTestVideo.java
package dronetest;
import com.codeminders.ardrone.ARDrone;
import com.codeminders.ardrone.ARDrone.VideoChannel;
import com.codeminders.ardrone.DroneStatusChangeListener;
import com.codeminders.ardrone.NavData;
import com.codeminders.ardrone.NavDataListener;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DroneTestVideo extends javax.swing.JFrame implements DroneStatusChangeListener, NavDataListener
private static final long CONNECT_TIMEOUT = 10000L;
private ARDrone drone;
private final VideoPanel video = new VideoPanel();
public DroneTestVideo()
initComponents();
initDrone();
videoPanel.add(video);
takeoff();
private void initDrone()
try
drone = new ARDrone();
drone.addStatusChangeListener(this);
drone.addStatusChangeListener(new DroneStatusChangeListener()
public void ready()
org.apache.log4j.Logger.getLogger(getClass().getName()).debug("updateLoop::ready()");
);
System.err.println("Configure");
drone.selectVideoChannel(ARDrone.VideoChannel.HORIZONTAL_ONLY);
drone.setCombinedYawMode(true);
drone.enableAutomaticVideoBitrate();
System.err.println("Connecting to the drone");
drone.connect();
drone.waitForReady(CONNECT_TIMEOUT);
drone.clearEmergencySignal();
System.err.println("Connected to the drone");
catch (IOException ex)
Logger.getLogger(DroneTestVideo.class.getName()).log(Level.SEVERE, null,ex);
drone.addNavDataListener(this);
video.setDrone(drone);
private void takeoff()
try
System.err.println("**********\nTRIM\n**********");
drone.trim();
Thread.sleep(2000);
System.err.println("**********\nTAKEOFF\n**********");
drone.takeOff();
Thread.sleep(7000);
drone.land();
catch (IOException ex)
Logger.getLogger(DroneTestVideo.class.getName()).log(Level.SEVERE, null, ex);
catch (InterruptedException ex)
Logger.getLogger(DroneTestVideo.class.getName()).log(Level.SEVERE, null, ex);
public static void main(String args[])
java.awt.EventQueue.invokeLater(new Runnable()
public void run()
new DroneTestVideo().setVisible(true);
);
VideoPanel.java
package dronetest;
import com.codeminders.ardrone.ARDrone;
import com.codeminders.ardrone.DroneVideoListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class VideoPanel extends javax.swing.JPanel implements DroneVideoListener
private AtomicReference<BufferedImage> image = new AtomicReference<BufferedImage>();
private AtomicBoolean preserveAspect = new AtomicBoolean(true);
private BufferedImage noConnection = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB);
public VideoPanel()
initComponents();
Graphics2D g2d = (Graphics2D) noConnection.getGraphics();
Font f = g2d.getFont().deriveFont(24.0f);
g2d.setFont(f);
g2d.drawString("No video connection", 40, 110);
image.set(noConnection);
public void setDrone(ARDrone drone)
drone.addImageListener(this);
System.err.println("setDrone function here!");
public void setPreserveAspect(boolean preserve)
preserveAspect.set(preserve);
public void frameReceived(BufferedImage im)
image.set(im);
repaint();
public void paintComponent(Graphics g)
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
drawDroneImage(g2d, width, height);
private void drawDroneImage(Graphics2D g2d, int width, int height)
BufferedImage im = image.get();
if (im == null)
return;
int xPos = 0;
int yPos = 0;
if (preserveAspect.get())
g2d.setColor(Color.BLACK);
g2d.fill3DRect(0, 0, width, height, false);
float widthUnit = ((float) width / 4.0f);
float heightAspect = (float) height / widthUnit;
float heightUnit = ((float) height / 3.0f);
float widthAspect = (float) width / heightUnit;
if (widthAspect > 4)
xPos = (int) (width - (heightUnit * 4)) / 2;
width = (int) (heightUnit * 4);
else if (heightAspect > 3)
yPos = (int) (height - (widthUnit * 3)) / 2;
height = (int) (widthUnit * 3);
if (im != null)
g2d.drawImage(im, xPos, yPos, width, height, null);
【问题讨论】:
【参考方案1】:您没有更新屏幕,因此不会在 VideoPanel 中调用对 drawDroneImage 的新调用。
您应该设置一个循环来刷新 JPanel(称为 repaint())。
此外,您还需要实施 DoubleBuffering Strategies 以获得更顺畅的图像事务。
【讨论】:
我想我确实更新了我的屏幕,正如你在 public void frameReceived(BufferedImage im) 函数中看到的那样,我调用了“repaint()”函数。这就是假设的工作方式吗?以上是关于Javadrone - 无法从 Parrot AR.Drone 2.0 获取图像的主要内容,如果未能解决你的问题,请参考以下文章