java - Javadrone - Unable to get image from Parrot AR.Drone 2.0 -
i working on app control parrot ar.drone 2.0
using javadrone api
, library.
i able connect drone , make take off/land successfully. there 2 java files : dronetestvideo.java
, videopanel.java
.
the dronetestvideo.java
file responsible connecting drone , data, while videopanel.java
file serves set images.
javadrone api
can download here.
however, have no idea why i'failing live images drone. show me black screen words:
"no video connection"
here code:
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 drone"); drone.connect(); drone.waitforready(connect_timeout); drone.clearemergencysignal(); system.err.println("connected 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); } } }
you not updating screen, no new calls drawdroneimage called videopanel.
you should set loop refresh jpanel (call repaint()).
and more, need implement doublebuffering strategies smoother image transaction.
Comments
Post a Comment