I wanted to try making a home surveillance project using my webcam and EV3 sending images into Dropbox. But how to put the obvious time and date stamp on the images? It turns out this is really easy to do using the Graphics2D Java class! A short tutorial on how to do this…

Java provides the Graphics2D class to do simple image manipulation natively. The nice thing about working with leJOS is that you can take advantage of all the existing Java classes in your code that runs on the EV3.

I used my previous code and added a call to the Graphics2D library to overlay the date and time. The thing to remember here is that you have to account for the font size when choosing the x,y location to place the text.

   /**
    * Add overlay text with the current UTC date and battery voltage level onto the image
    * Text colour, font and size can be adjusted below
    * 
    * @param old Original image captured from the camera
    * @return new BufferedImage that contains the original image with text overlay
    */
   private BufferedImage addTextOverlay(BufferedImage old) {
       int w = old.getWidth();
       int h = old.getHeight();
       BufferedImage img = new BufferedImage(
               w, h, BufferedImage.TYPE_INT_RGB);
       Graphics2D g2d = img.createGraphics();
       g2d.drawImage(old, 0, 0, null);
       g2d.setPaint(Color.red);
       g2d.setFont(new Font("Serif", Font.BOLD, 15));
       
       // Get the current system date and time in UTC
       Date d = new Date(System.currentTimeMillis());
       String s = "" + d.toString();
       FontMetrics fm = g2d.getFontMetrics();
       int x = img.getWidth() - fm.stringWidth(s) - 5;
       int y = fm.getHeight();
       g2d.drawString(s, x, y);

       // Get the current battery level in mV
       s = "B: " + Battery.getVoltageMilliVolt() + " mV";
       x = img.getWidth() - fm.stringWidth(s) - 5;
       g2d.drawString(s, x, h - y - 5);
       
       g2d.dispose();
       return img;
   }

 

The full code is here: https://github.com/markcrosbie/lejos_projects/tree/0eb43b00e9d8b0bab6bd985ff6d663d02907db3b/SurveillanceCam

I pointed the webcam out of a window in the morning and left the EV3 running on battery power for the day. I was curious how long the battery would last for! It turns out about 7 hours is the answer. After running for that long I had over 2500 images in my Dropbox directory, which I then stitched together into a timelapse video. Cool!