javaddrone -无法从Parrot AR.Drone 2.0获取图像



我正在开发一个应用程序来控制使用Javadrone API和库的Parrot Ar.Drone 2.0

我能够连接到无人机并使其成功起飞/降落。有2个java文件:DroneTestVideo.java, VideoPanel.java .

DroneTestVideo.java文件负责接入无人机获取数据,VideoPanel.java文件负责设置图像。

Javadrone API可在此下载。

然而,我不知道为什么我没有从无人机获得实时图像。它只是给我一个黑屏,上面写着:

"No video connection"

下面是我的代码:

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("**********nTRIMn**********");
            drone.trim();
            Thread.sleep(2000);
            System.err.println("**********nTAKEOFFn**********");
            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);
        }
    }
}

你没有更新你的屏幕,所以没有新的调用drawDroneImage将被调用到VideoPanel

你应该设置一个循环来刷新JPanel(调用repaint())。

此外,您还需要实现DoubleBuffering Strategies以获得更平滑的图像事务。

相关内容

  • 没有找到相关文章

最新更新