Saturday, November 12, 2016

IOT: Java Program to take Selfie Snaps and Videos Using RaspiCam on Raspberry Pi


Objective: The Objective of this exercise is to run the Java program on Raspberry Pi which will take 10 Selfie Snaps and two 10 second Videos.

YouTube Video:
GitHub Source:

https://github.com/agilerules/IOT/tree/master/iot-pi4j-raspicamera

Dependencies:
Software:
Java
Pi4j
Maven (Please find my blog on how to install Maven on Raspberry Pi)
Hardware:
Raspberry Pi (installed with Java, Maven)
 Camera Module (RaspiCam)
Steps to setup the Java program:
1.  If your RaspiCam is not setup yet on the Raspberry Pi, please refer my other blog on how to setup this.  Ensure that Raspberry Pi is switched off and install the Raspberry Pi Camera module by inserting the cable into the Raspberry Pi.
2. The cable slots into the connector situated between the Ethernet and HDMI ports, with the silver connectors facing the HDMI port. I just used a gift box here to hold the Raspicam, so that it can take my selfie pictures easily, but this step is really not required.

3.   Now connect to Raspberry Pi using Putty with your Raspberry Pi Credentials and by-default, you will in the path /home/pi.
4.   For proper organization purpose, I created folders (using mkdir command) \projects. But this is not mandatory. You can even skip this step and move to next step.
mkdir projects
5.  From the \projects folder, execute the following command to Git clone the code to your local raspberry Pi.
 git clone https://github.com/agilerules/IOT.git
6.  Once the git clone is complete, you will see IOT folder created in \projects folder. Now go to IOT folder (using the command cd IOT) and you will see the folder iot-pi4j-raspicamera inside this folder IOT.
7.  Move to this iot-pi4j-raspicamera folder (using the command cd iot-pi4j-raspicamera). Now execute the following commands for Maven clean and Maven package:
  mvn clean
     

   mvn package


8. Now execute the following command to start the maven build and run the Java class Takesnap.java in standalone mode. (Incase if mvn command is not recognized then it means that maven is not installed on your Raspberry Pi.  Please follow my blog on how to install Maven on Raspberry Pi.
mvn exec:java -Dexec.mainClass="camera.TakeSnap" -Dexec.classpathScope=runtime
9.   Now you should see the following output, where it will take 10 Selfie snaps.



     You can view the snap (files ending with .jpg) in the project root folder i.e inside \iot-pi4j-raspicamera by invoking an ls command.

10. Now execute the following command to start the maven build and run the Java class TakeVideo.java in standalone mode.
mvn exec:java -Dexec.mainClass="camera.TakeVideo" -Dexec.classpathScope=runtime

    You can view the snap (files ending with .h264) in the project root folder i.e inside \iot-pi4j-raspicamera by invoking an ls command.
11. Here is the source code explanation below:
12. Now you should see the following output, where it will take two 10 seconds Video and the files will be available in the following path:
The complete project structure is available here:
       a)      Pom.xml:
       The below is the library dependency that we need for this application:
pi4j-core – This is for Pi4J to communicate with Raspberry Pi GPIO pins.
<dependencies>
            <dependency>
                  <groupId>com.pi4j</groupId>
                  <artifactId>pi4j-core</artifactId>
                  <version>1.1-SNAPSHOT</version>
            </dependency>
   </dependencies>

<repository>
            <id>oss-snapshots-repo</id>
            <name>Sonatype OSS Maven Repository</name>
            <url>https://oss.sonatype.org/content/groups/public</url>
            <snapshots>
                  <enabled>true</enabled>
                  <updatePolicy>always</updatePolicy>
            </snapshots>
</repository>
      b)      Takesnap.java:
     This java class will take 10 still images with timeout of 5 millisecond and the output file name will be snap<i>.jpg, where i ranges from 1 to 10 and this can be achieved with the following command:
   raspistill --timeout 5 --output snap" + i + ".jpg --nopreview
 The above command can be executed using exec() function of Runtime instance.
Runtime rt = Runtime.getRuntime();
System.out.println("Raspberry Camera is going to take 10 snaps, be ready...");
    System.out.println("Started taking the snaps...");
    for (int i=1; i<=10; i++)
    {
      long before = System.currentTimeMillis();
      //The below raspistill command will take photos with width as 200,  as 150, timeout of 1 millsec, image name as "snap<i>.jpg" (where <i> is the counter) with nopreview
Process snap = rt.exec("raspistill --timeout 5 --output snap" + i + ".jpg --nopreview");
      snap.waitFor(); // Sync
      long after = System.currentTimeMillis();
      System.out.println("Snapshot Take #" + i + " is complete in " + Long.toString(after - before) + " ms.");
    }
     c)      TakeVideo.java:
    This java class will take two videos of 10 seconds each and output file name will be              video<i>.h264, where i ranges from 1 to 2 and this can be achieved with the following command:
        raspivid --timeout 10000 --output video" + i + ".h264"
The above command can be executed using exec() function of Runtime instance.

Runtime rt = Runtime.getRuntime();
System.out.println("Raspberry Camera is going to take two videos of 10 seconds each, be ready..");
      System.out.println("Started capturing video..");
       for (int i=1; i<=2; i++)
       {
        long before = System.currentTimeMillis();
//The below raspivid command will take videos of 1000 millseconds (i.e 10 seconds) with the output file name as "video<i>.h264" (where <i> is the counter)
Process snap = rt.exec("raspivid --timeout 10000 --output video" + i + ".h264");
             snap.waitFor(); // Sync
            long after = System.currentTimeMillis();
System.out.println("Video Take #" + i + " is complete in " + Long.toString(after - before) + " ms.");
    }

That's it. Hope it was useful..:-)

No comments:

Post a Comment