From a6bb46b8f568abc563ef905629fe9c568753f386 Mon Sep 17 00:00:00 2001 From: JianGuo Date: Sat, 8 Feb 2025 15:05:08 +0800 Subject: [PATCH] =?UTF-8?q?build(pom.xml):=20=E6=9B=B4=E6=96=B0=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=89=88=E6=9C=AC=E5=B9=B6=E4=BC=98=E5=8C=96=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将项目版本从 0.1.2 升级到 0.1.4 - 移除了 humble-video-all 依赖 - 添加了 maven-dependency-plugin 以复制依赖库到 target/lib 目录 - 更新了 maven-assembly-plugin 配置,设置最终名称为 sane-service-v${project.version} - 添加了主类配置项,指定为 org.aohe.Main refactor(webcam): 重构 WebCam 类 - 删除了 WebCam 类的主体逻辑 -保留了 convertToType 方法 - 添加了 RecordAndEncodeVideo 类,实现了屏幕录制功能 fix(show): 修复系统托盘图标加载问题 - 修改了 TrayFrameUtf8 类,使用 UrlResource 加载托盘图标 - 优化了资源加载路径 --- pom.xml | 58 ++-- .../java/org/aohe/show/TrayFrameUtf8.java | 15 +- .../org/aohe/webcam/RecordAndEncodeVideo.java | 278 ++++++++++++++++++ src/main/java/org/aohe/webcam/WebCam.java | 198 ++++++------- 4 files changed, 427 insertions(+), 122 deletions(-) create mode 100644 src/main/java/org/aohe/webcam/RecordAndEncodeVideo.java diff --git a/pom.xml b/pom.xml index 4d35196..3db64c6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.aohe twain-service - 0.1.2 + 0.1.4 8 @@ -69,16 +69,7 @@ webcam-capture-driver-native 1.2.0-8 - - commons-cli - commons-cli - 1.4 - - - io.humble - humble-video-all - 0.3.0 - + com.github.sarxos webcam-capture @@ -146,6 +137,7 @@ + sane-service-v${project.version} org.apache.maven.plugins @@ -156,16 +148,42 @@ - org.apache.maven.plugins - maven-assembly-plugin - 2.4.1 - - - - jar-with-dependencies - - + maven-dependency-plugin + + + package + + copy-dependencies + + + ${project.build.directory}/lib + + + + + maven-assembly-plugin + + ${project.build.finalName} + + + org.aohe.Main + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + org.apache.maven.plugins diff --git a/src/main/java/org/aohe/show/TrayFrameUtf8.java b/src/main/java/org/aohe/show/TrayFrameUtf8.java index 5a4ad6f..1bda429 100644 --- a/src/main/java/org/aohe/show/TrayFrameUtf8.java +++ b/src/main/java/org/aohe/show/TrayFrameUtf8.java @@ -1,16 +1,23 @@ package org.aohe.show; import cn.hutool.core.io.resource.ResourceUtil; +import cn.hutool.core.io.resource.UrlResource; +import com.github.sarxos.webcam.util.ImageUtils; import lombok.extern.slf4j.Slf4j; import org.aohe.constant.SocketEnum; import org.aohe.web.SocketUtils; +import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; +import java.net.URL; import java.nio.charset.StandardCharsets; /** @@ -19,7 +26,7 @@ import java.nio.charset.StandardCharsets; @Slf4j public class TrayFrameUtf8 { - public static void initSystemTrayUTF8() { + public static void initSystemTrayUTF8() throws IOException { SocketUtils.start(SocketEnum.SCAN_SOCKET); //使用 JDialog 作为 JPopupMenu 载体 JDialog jDialog = new JDialog(); @@ -83,8 +90,10 @@ public class TrayFrameUtf8 { jPopupMenu.add(showMainFrame); jPopupMenu.add(exit); - - Image image = Toolkit.getDefaultToolkit().getImage(ResourceUtil.getResource("ah.png").getFile()); + URL url = ResourceUtil.getResource("../res/ah.png"); + String trayPng = "../res/ah.png"; + log.info("file:{}", url.getFile()); + Image image = Toolkit.getDefaultToolkit().getImage(url.getFile()); // 创建系统托盘图标 TrayIcon trayIcon = new TrayIcon(image, "Scan Program For AH"); // 自动调整系统托盘图标大小 diff --git a/src/main/java/org/aohe/webcam/RecordAndEncodeVideo.java b/src/main/java/org/aohe/webcam/RecordAndEncodeVideo.java new file mode 100644 index 0000000..1dd8d1e --- /dev/null +++ b/src/main/java/org/aohe/webcam/RecordAndEncodeVideo.java @@ -0,0 +1,278 @@ +//package org.aohe.webcam; +///******************************************************************************* +// * 版权所有 (c) 2014, Art Clarke. 保留所有权利。 +// * +// * 本文件是Humble-Video的一部分。 +// * +// * Humble-Video 是自由软件:您可以根据自由软件基金会发布的GNU Affero通用公共许可证的条款重新分发和/或修改它, +// * 许可证版本为3,或(由您选择)任何更新的版本。 +// * +// * Humble-Video 按“原样”分发,没有任何形式的担保;甚至没有默示的适销性或特定用途适用性的保证。 +// * 有关详细信息,请参阅GNU Affero通用公共许可证。 +// * +// * 您应该已经收到了一份GNU Affero通用公共许可证的副本。如果没有,请参见。 +// *******************************************************************************/ +//import java.awt.*; +//import java.awt.image.BufferedImage; +//import java.io.IOException; +//import java.nio.ByteBuffer; +// +//import com.github.eduramiba.webcamcapture.drivers.NativeDriver; +//import com.github.sarxos.webcam.Webcam; +//import com.github.sarxos.webcam.WebcamResolution; +//import io.humble.ferry.Buffer; +//import org.aohe.constant.SocketEnum; +//import org.aohe.web.SocketUtils; +//import org.apache.commons.cli.CommandLine; +//import org.apache.commons.cli.CommandLineParser; +//import org.apache.commons.cli.HelpFormatter; +//import org.apache.commons.cli.OptionBuilder; +//import org.apache.commons.cli.Options; +//import org.apache.commons.cli.ParseException; +// +//import io.humble.video.Codec; +//import io.humble.video.Encoder; +//import io.humble.video.MediaAudio; +//import io.humble.video.MediaPacket; +//import io.humble.video.MediaPicture; +//import io.humble.video.Muxer; +//import io.humble.video.MuxerFormat; +//import io.humble.video.PixelFormat; +//import io.humble.video.Rational; +//import io.humble.video.awt.MediaPictureConverter; +//import io.humble.video.awt.MediaPictureConverterFactory; +// +///** +// * 将计算机屏幕内容录制到媒体文件中,持续指定的时间。 +// * 这是一个演示程序,旨在教授如何使用Humble API。 +// *

+// * 引入的概念: +// *

+// *
    +// *
  • Muxer: {@link Muxer} 对象是可以写入媒体数据的容器。
  • +// *
  • 编码器: {@link Encoder} 对象可以将 {@link MediaAudio} 或 {@link MediaPicture} 对象转换为 {@link MediaPacket} 对象, +// * 以便将其写入 {@link Muxer} 对象。
  • +// *
+// * +// *

+// * 使用Maven运行此程序: +// *

+// *
+// * mvn install exec:java -Dexec.mainClass="io.humble.video.demos.RecordAndEncodeVideo" -Dexec.args="filename.mp4"
+// * 
+// * +// * @author aclarke +// * +// */ +//public class RecordAndEncodeVideo { +// +// /** +// * 录制屏幕内容。 +// * +// * @param filename 文件名,用于保存录制的视频。 +// * @param formatName 视频格式名称,如果未指定则根据文件名猜测。 +// * @param codecName 编解码器名称,如果未指定则根据格式猜测。 +// * @param duration 录制的持续时间(秒)。 +// * @throws AWTException 如果创建Robot对象失败。 +// * @throws InterruptedException 如果线程被中断。 +// * @throws IOException 如果发生I/O错误。 +// */ +// private static void recordScreen(String filename, String formatName, +// String codecName, int duration) throws AWTException, InterruptedException, IOException { +// SocketUtils.start(SocketEnum.CAM_SOCKET); +// Webcam.setDriver(new NativeDriver()); +// Webcam webcam = Webcam.getDefault(); +// Dimension[] viewSizes = webcam.getViewSizes(); +// Dimension bigSize = WebcamResolution.VGA.getSize(); +// for (Dimension d : viewSizes){ +// //获取最大分辨率 +// if(bigSize.getWidth()", options); +// } else { +// /** +// * 读取选项值及其默认值。 +// */ +// final int duration = Integer.parseInt(cmd.getOptionValue("duration", "10")); +// if (duration <= 0) +// throw new IllegalArgumentException("duration 必须大于 0"); +// final int snaps = Integer.parseInt(cmd.getOptionValue("snaps", "5")); +// if (snaps <= 0) +// throw new IllegalArgumentException("snaps 必须大于 0"); +// final String codecname = cmd.getOptionValue("codec"); +// final String formatname = cmd.getOptionValue("format"); +// final String filename = cmd.getArgs()[0]; +// +// recordScreen(filename, formatname, codecname, 1000); +// } +// } catch (ParseException e) { +// System.err.println("解析命令行时出现异常: " + e.getLocalizedMessage()); +// } +// } +// +// /** +// * 将任意类型的 {@link BufferedImage} 转换为指定类型的 {@link BufferedImage}。 +// * 如果源图像已经是目标类型,则返回源图像,否则创建新图像并将源图像的内容复制到新图像中。 +// * +// * @param sourceImage 要转换的图像。 +// * @param targetType 目标 BufferedImage 类型。 +// * @return 指定目标类型的 BufferedImage。 +// * @see BufferedImage +// */ +// public static BufferedImage convertToType(BufferedImage sourceImage, +// int targetType) { +// BufferedImage image; +// +// // 如果源图像是目标类型,则返回源图像 +// if (sourceImage.getType() == targetType) +// image = sourceImage; +// +// // 否则创建目标类型的新图像并绘制新图像 +// else { +// image = new BufferedImage(sourceImage.getWidth(), +// sourceImage.getHeight(), targetType); +// image.getGraphics().drawImage(sourceImage, 0, 0, null); +// } +// +// return image; +// } +//} diff --git a/src/main/java/org/aohe/webcam/WebCam.java b/src/main/java/org/aohe/webcam/WebCam.java index d72afce..49119d4 100644 --- a/src/main/java/org/aohe/webcam/WebCam.java +++ b/src/main/java/org/aohe/webcam/WebCam.java @@ -1,99 +1,99 @@ -package org.aohe.webcam; - -import com.github.sarxos.webcam.Webcam; -import com.github.sarxos.webcam.WebcamResolution; -import io.humble.video.*; -import io.humble.video.awt.MediaPictureConverter; -import io.humble.video.awt.MediaPictureConverterFactory; -import org.aohe.constant.SocketEnum; -import org.aohe.web.SocketUtils; - -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.IOException; - -public class WebCam { - - public static void main(String[] args) throws InterruptedException, IOException { - - SocketUtils.start(SocketEnum.CAM_SOCKET); - - Webcam webcam = Webcam.getDefault(); - Dimension[] viewSizes = webcam.getViewSizes(); - Dimension bigSize = WebcamResolution.VGA.getSize(); - for (Dimension d : viewSizes){ - //获取最大分辨率 - if(bigSize.getWidth()