Merge pull request #10900 from SmartyAnsh/BAEL-4239_Capturing-Image-from-webcam-in-java

Bael 4239 capturing image from webcam in java
This commit is contained in:
Eric Martin 2021-06-18 11:43:58 -05:00 committed by GitHub
commit d8f657667e
4 changed files with 116 additions and 0 deletions

View File

@ -82,6 +82,17 @@
<artifactId>MarvinPlugins</artifactId>
<version>${marvin-version}</version>
</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>
<properties>
@ -91,6 +102,8 @@
<tess4j.version>4.5.1</tess4j.version>
<tesseract-platform.version>4.1.0-1.5.2</tesseract-platform.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>
<thumbnailator-version>0.4.11</thumbnailator-version>
<marvin-version>1.5.5</marvin-version>

View File

@ -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 imagePanel = new MarvinImagePanel();
imagePanel.setImage(image);
imagePanel.setSize(800,600);
imagePanel.setVisible(true);
}
}

View File

@ -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(frame);
Thread.sleep(2000);
canvas.dispatchEvent(new WindowEvent(canvas, WindowEvent.WINDOW_CLOSING));
}
}

View File

@ -0,0 +1,41 @@
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"));
}
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);
}
}