AM335X——USB摄像头

CSDN仅用于增加百度收录权重,排版未优化,日常不维护。请访问:www.hceng.cn 查看、评论。
本博文对应地址: https://hceng.cn/2019/06/23/AM335X——USB摄像头/#more

记录使用AM335X,在LCD和Web上显示USB摄像头图像。

之前在Tiny4412上学习了UVC,并在Tiny4412的LCD上显示了USB摄像头图像。
最近需要在AM335X上,实现USB摄像头图像在LCD和Web上显示,简单记录一下。

1.准备工作

1.1 修改UVC驱动

参考之前博客博客Linux摄像头驱动2——UVC_2.2移植内核摄像头驱动里,修改drivers/media/usb/uvc/uvc_driver.cdrivers/media/usb/uvc/uvc_video.c

1.2 添加UVC驱动

在内核里,添加UVC驱动

Device Drivers  ---> 
    <*> Multimedia support  --->
        [*]   Media USB Adapters  --->
            <*>   USB Video Class (UVC)

重新编译内核,烧写。

2.LCD显示

参考之前的博客Linux摄像头驱动3——LCD显示,直接使用代码,交叉编译得到LCD显示程序video2lcd

video2lcd拷贝到AM335X里运行,发现提示报错:

Wrong JPEG library version: library is 92, caller expects 62

video2lcd中的mjpeg2rgb.c会用到libjpeg库,程序里请求的是62版本的,而AM335x里比较新,为92版本,提示不兼容。
看了下LCD显示程序的源码,源码里没看到哪里对版本需求的定义,决定暂时先重新移植62版的libjpeg库。

先下载jpegsrc.v6b.tar.gz,编译该源码会用到libtool-1.5.22.tar.gz,也要下载。

  • 交叉编译libtool-1.5.22
    1、因为是交叉编译,需要先创建安装路径,让生成的文件保存在该目录,避免覆盖主机的相关文件,mkdir /work/out_libtool
    2、解压源码,tar xf libtool-1.5.22.tar.gz
    3、进入解压目录,设置配置文件,./configure --host=arm-linux-gnueabihf --prefix=/work/out_libtool CC=arm-linux-gnueabihf-gcc
    4、交叉编译、安装,make && make install

  • 交叉编译jpegsrc.v6b
    1、先创建安装路径,mkdir /work/out_jpeg
    2、解压源码,tar xf libtool-1.5.22.tar.gz
    3、进入解压目录,设置配置文件,./configure --host=arm-linux-gnueabihf --prefix=/work/out_jpeg CC=arm-linux-gnueabihf-gcc --enable-shared --enable-static
    4、该程序无法自动生成一些目录,手动创建以下四个目录:/work/out_jpeg/man/man1/work/out_jpeg/include/work/out_jpeg/lib/work/out_jpeg/bin
    5、将前面libtool-1.5.22生成的libtoollibtoolize复制到源码根目录,cp /work/libtool/libtool /work/jpeg-6b/cp work/libtool/libtoolize /work/jpeg-6b/
    6、交叉编译、安装,make && make install

/work/out_jpeg/lib会生成如下文件:

libjpeg.so  libjpeg.so.62  libjpeg.so.62.0.0

在AM335x里,执行rm /usr/lib/libjpeg*,删除AM335x的libjpeg*相关文件,复制前面生成的文件到该目录下,因为原来/usr/lib/下还有/usr/lib/libjpeg.so.8libjpeg.so.9,其它程序(后面Web)会用到,还需要执行ln -sf /usr/lib/libjpeg.so.62 libjpeg.so.8ln -sf /usr/lib/libjpeg.so.62 libjpeg.so.9,让libjpeg.so.8libjpeg.so.9软链接到libjpeg.so.62,此时/usr/lib/libjpeg*如下:
{% codeblock lang:shell %}

ls /usr/lib/libjpeg* -lh

-rwxr-xr-x 1 root root 134.3K Jan 1 1970 /usr/lib/libjpeg.so
-rwxr-xr-x 1 root root 134.3K Jan 1 1970 /usr/lib/libjpeg.so.62
-rwxr-xr-x 1 root root 134.3K Jan 1 1970 /usr/lib/libjpeg.so.62.0.0
lrwxrwxrwx 1 root root 22 Jan 1 00:06 /usr/lib/libjpeg.so.8 -> /usr/lib/libjpeg.so.62
lrwxrwxrwx 1 root root 22 Jan 1 00:07 /usr/lib/libjpeg.so.9 -> /usr/lib/libjpeg.so.62
{% endcodeblock %}

此时再执行video2lcd -v /dev/video0 -d /dev/fb0即可在LCD上看到USB摄像头图像。

3.Web显示

MJPG-streamer是一个优秀的开源项目,它可以通过HTTP的方式访问Linux上面的兼容摄像头,从而做到远程视频传输的效果。

Buildroot里添加mjpg-streamer:

Target packages  --->
    Networking applications  --->
        [*] mjpg-streamer

重新烧写根文件系统,执行以下命令启动服务:

mjpg_streamer -i "/usr/lib/input_uvc.so -d /dev/video0 -r 640x480 -f 30 -q 90 -n" -o "/usr/lib/output_http.so -w /usr/www"

打开浏览器,输入AM335X的IP:8080即可看到默认显示界面:

/usr/www目录下,有很多html的示例,包括静态图片、视频图像、控制等,参考html修改,实现自定义的Web界面。

如果想要外网访问,参考之前博客黑群辉_3.2.2 反向代理frp尝试外网访问。

最后再记录下命令参数含义。

{% codeblock lang:shell %}

mjpg_streamer --help


Usage: mjpg_streamer
-i | --input “ [parameters]”
-o | --output “ [parameters]”
[-h | --help ]…: display this help
[-v | --version ]…: display version information
[-b | --background]…: fork to the background, daemon mode

Example #1:
To open an UVC webcam “/dev/video1” and stream it via HTTP:
mjpg_streamer -i “input_uvc.so -d /dev/video1” -o “output_http.so”

Example #2:
To open an UVC webcam and stream via HTTP port 8090:
mjpg_streamer -i “input_uvc.so” -o “output_http.so -p 8090”

Example #3:
To get help for a certain input plugin:
mjpg_streamer -i “input_uvc.so --help”

In case the modules (=plugins) can not be found:

  • Set the default search path for the modules with:
    export LD_LIBRARY_PATH=/path/to/plugins,
  • or put the plugins into the “/lib/” or “/usr/lib” folder,
  • or instead of just providing the plugin file name, use a complete
    path and filename:
    mjpg_streamer -i “/path/to/modules/input_uvc.so”

{% endcodeblock %}

得知mjpg_streamer后需要接-i-o两个参数,再分别看这两个参数提供的不同so插件文件支持哪些选项。
可选的-h查询帮助信息、-v查询版本信息、-b后台执行就不多介绍了。

输入so插件包含:input_uvc.soinput_testpicture.so、(input_file.soinput_ptp2.soinput_http.so)
输出so插件包含:output_http.so、(output_file.sooutput_udp.sooutput_rtsp.sooutput_viewer.so)
其中括号部分,我这个Buildrootmjpg_streamer版本没有提供。

  • 输入插件1:input_uvc.so
    {% codeblock lang:shell %}

mjpg_streamer -i “/usr/lib/input_uvc.so --help”

MJPG Streamer Version: git rev: fd184d0

Help for input plugin…: UVC webcam grabber

The following parameters can be passed to this plugin:

[-d | --device ]…: video device to open (your camera)
[-r | --resolution ]…: the resolution of the video device,
can be one of the following strings:
QQVGA QCIF CGA QVGA CIF PAL
VGA SVGA XGA HD SXGA UXGA
FHD
or a custom value like the following
example: 640x480
[-f | --fps ]…: frames per second
(activates YUYV format, disables MJPEG)
[-q | --quality ] …: set quality of JPEG encoding
[-m | --minimum_size ].: drop frames smaller then this limit, useful
if the webcam produces small-sized garbage frames
may happen under low light conditions
[-e | --every_frame ]…: drop all frames except numbered
[-n | --no_dynctrl ]…: do not initalize dynctrls of Linux-UVC driver
[-l | --led ]…: switch the LED “on”, “off”, let it “blink” or leave
it up to the driver using the value “auto”
[-t | --tvnorm ] …: set TV-Norm pal, ntsc or secam
[-u | --uyvy ] …: Use UYVY format, default: MJPEG (uses more cpu power)
[-y | --yuv ] …: Use YUV format, default: MJPEG (uses more cpu power)
[-fourcc ] …: Use FOURCC codec ‘argopt’,
currently supported codecs are: RGBP

Optional parameters (may not be supported by all cameras):

[-br ]…: Set image brightness (auto or integer)
[-co ]…: Set image contrast (integer)
[-sh ]…: Set image sharpness (integer)
[-sa ]…: Set image saturation (integer)
[-cb ]…: Set color balance (auto or integer)
[-wb ]…: Set white balance (auto or integer)
[-ex ]…: Set exposure (auto, shutter-priority, aperature-priority, or integer)
[-bk ]…: Set backlight compensation (integer)
[-rot ]…: Set image rotation (0-359)
[-hf ]…: Set horizontal flip (true/false)
[-vf ]…: Set vertical flip (true/false)
[-pl ]…: Set power line filter (disabled, 50hz, 60hz, auto)
[-gain ]…: Set gain (auto or integer)
[-cagc ]…: Set chroma gain control (auto or integer)

input_init() return value signals to exit
{% endcodeblock %}

基本参数:
-d:指定video设备节点
-r:指定分辨率,VGA或640x480类似值
-f:指定每秒帧数
-q:指定JPEG编码质量
-m:指定丢弃小于该值的帧
-e:删除除编号以外的所有帧
-n:不要初始化Linux-UVC驱动程序的dynctrls
-l:设置LED on/off/blink,设置auto将取决于驱动程序
-t:设置TV-Norm pal,ntsc或secam
-u:指定输入为UYVY格式,通过CPU将UYVC转为MJPEG,这样会消耗更多CPU
-v:指定输入为YUV格式,通过CPU将YUV转为MJPEG,这样会消耗更多CPU
-fourcc:使用FOURCC编解码器argopt,目前支持的编解码器是:RGBP
新版新增:
-timestamp:使用系统时间填充帧时间戳
-softfps:丢帧以尝试实现此FPS

图像参数:
-br:设置图像亮度
-co:设置图像对比度
-sh:设置图像清晰度
-sa:设置图像饱和度
-cb:设置图像颜色平衡
-wb:设置图像白平衡
-ex:设置图像曝光
-bk:设置图像曝光补偿
-rot:设置图像旋转0-359
-hf:设置图像水平翻转
-vf:设置图像垂直翻转
-pl:设置图像避免闪烁频率
-gain:设置图像增益
-cagc:设置图像色度增益控制

  • 输入插件2:input_testpicture.so
    {% codeblock lang:shell %}

mjpg_streamer -i “/usr/lib/input_testpicture.so --help”

MJPG Streamer Version: git rev: fd184d0

Help for input plugin…: TESTPICTURE input plugin

The following parameters can be passed to this plugin:

[-d | --delay ]…: delay to pause between frames
[-r | --resolution]…: can be 960x720, 640x480, 320x240, 160x120

input_init() return value signals to exit
{% endcodeblock %}

基本参数:
-d:指定在两帧之间的延时
-r:指定在分辨率

  • 输入插件3:input_file.so
    {% codeblock lang:shell %}

mjpg-streamer -i “/snap/mjpg-streamer/42/lib/mjpg-streamer/input_file.so --help”

MJPG Streamer Version: git rev: f387bb44e6c087271b763b27da998bf2e06c4f5d

Help for input plugin…: FILE input plugin

The following parameters can be passed to this plugin:

[-d | --delay ]…: delay (in seconds) to pause between frames
[-f | --folder ]…: folder to watch for new JPEG files
[-r | --remove ]…: remove/delete JPEG file after reading
[-n | --name ]…: ignore changes unless filename matches
[-e | --existing ]…: serve the existing *.jpg files from the specified directory

input_init() return value signals to exit
{% endcodeblock %}

基本参数:
-d:指定在两帧之间的延时(单位:s)
-f:指定查看新JEPG文件的目录
-r:指定读取后要删除的JEPG文件
-n:指定除非文件名匹配忽略更改
-e:指定目录读取现有JPG文件

  • 输入插件4:input_ptp2.so
    {% codeblock lang:shell %}

mjpg-streamer -i “/snap/mjpg-streamer/42/lib/mjpg-streamer/input_ptp2.so --help”

MJPG Streamer Version: git rev: f387bb44e6c087271b763b27da998bf2e06c4f5d
PTP2 input plugin: invalid option – ‘-’

Help for input plugin…: PTP2 input plugin

The following parameters can be passed to this plugin:

[-h ]…: print this help
[-u X ]…: delay between frames in us (default 0)
[-d X ]…: camera address in [usb:xxx,yyy] form; use
gphoto2 --auto-detect to get a list of
available cameras

input_init() return value signals to exit
{% endcodeblock %}

基本参数:
-h:打印帮助
-u:指定在两帧之间的延时(单位:us)
-d:指定可用相机

  • 输入插件5:input_http.so
    {% codeblock lang:shell %}

mjpg-streamer -i “/snap/mjpg-streamer/42/lib/mjpg-streamer/input_http.so --help”"

MJPG Streamer Version: git rev: f387bb44e6c087271b763b27da998bf2e06c4f5d

Help for input plugin…: HTTP Input plugin

The following parameters can be passed to this plugin:

[-v | --version ]…: current SVN Revision
[-h | --help]…: show this message
[-H | --host]…: select host to data from, localhost is default
[-p | --port]…: port, defaults to 8080

input_init() return value signals to exit
{% endcodeblock %}

基本参数:
-v:打印版本信息
-h:打印帮助信息
-H:指定主机地址
-p:指定端口

  • 输出插件1:output_http.so
    {% codeblock lang:shell %}

mjpg_streamer -o “/usr/lib/output_http.so --help”

MJPG Streamer Version: git rev: fd184d0

Help for output plugin…: HTTP output plugin

The following parameters can be passed to this plugin:

[-w | --www ]…: folder that contains webpages in
flat hierarchy (no subfolders)
[-p | --port ]…: TCP port for this HTTP server
[-l ] --listen ]…: Listen on Hostname / IP
[-c | --credentials ]…: ask for “username:password” on connect
[-n | --nocommands ]…: disable execution of commands

output_init() return value signals to exit
{% endcodeblock %}

基本参数:
-w:指定网页根目录(不支持子文件夹)
-p:指定HTTP服务器的TCP端口
-l:指定监听主机IP
-c:指定在连接时询问的账户名、密码
-n:指定禁用命令执行

  • 输出插件2:output_file.so
    {% codeblock lang:shell %}

mjpg-streamer -o “/snap/mjpg-streamer/42/lib/mjpg-streamer/output_file.so --help”

MJPG Streamer Version: git rev: f387bb44e6c087271b763b27da998bf2e06c4f5d

Help for output plugin…: FILE output plugin

The following parameters can be passed to this plugin:

[-f | --folder ]…: folder to save pictures
[-m | --mjpeg ]…: save the frames to an mjpg file
[-l | --link ]…: link the last picture in ringbuffer as this fixed named file
[-d | --delay ]…: delay after saving pictures in ms
[-i | --input ]…: read frames from the specified input plugin
The following arguments are takes effect only if the current mode is not MJPG
[-s | --size ]…: size of ring buffer (max number of pictures to hold)
[-e | --exceed ]…: allow ringbuffer to exceed limit by this amount
[-c | --command ]…: execute command after saving picture

output_init() return value signals to exit
{% endcodeblock %}

基本参数:
-f:指定保存图片文件夹
-m:将帧保存的mjpg文件夹
-l:将ringbuffer中的最后一张图片链接为此固定命名文件
-d:保存图片后延时(单位:ms)
-i:从指定的输入插件中读取帧
仅当当前模式不是MJPG时,以下参数才会生效
-s:环形缓冲区的大小(要保留的最大图片数)
-e:允许ringbuffer超过此数量的限制
-c:保存图片后执行命令

  • 输出插件3:output_udp.so
    {% codeblock lang:shell %}

mjpg-streamer -o “/snap/mjpg-streamer/42/lib/mjpg-streamer/output_udp.so --help”

MJPG Streamer Version: git rev: f387bb44e6c087271b763b27da998bf2e06c4f5d

Help for output plugin…: UDP output plugin

The following parameters can be passed to this plugin:

[-f | --folder ]…: folder to save pictures
[-d | --delay ]…: delay after saving pictures in ms
[-c | --command ]…: execute command after saveing picture
[-p | --port ]…: UDP port to listen for picture requests. UDP message is the filename to save

[-i | --input ]…: read frames from the specified input plugin (first input plugin between the arguments is the 0th)


output_init() return value signals to exit
{% endcodeblock %}

基本参数:
-f:指定保存图片的文件夹
-d:保存图片后延时(单位:ms)
-c:保存图片后执行命令
-p:指定UDP端口监听图片请求
-i:从指定的输入插件读取帧

  • 输出插件4:output_rtsp.so
    {% codeblock lang:shell %}

mjpg-streamer -o “/snap/mjpg-streamer/42/lib/mjpg-streamer/output_rtsp.so --help”

MJPG Streamer Version: git rev: f387bb44e6c087271b763b27da998bf2e06c4f5d

Help for output plugin…: RTSP output plugin

The following parameters can be passed to this plugin:

[-p | --port ]…: UDP port to listen for picture requests. UDP message is the filename to save

[-i | --input ]…: read frames from the specified input plugin (first input plugin between the arguments is the 0th)


output_init() return value signals to exit
{% endcodeblock %}

基本参数:
-p:指定UDP端口监听图片请求
-i:从指定的输入插件读取帧

  • 输出插件5:output_viewer.so
    {% codeblock lang:shell %}

mjpg-streamer -o “/snap/mjpg-streamer/42/lib/mjpg-streamer/output_viewer.so --help”

MJPG Streamer Version: git rev: f387bb44e6c087271b763b27da998bf2e06c4f5d

Help for output plugin…: VIEWER output plugin

output_init() return value signals to exit
{% endcodeblock %}

基本参数:

你可能感兴趣的:(Linux应用,嵌入式基础)