Skip to content

Commit

Permalink
Raspberrypi drivers using raspixxx CLI, refs #694
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmao86 authored and sarxos committed Mar 20, 2019
1 parent faea287 commit 78c58cb
Show file tree
Hide file tree
Showing 54 changed files with 4,686 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ README.md~
*.ipr
*.iws
.DS_Store
/webcam-capture-drivers/driver-raspistill/jgoodies-forms-1.8.0.jar

This comment has been minimized.

Copy link
@sarxos

sarxos Mar 20, 2019

Owner

@alexmao86 Can we change this to:

*.jar
*.zip

This comment has been minimized.

Copy link
@alexmao86

alexmao86 Mar 21, 2019

Author Collaborator

ok. they are already removed

This comment has been minimized.

Copy link
@sarxos

sarxos Mar 21, 2019

Owner

@alexmao86 Will you send next pull request? Or should I rather remove them by myself?

This comment has been minimized.

Copy link
@alexmao86

alexmao86 Mar 21, 2019

Author Collaborator

@sarxos, to simple, please remove it by yourself and then I will sync with you. because I am not skilled git user. :)

This comment has been minimized.

Copy link
@alexmao86

alexmao86 Mar 22, 2019

Author Collaborator

@sarxos , let me remove them and send next pull request. I need to change unit test code according to resources deletion. I am free today.

/webcam-capture-drivers/driver-raspistill/jgoodies-forms-1.8.0-sources.jar
/webcam-capture-drivers/driver-raspistill/miglayout15-swing.jar
/webcam-capture-drivers/driver-raspistill/miglayout-src.zip
116 changes: 116 additions & 0 deletions webcam-capture-drivers/driver-raspberrypi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# webcam-capture-driver-raspberrypi

This capture driver is designed special for raspberrypi. __raspistill,raspiyuv, ...__ are the command line tools on raspberrypi for capturing photographs or vedios with the camera module. The most important is that command line tools have feature grabbing image repeatly(mock FPS) from camera and print to file or console. They are:

1. -o, --output : Output filename <filename> (to **write to stdout, use '-o -**'). If not specified, no file is saved
2. -tl, --timelapse : Timelapse mode. Takes a picture every <t>ms
3. -n, --nopreview : Do not display a preview window

So it is possible to use java `Runtime.execute()` to launch raspixxx process and intercept its console output, makes them as
webcam driver. this is one simple and straightforward approach without native JNI or JNA call, no file system exchange. You
will not struggle with performance issue and native code compiling. Because raspberrypi camera is connected to GPU directly,
the only CPU usage of drivers is converting rgb24 data to java BufferedImage, the system average CPU loading is 30% when
driver and Swing panel running.

This dirver has been contributed by Alex Mao (alexmao86, https://github.com/alexmao86). Thank you Alex!

## Install Required Binaries

sudo apt-get update
sudo apt-get install raspistill raspivid raspiyuv raspividyuv

## Maven

```xml
<!--If you want to access snapshot repo, please add this to your pom -->
<repository>
<id>Sonatype OSS Snapshot Repository</id>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture-driver-raspberrypi</artifactId>
<version>${your version}</version>
</dependency>
```

## How To Use

Set capture driver before you start using Webcam class. this is one basic demo, check test source for demo with option panel.

```java
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamPanel.DrawMode;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.ds.raspberrypi.*;


public class WebcamPanelExample {

static {
//Webcam.setDriver(new RaspistillDriver());
Webcam.setDriver(new RaspiYUVDriver());
//Webcam.setDriver(new RaspividDriver());
//Webcam.setDriver(new RaspividYUVDriver());
}

public static void main(String[] args) throws InterruptedException {

final JFrame window = new JFrame("Raspberrypi Capture Example");
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setLayout(new FlowLayout());

final Dimension resolution = WebcamResolution.QVGA.getSize();

for (final Webcam webcam : Webcam.getWebcams()) {

webcam.setCustomViewSizes(resolution);
webcam.setViewSize(resolution);
webcam.open();

final WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true);
panel.setDrawMode(DrawMode.FIT);
panel.setImageSizeDisplayed(true);
panel.setPreferredSize(resolution);

window.getContentPane().add(panel);
}

window.pack();
window.setVisible(true);
}
}
```

Please check WebcamPanelExample.java in src/test/java for detail.

## Known Problems

Because is its based on pure Java image processing, the performance of `RaspistillDriver` and `RaspiYUVDriver` is not very good. The actuall FPS is low. According to test, about 500ms for PNG decoder to decode 640x480 image from raspistill stream. But the advantage of `RaspividDriver` and `RaspividYUVDriver` is the fact that Java code is comsuming RAW RGB24 data in _almost_ realtime by following native process STDOUT.

## Driver Parameters

This driver makes above arguments configurable, when driver instance created, it will load arguments step by step as follows:

1. Load built-in parameters
2. Search system properties which starts with "raspi.", then merge to properties
3. You can set all command line arguments from device instance's setParameters

## Capture Driver License

Copyright (C) 2012 - 2019 Webcam Capture API Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

100 changes: 100 additions & 0 deletions webcam-capture-drivers/driver-raspberrypi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>webcam-capture-drivers</artifactId>
<groupId>com.github.sarxos</groupId>
<version>0.3.13-SNAPSHOT</version>
</parent>

<artifactId>webcam-capture-driver-raspberrypi</artifactId>
<packaging>jar</packaging>

<name>Webcam Capture - Raspberry Pi camera Driver</name>
<description>Webcam Capture driver for capturing images for raspberrypi camera</description>
<developers>
<developer>
<id>alexmao86</id>
<name>Mao Anping</name>
<email>[email protected]</email>
<organization>Personal</organization>
<organizationUrl>www.nowcode.cn</organizationUrl>
<timezone>Asia/Shanghai, UTC+8</timezone>
</developer>
</developers>
<dependencies>

<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<!-- <compilerArgument>-g:none</compilerArgument> -->
<debug>true</debug>
<target>1.8</target>
<source>1.8</source>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.sarxos.webcam.ds.raspberrypi;

public class ColorUtil {

This comment has been minimized.

Copy link
@sarxos

sarxos Mar 20, 2019

Owner

@alexmao86 Are these java classes required?

This comment has been minimized.

Copy link
@sarxos

sarxos Mar 20, 2019

Owner

@alexmao86 I would also like to remove images from driver-raspberrypi/src/etc/resources, I think these are not required.

This comment has been minimized.

Copy link
@alexmao86

alexmao86 Mar 21, 2019

Author Collaborator

@sarxos for some classes in etc/resources. it is unused classes for experiment, it is OK to delete them. and I will remove some unused resource.

thanks,
Alex

private static int R = 0;
private static int G = 1;
private static int B = 2;
/**
* Change raw YUV420 color to RGB color. Colours could be 0-255.
*
* @param yuv
* The array of YUV components to convert
* @param rgb
* An array to return the colour values with
*/
public static byte[] convertYUVtoRGB(byte[] src, int width, int height) {
int numOfPixel = width * height;
int positionOfV = numOfPixel;
int positionOfU = numOfPixel/4 + numOfPixel;
byte[] rgb = new byte[numOfPixel*3];

for(int i=0; i<height; i++){
int startY = i*width;
int step = (i/2)*(width/2);
int startV = positionOfV + step;
int startU = positionOfU + step;
for(int j = 0; j < width; j++){
int Y = startY + j;
int V = startV + j/2;
int U = startU + j/2;
int index = Y*3;
rgb[index+B] = (byte)((src[Y]&0xff) + 1.4075 * ((src[V]&0xff)-128));
rgb[index+G] = (byte)((src[Y]&0xff) - 0.3455 * ((src[U]&0xff)-128) - 0.7169*((src[V]&0xff)-128));
rgb[index+R] = (byte)((src[Y]&0xff) + 1.779 * ((src[U]&0xff)-128));
}
}

return rgb;
}


}
Loading

0 comments on commit 78c58cb

Please sign in to comment.