Skip to content

Commit f800924

Browse files
committed
BAEL-4329 - Capturing image from webcam in java
1 parent 9db4e1d commit f800924

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

image-processing/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@
8282
<artifactId>MarvinPlugins</artifactId>
8383
<version>${marvin-version}</version>
8484
</dependency>
85+
<dependency>
86+
<groupId>org.bytedeco</groupId>
87+
<artifactId>javacv-platform</artifactId>
88+
<version>${javacv-platform.version}</version>
89+
</dependency>
90+
<dependency>
91+
<groupId>com.github.sarxos</groupId>
92+
<artifactId>webcam-capture</artifactId>
93+
<version>${webcam-capture.version}</version>
94+
</dependency>
95+
8596
</dependencies>
8697

8798
<properties>
@@ -91,6 +102,8 @@
91102
<tess4j.version>4.5.1</tess4j.version>
92103
<tesseract-platform.version>4.1.0-1.5.2</tesseract-platform.version>
93104
<opencv.version>3.4.2-0</opencv.version>
105+
<javacv-platform.version>1.5.5</javacv-platform.version>
106+
<webcam-capture.version>0.3.12</webcam-capture.version>
94107
<imgscalr-version>4.2</imgscalr-version>
95108
<thumbnailator-version>0.4.11</thumbnailator-version>
96109
<marvin-version>1.5.5</marvin-version>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.imagefromwebcam;
2+
3+
import marvin.gui.MarvinImagePanel;
4+
import marvin.image.MarvinImage;
5+
import marvin.io.MarvinImageIO;
6+
import marvin.video.MarvinJavaCVAdapter;
7+
import marvin.video.MarvinVideoInterface;
8+
import marvin.video.MarvinVideoInterfaceException;
9+
10+
public class MarvinExample {
11+
12+
public static void main(String[] args) throws MarvinVideoInterfaceException {
13+
MarvinVideoInterface videoAdapter = new MarvinJavaCVAdapter();
14+
videoAdapter.connect(0);
15+
MarvinImage image = videoAdapter.getFrame();
16+
MarvinImageIO.saveImage(image, "selfie.jpg");
17+
}
18+
19+
public void captureWithPanel() throws MarvinVideoInterfaceException {
20+
MarvinVideoInterface videoAdapter = new MarvinJavaCVAdapter();
21+
videoAdapter.connect(0);
22+
MarvinImage image = videoAdapter.getFrame();
23+
24+
MarvinImagePanel videoPanel = new MarvinImagePanel();
25+
videoPanel.setImage(image);
26+
27+
videoPanel.setSize(800,600);
28+
videoPanel.setVisible(true);
29+
}
30+
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.imagefromwebcam;
2+
3+
import org.bytedeco.javacv.*;
4+
import org.bytedeco.opencv.opencv_core.IplImage;
5+
import java.awt.event.WindowEvent;
6+
import javax.swing.JFrame;
7+
import static org.bytedeco.opencv.helper.opencv_imgcodecs.cvSaveImage;
8+
9+
public class OpenCVExample {
10+
11+
public static void main(String[] args) throws Exception {
12+
CanvasFrame canvas = new CanvasFrame("Web Cam");
13+
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
14+
15+
FrameGrabber grabber = new OpenCVFrameGrabber(0);
16+
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
17+
18+
grabber.start();
19+
Frame frame = grabber.grab();
20+
21+
IplImage img = converter.convert(frame);
22+
cvSaveImage("selfie.jpg", img);
23+
24+
canvas.showImage(converter.convert(img));
25+
26+
Thread.sleep(2000);
27+
28+
canvas.dispatchEvent(new WindowEvent(canvas, WindowEvent.WINDOW_CLOSING));
29+
}
30+
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.imagefromwebcam;
2+
3+
import java.awt.image.BufferedImage;
4+
import java.io.File;
5+
import java.io.IOException;
6+
7+
import javax.imageio.ImageIO;
8+
import javax.swing.JFrame;
9+
10+
import com.github.sarxos.webcam.Webcam;
11+
import com.github.sarxos.webcam.WebcamPanel;
12+
import com.github.sarxos.webcam.WebcamResolution;
13+
import com.github.sarxos.webcam.util.ImageUtils;
14+
15+
public class WebcamCaptureExample {
16+
17+
public static void main(String[] args) throws IOException, Exception {
18+
Webcam webcam = Webcam.getDefault();
19+
webcam.open();
20+
21+
BufferedImage image = webcam.getImage();
22+
23+
ImageIO.write(image, ImageUtils.FORMAT_JPG, new File("selfie.jpg"));
24+
25+
//WebcamUtils.capture(webcam, "selfie1.jpg");
26+
}
27+
28+
public void captureWithPanel() {
29+
Webcam webcam = Webcam.getDefault();
30+
webcam.setViewSize(WebcamResolution.VGA.getSize());
31+
32+
WebcamPanel panel = new WebcamPanel(webcam);
33+
panel.setImageSizeDisplayed(true);
34+
35+
JFrame window = new JFrame("Webcam");
36+
window.add(panel);
37+
window.setResizable(true);
38+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39+
window.pack();
40+
window.setVisible(true);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)