英文:
OpenCV Frame Grabber not work on MacOS Catalina
问题
在macOS Catalina终端中输入以下命令时,会获得以下结果:
java -jar test-1.0-SNAPSHOT.jar
结果如下:
启动应用程序
zsh: 中止 java -jar test-1.0-SNAPSHOT.jar
更多细节:
我的应用程序的主要功能基于摄像头捕捉,我有一个要求是应用程序需要在Windows 10、Linux和MacOS上运行。这就是为什么我决定选择JavaCV来处理摄像头捕捉。在Windows和Linux上一切正常。然而,在MacOS上无法正常工作。为了在Mac上测试应用程序,我正在使用VirtualBox 6.1和macOS Catalina 10.15.5。在项目中,我还使用了Gradle和Kotlin。
我还检查了虚拟机是否有权限访问摄像头,并且摄像头在系统应用程序中正常工作。我怀疑应用程序由于摄像头权限问题而无法正常工作。
我还要补充说明,macOS Catalina已经更新到最新版本。
基本代码:
build.gradle:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
id 'application'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.3'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar {
manifest {
attributes "Main-Class": "App"
}
from {
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
exclude('META-INF/*.SF')
exclude('META-INF/*.DSA')
exclude('META-INF/*.RSA')
exclude('module-info.class')
}
mainClassName = 'App'
App.kt:
import org.bytedeco.javacv.OpenCVFrameGrabber
class App {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println("启动应用")
val grabber: OpenCVFrameGrabber = OpenCVFrameGrabber(0)
grabber.start()
}
}
}
为了创建可执行的JAR文件,我在Mac终端中输入以下命令:
sudo ./gradlew clean build
英文:
Short version:
when I'm type this command in macOS Catalina terminal:
java -jar test-1.0-SNAPSHOT.jar
gets the result:
Start App
zsh: abort java -jar test-1.0-SNAPSHOT.jar
More details:
My main feature in app is based on webcam capture and I have a requirement for the application to work on Windows 10, Linux and MacOS. That's why I decided to choose JavaCV to handle webcam capturning. Everything works fine on windows and linux. However, it doesn't work properly on MacOS. For testing the application on a Mac, I'm using virtual box 6.1 and macOS Catalina 10.15.5. In project I'm also using gradle and kotlin.
I also checked if I have access to the camera from virtual machine and the camera works correctly in system applications. I suspect that the application is not working due to camera permissions.
I will also add that the macOS Catalina is up to date
Basic code
build.gradle:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
id 'application'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.3'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar {
manifest {
attributes "Main-Class": "App"
}
from {
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
exclude('META-INF/*.SF')
exclude('META-INF/*.DSA')
exclude('META-INF/*.RSA')
exclude('module-info.class')
}
mainClassName = 'App'
App.kt
import org.bytedeco.javacv.OpenCVFrameGrabber
class App {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println("Start App")
val grabber: OpenCVFrameGrabber = OpenCVFrameGrabber(0);
grabber.start()
}
}
}
In order to create executable jar file, I'm type this in mac terminal:
sudo
./gradlew clean build
答案1
得分: 0
我解决了我的问题。macOS的问题在于应用程序在启动之前应该请求摄像头访问权限。macOS不将JAR文件视为应用程序。更多信息在此处。例如,当你通过IDE运行应用程序时,系统会要求你访问摄像头的权限。
英文:
I solved my problem. The problem with macOS was that the app should ask for camera access before launching it. Jar files are not treated as applications by macOS.
More info here. For example, when you running the application by IDE, system will ask you for access to the camera.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论