我想在Sarxos的帮助下为网络摄像头拍摄快照
package org.rogatio.circlead.sunray.util.websnap;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.slf4j.LoggerFactory;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
public class GuiSnap {
private static Webcam webcam = null;
public static void main(String[] args) throws IOException {
webcam = Webcam.getDefault();
if (webcam != null) {
Dimension[] dimensions = webcam.getViewSizes();
webcam.setViewSize(dimensions[dimensions.length - 1]);
webcam.open();
}
JFrame window = createFrame();
if (window != null) {
JButton button = createButton(window);
JPanel panel = createWebcamPanel();
if (panel != null) {
panel.add(button);
if (window != null) {
window.add(panel);
}
}
}
}
private static WebcamPanel createWebcamPanel() {
if (webcam == null) {
return null;
}
WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true);
panel.setDisplayDebugInfo(false);
panel.setImageSizeDisplayed(true);
panel.setMirrored(false);
return panel;
}
private static JFrame createFrame() {
if (webcam == null) {
return null;
}
final JFrame window = new JFrame("Circlead Sunray (" + webcam.getName() + ")");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
window.setSize(dim.width / 2, dim.height / 2);
window.setPreferredSize(new Dimension(dim.width / 2, dim.height / 2));
window.setLocation(dim.width / 2 - window.getSize().width / 2, dim.height / 2 - window.getSize().height / 2);
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
return window;
}
private static JButton createButton(JFrame window) {
JButton button = new JButton("Snap");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
snap();
System.exit(0);
window.dispose();
window.setVisible(false);
}
});
return button;
}
private static String snap() {
if (webcam != null) {
BufferedImage image = webcam.getImage();
String date = null;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.GERMANY);
date = format.format(new Date());
File img = new File("snap_" + date + ".png");
if (img.exists()) {
img.delete();
}
try {
ImageIO.write(image, "PNG", img);
System.out.println("" + img.toString());
return img.toString();
} catch (IOException e) {
}
}
return null;
}
}
该代码在Ubuntu 18.04下的Eclipse内部运行良好。但是当我用maven编译并以
开头时java -jar circlead-websnap-util-1.1.8-SNAPSHOT-jar-with-dependencies.jar
那我只得到一个没有任何东西的白色窗户。日志显示没有错误,我不知道从命令行运行它时如何获得工作版本。有提示吗?
这是我的行家
<?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>
<groupId>org.rogatio.circlead.sunray</groupId>
<artifactId>circlead-websnap-util</artifactId>
<name>Circlead :: Websnap :: Util</name>
<version>1.1.8-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java-compiler.version>1.8</java-compiler.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>0.3.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<inherited>true</inherited>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java-compiler.version}</source>
<target>${java-compiler.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>org.rogatio.circlead.sunray.util.websnap.GuiSnap</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>