TkXCam

DDG - 2020-04-16: Below a simple example to use your computers webcam into a Tcl/Tk application on Linux/Unix with X-Windows using the mplayer. I got the webcam commandline option which works on my Laptop from this site: https://www.geekyhacker.com/2013/08/31/how-to-use-mplayer-to-display-your-webcam/

IMPLEMENTATION

package require Tk
package provide tkxcam 0.1

namespace eval tkxcam {} 

proc tkxcam::tkxcam {path} {
    variable fid
    set frame [frame $path -container true]
    if {[auto_execok mplayer] eq ""} {
        error "mplayer must be installed"
    }
    
    set fid [open "|mplayer -zoom -slave -idle -fstype fullscreen -wid [winfo id $path]  tv:// -tv driver=v4l2:width=800:height=600:device=/dev/video0:fps=30:outfmt=yuy2" r+]
    fconfigure $fid -blocking 0 -buffering line
    fileevent $fid readable [list [namespace current]::ReadPipe $fid]
    bind $path <Destroy> [list [namespace current]::command stop]
    return $path
}

proc ::tkxcam::ReadPipe {chan} {
    variable fid
    set d [read $chan]
    if {[eof $chan]} {
        fileevent $chan readable {}
        close $chan
        set fid ""
    }
}
proc ::tkxcam::command {cmd} {
    variable fid
    if {$fid ne ""} {
        puts $fid $cmd
        flush $fid
    }
}

And here an usage example:

if {[llength $argv] > 0 && [lindex $argv 0] eq [info script]}
    tkxcam::tkxcam .cam
    pack .cam -side left -fill both -expand true
}

SEE ALSO

- SnitMPlayer

DISCUSSION

Please discuss here ...