I’ve created my first “Java” tutorial. Still not quite finished I want to add motor and gpio control, plus a little more tinsel. But I thought it might be handy to have this here so I can show it to other people.
Basically if you make this in your java main and call setupbot() the bot will set itself up and start polling, then as soon as an image is ready you will get a callback “onnewimage” where you can display it on the screen.
I’m going to add a joystick and some more output to the java frame before I’m done with this example.
public class SimpleBotTutorial implements IBotListener{
ZBot zBot = null;
InputStream inS;
OutputStream outS;
JFrame frame = new JFrame("Video Feed");
JLabel JLabelImage = new JLabel();
public void logI(String str)
{
System.out.println(str);
}
private boolean getStreams()
{
try
{
CommPortIdentifier portIdentifier;
portIdentifier = CommPortIdentifier.getPortIdentifier("COM3");
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(baud,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
inS = serialPort.getInputStream();
outS = serialPort.getOutputStream();
return true;
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
// TODO FAIL EXIT
return false;
}
}
private void setupGui()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(251,150));
frame.getContentPane().add(JLabelImage, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public boolean startBot()
{
// if we cant get the stream no reason continuing
if(!getStreams())
{
return false;
}
// setup our zbot and pass it the streams
zBot = new ZBot( inS, outS );
// tell it we implement its callbacks for video and events
zBot.setIbl(this);
// wait a second for a connection, then check
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// check to see if its ready or we timed out
if(zBot.getState() < 2)
{
// fail
zBot.pollEngine.stopSvc();
// close our streams
try {
inS.close();
outS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// let caller know we failed
return false;
}
// setup the gui to get our data
setupGui();
return true;
}
public void printAllBotValues()
{
this.logI("=================================================");
for ( EzReadVars rv : EzReadVars.values())
{
this.logI("Var: " + rv + " Value: " + zBot.getValue(rv));
}
}
public void rawToImage(byte[] rawData, int w, int h)
{
// make new container for the image
BufferedImage NewImg = new BufferedImage(w, h, BufferedImage.TYPE_USHORT_565_RGB);
// convert to short for raster
short[] picAsShort = new short[w*h];
ByteBuffer.wrap(rawData).order(ByteOrder.BIG_ENDIAN).asShortBuffer().get(picAsShort);
// put directly in raster of image
short[] imgData = ((DataBufferUShort)NewImg.getRaster().getDataBuffer()).getData();
System.arraycopy(picAsShort, 0, imgData, 0, picAsShort.length);
// set our jlabel to the image
this.JLabelImage.setIcon(new ImageIcon(NewImg));
// refresh
frame.repaint();
}
@Override
public void onNewImage(ZImage image) {
// turn it into a image and display it
rawToImage(image.getData(),image.getWidth(),image.getHeight());
}
@Override
public void onNewEvent(int event) {
// TODO Auto-generated method stub
}
}