forked from gabrield/v4l-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtk-v4l2.lua
executable file
·113 lines (77 loc) · 2.26 KB
/
gtk-v4l2.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/env lua
--[[
This example depends on the lgob (a set of bindings of GObject-based libraries, like GTK+ and WebKitGtk, and some others like Cairo, for Lua).
I've installed the 32 bits version of lgob from here: http://downloads.tuxfamily.org/oproj/bin/ubuntu32/
You have the choice to install it from the sources, found at: http://oproj.tuxfamily.org/wiki/doku.php?id=lgob
Enjoy!
]] --
require "v4l"
require('lgob.gdk')
require('lgob.gtk')
function saveimg(name, img)
file = io.open(name, "w+")
file:write("P3\n".. w .. " " .. h .."\n255\n")
for i=1,#img do
local p = a[i] .. "\n"
file:write(p)
end
file:close()
end
camera = #arg
if camera < 1 then
camera = "/dev/video0"
else
camera = arg[1]
end
dev = v4l.open(camera)
if dev < 0 then
print("camera not found")
os.exit(0)
end
w, h = v4l.widht(), v4l.height()
print(camera .. ": " ..w .. "x" .. h)
-- saveimg(a)
for i=1,3 do -- take 3 pics to get a better image
a = v4l.getframe()
end
img = "P3\n" .. w .. " " .. h .. "\n255\n" .. table.concat(a, "\n") -- formats the image to pixbuf format
-- saveimg(a)
-- img = io.open("image.ppm", "r"):read("*a")
-- print(img)
function runDialog(dialog)
dialog:run()
dialog:hide()
names = dialog:get_filenames()
file = table.concat(names)
print(file)
saveimg(file, a)
end
loader = gdk.PixbufLoader.new()
loader:write(img, img:len())
loader:close()
pixbuf = loader:get_pixbuf()
window = gtk.Window.new()
hbox = gtk.VBox.new(false, 10)
image = gtk.Image.new_from_pixbuf(pixbuf)
button = gtk.Button.new_with_label("Save")
dialog = gtk.FileChooserDialog.new("Select a name to save", window, gtk.FILE_CHOOSER_ACTION_SAVE,
"gtk-cancel", gtk.RESPONSE_CANCEL, "gtk-ok", gtk.RESPONSE_OK)
filter = gtk.FileFilter.new()
filter:add_pattern("*.ppm")
filter:set_name("PPM Images")
dialog:add_filter(filter)
dialog:set("select-multiple", true)
hbox:add(image, button)
window:add(hbox)
window:set('title', "Camera photo " .. camera .. " ".. w .. "x" .. h, 'window-position', gtk.WIN_POS_CENTER)
window:connect('delete-event', gtk.main_quit)
button:connect('clicked', runDialog, dialog)
window:show_all()
gtk.main()
window:show_all()
img = nil
a = nil
dev = v4l.close(dev);
if dev == 0 then
print("File descriptor closed: " .. dev)
end