BAEL-4329 - Capturing image from webcam in java
This commit is contained in:
parent
559d47a612
commit
4440c3e9c5
|
@ -82,6 +82,17 @@
|
||||||
<artifactId>MarvinPlugins</artifactId>
|
<artifactId>MarvinPlugins</artifactId>
|
||||||
<version>${marvin-version}</version>
|
<version>${marvin-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.bytedeco</groupId>
|
||||||
|
<artifactId>javacv-platform</artifactId>
|
||||||
|
<version>${javacv-platform.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.sarxos</groupId>
|
||||||
|
<artifactId>webcam-capture</artifactId>
|
||||||
|
<version>${webcam-capture.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -91,6 +102,8 @@
|
||||||
<tess4j.version>4.5.1</tess4j.version>
|
<tess4j.version>4.5.1</tess4j.version>
|
||||||
<tesseract-platform.version>4.1.0-1.5.2</tesseract-platform.version>
|
<tesseract-platform.version>4.1.0-1.5.2</tesseract-platform.version>
|
||||||
<opencv.version>3.4.2-0</opencv.version>
|
<opencv.version>3.4.2-0</opencv.version>
|
||||||
|
<javacv-platform.version>1.5.5</javacv-platform.version>
|
||||||
|
<webcam-capture.version>0.3.12</webcam-capture.version>
|
||||||
<imgscalr-version>4.2</imgscalr-version>
|
<imgscalr-version>4.2</imgscalr-version>
|
||||||
<thumbnailator-version>0.4.11</thumbnailator-version>
|
<thumbnailator-version>0.4.11</thumbnailator-version>
|
||||||
<marvin-version>1.5.5</marvin-version>
|
<marvin-version>1.5.5</marvin-version>
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.imagefromwebcam;
|
||||||
|
|
||||||
|
import marvin.gui.MarvinImagePanel;
|
||||||
|
import marvin.image.MarvinImage;
|
||||||
|
import marvin.io.MarvinImageIO;
|
||||||
|
import marvin.video.MarvinJavaCVAdapter;
|
||||||
|
import marvin.video.MarvinVideoInterface;
|
||||||
|
import marvin.video.MarvinVideoInterfaceException;
|
||||||
|
|
||||||
|
public class MarvinExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws MarvinVideoInterfaceException {
|
||||||
|
MarvinVideoInterface videoAdapter = new MarvinJavaCVAdapter();
|
||||||
|
videoAdapter.connect(0);
|
||||||
|
MarvinImage image = videoAdapter.getFrame();
|
||||||
|
MarvinImageIO.saveImage(image, "selfie.jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void captureWithPanel() throws MarvinVideoInterfaceException {
|
||||||
|
MarvinVideoInterface videoAdapter = new MarvinJavaCVAdapter();
|
||||||
|
videoAdapter.connect(0);
|
||||||
|
MarvinImage image = videoAdapter.getFrame();
|
||||||
|
|
||||||
|
MarvinImagePanel videoPanel = new MarvinImagePanel();
|
||||||
|
videoPanel.setImage(image);
|
||||||
|
|
||||||
|
videoPanel.setSize(800,600);
|
||||||
|
videoPanel.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.imagefromwebcam;
|
||||||
|
|
||||||
|
import org.bytedeco.javacv.*;
|
||||||
|
import org.bytedeco.opencv.opencv_core.IplImage;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import static org.bytedeco.opencv.helper.opencv_imgcodecs.cvSaveImage;
|
||||||
|
|
||||||
|
public class OpenCVExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
CanvasFrame canvas = new CanvasFrame("Web Cam");
|
||||||
|
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
FrameGrabber grabber = new OpenCVFrameGrabber(0);
|
||||||
|
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
|
||||||
|
|
||||||
|
grabber.start();
|
||||||
|
Frame frame = grabber.grab();
|
||||||
|
|
||||||
|
IplImage img = converter.convert(frame);
|
||||||
|
cvSaveImage("selfie.jpg", img);
|
||||||
|
|
||||||
|
canvas.showImage(converter.convert(img));
|
||||||
|
|
||||||
|
Thread.sleep(2000);
|
||||||
|
|
||||||
|
canvas.dispatchEvent(new WindowEvent(canvas, WindowEvent.WINDOW_CLOSING));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.imagefromwebcam;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
import com.github.sarxos.webcam.Webcam;
|
||||||
|
import com.github.sarxos.webcam.WebcamPanel;
|
||||||
|
import com.github.sarxos.webcam.WebcamResolution;
|
||||||
|
import com.github.sarxos.webcam.util.ImageUtils;
|
||||||
|
|
||||||
|
public class WebcamCaptureExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException, Exception {
|
||||||
|
Webcam webcam = Webcam.getDefault();
|
||||||
|
webcam.open();
|
||||||
|
|
||||||
|
BufferedImage image = webcam.getImage();
|
||||||
|
|
||||||
|
ImageIO.write(image, ImageUtils.FORMAT_JPG, new File("selfie.jpg"));
|
||||||
|
|
||||||
|
//WebcamUtils.capture(webcam, "selfie1.jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void captureWithPanel() {
|
||||||
|
Webcam webcam = Webcam.getDefault();
|
||||||
|
webcam.setViewSize(WebcamResolution.VGA.getSize());
|
||||||
|
|
||||||
|
WebcamPanel panel = new WebcamPanel(webcam);
|
||||||
|
panel.setImageSizeDisplayed(true);
|
||||||
|
|
||||||
|
JFrame window = new JFrame("Webcam");
|
||||||
|
window.add(panel);
|
||||||
|
window.setResizable(true);
|
||||||
|
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
window.pack();
|
||||||
|
window.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue