macos 在程序中使用 mac 相机的示例代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sample code for using mac camera in a program?
提问by Nick Fortescue
I'd like to use the camera in my Macbook in a program. I'm fairly language agnostic - C, Java, Python etc are all fine. Could anyone suggest the best place to look for documents or "Hello world" type code?
我想在程序中使用我的 Macbook 中的相机。我对语言相当不可知——C、Java、Python 等都很好。任何人都可以建议查找文档或“Hello world”类型代码的最佳位置吗?
采纳答案by Chris Hanson
The ImageKit framework in Leopard has an IKPictureTaker
class that will let you run the standard picture-taking sheet or panel that you seen in iChat and other applications.
Leopard 中的 ImageKit 框架有一个IKPictureTaker
类,可以让您运行在 iChat 和其他应用程序中看到的标准图片拍摄表或面板。
If you don't want to use the standard picture-taker panel/sheet interface, you an use the QTKit Capturefunctionality to get an image from the iSight.
如果您不想使用标准的拍照面板/工作表界面,您可以使用QTKit Capture功能从 iSight 获取图像。
Both of these will require writing some Cocoa code in Objective-C, but that shouldn't really be an obstacle these days.
这两者都需要在 Objective-C 中编写一些 Cocoa 代码,但现在这不应该成为真正的障碍。
回答by millenomi
If you want to manipulate the camera directly from your code, you must use the QuickTime Capture APIs or the Cocoa QTKit Capturewrapper (much better).
如果您想直接从您的代码操作相机,您必须使用 QuickTime Capture API 或Cocoa QTKit Capture包装器(更好)。
The only caveat is: if you use a QTCaptureDecompressedVideoOutput, remember that the callbacks aren't made on the main thread, but on the QuickTIme-managed capture thread. Use [someObject performSelectorOnMainThread:... withObject:... waitUntilDone:NO] to send messages to an object on the main thread.
唯一的警告是:如果您使用 QTCaptureDecompressedVideoOutput,请记住回调不是在主线程上进行的,而是在 QuickTIme 管理的捕获线程上进行的。使用 [someObject performSelectorOnMainThread:... withObject:... waitUntilDone:NO] 向主线程上的对象发送消息。
回答by Derek
There is a utility called isightcapture that runs from the unix command line that takes a picture from the isight camera and saves it.
有一个名为 isightcapture 的实用程序,它从 unix 命令行运行,它从 isight 相机拍摄照片并保存它。
You can check it out at this web site: http://www.macupdate.com/info.php/id/18598
你可以在这个网站上查看:http: //www.macupdate.com/info.php/id/18598
An example of using this with AppleScript is:
在 AppleScript 中使用它的一个例子是:
tell application "Terminal" do script "/Applications/isightcapture myimage.jpg" end tell
告诉应用程序“终端”执行脚本“/Applications/isightcapture myimage.jpg”结束告诉
回答by meduz
From a related question which specifically asked the solution to be pythonic, you should give a try to motmot's camifacelibrary from Andrew Straw. It also works with firewire cameras, but it works also with the isight, which is what you are looking for.
从一个专门要求解决方案为pythonic的相关问题中,您应该尝试一下来自 Andrew Straw 的motmot 的 camiface库。它也适用于火线相机,但它也适用于您正在寻找的 isight。
From the tutorial:
从教程:
import motmot.cam_iface.cam_iface_ctypes as cam_iface
import numpy as np
mode_num = 0
device_num = 0
num_buffers = 32
cam = cam_iface.Camera(device_num,num_buffers,mode_num)
cam.start_camera()
frame = np.asarray(cam.grab_next_frame_blocking())
print 'grabbed frame with shape %s'%(frame.shape,)
It is used in this sample neuroscience demo
它用于此示例神经科学演示
回答by meduz
Another solution is OpenCV+pythonwith a script like:
另一种解决方案是OpenCV+python,其脚本如下:
import cv
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
Cannot do any simpler!
不能再简单了!
回答by Ken
Quartz Composer is also a pleasant way to capture and work with video, when it's applicable. There's a video input patch.
如果适用,Quartz Composer 也是捕获和处理视频的一种愉快方式。有一个视频输入补丁。
Quartz Composer is a visual programming environment that integrates into a larger Cocoa program if need be.
Quartz Composer 是一个可视化编程环境,如果需要,它可以集成到更大的 Cocoa 程序中。
http://developer.apple.com/graphicsimaging/quartz/quartzcomposer.html
http://developer.apple.com/graphicsimaging/quartz/quartzcomposer.html