RV1106手把手教你:4行代码使用linux v4l2接口调用usb摄像头连续拍照保存
<div class='showpostmsg'> 本帖最后由 LitchiCheng 于 2024-3-9 21:21 编辑<p> </p>
<p>针对v4l2进行了C++库的封装,方便后面集成其他的测试中使用,仓库地址如下,有兴趣的可以clone,麻烦给个星</p>
<article data-content="[{"type":"block","id":"3060-1621846615933","name":"paragraph","data":{},"nodes":[{"type":"text","id":"p5PQ-1621846617594","leaves":[{"text":"针对v4l2进行了C++库的封装,方便后面集成其他的测试中使用,仓库地址如下,有兴趣的可以clone,麻烦给个星","marks":[]}]}],"state":{}},{"type":"block","id":"q4GM-1709986797455","name":"paragraph","data":{},"nodes":[{"type":"text","id":"AbeI-1709986797449","leaves":[{"text":"","marks":[]}]},{"type":"inline","id":"XAVo-1708767960961","name":"link","data":{"href":"https://github.com/LitchiCheng/RV1106_Linux/tree/main"},"nodes":[{"type":"text","id":"Ct0g-1708767960962","leaves":[{"text":"LitchiCheng/RV1106_Linux: Linux test for RV1106 dev board (github.com)","marks":[]}]}]},{"type":"text","id":"G58s-1708767960967","leaves":[{"text":"","marks":[]}]}],"state":{}},{"type":"block","id":"Z6Iw-1709735771021","name":"paragraph","data":{},"nodes":[{"type":"text","id":"5p66-1709735771017","leaves":[{"text":"如下为调用usb摄像头,连续拍照保存的测试代码","marks":[]}]}],"state":{}},{"type":"block","id":"j4rx-1709735831614","name":"code","data":{"wrap":false,"language":"","theme":"default"},"nodes":[{"type":"block","id":"QBtO-1709735831613","name":"code-line","data":{},"nodes":[{"type":"text","id":"LmeH-1709735831612","leaves":[{"text":"#include <iostream>","marks":[]}]}]},{"type":"block","id":"tEjh-1709735832212","name":"code-line","data":{},"nodes":[{"type":"text","id":"ZAsF-1709735832211","leaves":[{"text":"#include \"v4l2CapPicTool.h\"","marks":[]}]}]},{"type":"block","id":"wUaO-1709735832214","name":"code-line","data":{},"nodes":[{"type":"text","id":"s2lL-1709735832213","leaves":[{"text":"#include \"timecost.h\"","marks":[]}]}]},{"type":"block","id":"FPTn-1709735832216","name":"code-line","data":{},"nodes":[{"type":"text","id":"Mqrc-1709735832215","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"AoJw-1709735832218","name":"code-line","data":{},"nodes":[{"type":"text","id":"Z1UZ-1709735832217","leaves":[{"text":"int main(int argc, char const *argv[])","marks":[]}]}]},{"type":"block","id":"CN3d-1709735832220","name":"code-line","data":{},"nodes":[{"type":"text","id":"pmPK-1709735832219","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"4euL-1709735832222","name":"code-line","data":{},"nodes":[{"type":"text","id":"YSrk-1709735832221","leaves":[{"text":" v4l2CapPicTool vt(\"/dev/video0\", 1080, 960, \"jpg\");","marks":[]}]}]},{"type":"block","id":"sz8i-1709735832224","name":"code-line","data":{},"nodes":[{"type":"text","id":"5Ycb-1709735832223","leaves":[{"text":" vt.init();","marks":[]}]}]},{"type":"block","id":"55yl-1709735832226","name":"code-line","data":{},"nodes":[{"type":"text","id":"rMxJ-1709735832225","leaves":[{"text":" while (1){","marks":[]}]}]},{"type":"block","id":"zxJ6-1709735832230","name":"code-line","data":{},"nodes":[{"type":"text","id":"KSfT-1709735832229","leaves":[{"text":" usleep(100000);","marks":[]}]}]},{"type":"block","id":"KwRv-1709735832232","name":"code-line","data":{},"nodes":[{"type":"text","id":"BXoW-1709735832231","leaves":[{"text":" {","marks":[]}]}]},{"type":"block","id":"bTSS-1709735832234","name":"code-line","data":{},"nodes":[{"type":"text","id":"skkY-1709735832233","leaves":[{"text":" timecost tmp(\"capture\");","marks":[]}]}]},{"type":"block","id":"wyOa-1709735832236","name":"code-line","data":{},"nodes":[{"type":"text","id":"Yrry-1709735832235","leaves":[{"text":" vt.capture();","marks":[]}]}]},{"type":"block","id":"Wyhh-1709735832238","name":"code-line","data":{},"nodes":[{"type":"text","id":"Mmox-1709735832237","leaves":[{"text":" } ","marks":[]}]}]},{"type":"block","id":"h41q-1709735832242","name":"code-line","data":{},"nodes":[{"type":"text","id":"02L1-1709735832241","leaves":[{"text":" {","marks":[]}]}]},{"type":"block","id":"71nF-1709735832244","name":"code-line","data":{},"nodes":[{"type":"text","id":"ouwR-1709735832243","leaves":[{"text":" timecost tmp(\"save\");","marks":[]}]}]},{"type":"block","id":"ALnq-1709735832246","name":"code-line","data":{},"nodes":[{"type":"text","id":"GaMa-1709735832245","leaves":[{"text":" vt.save(\"./test.jpg\"); ","marks":[]}]}]},{"type":"block","id":"isVE-1709735832248","name":"code-line","data":{},"nodes":[{"type":"text","id":"6CfD-1709735832247","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"iQMI-1709735832250","name":"code-line","data":{},"nodes":[{"type":"text","id":"k32d-1709735832249","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"hldJ-1709735832252","name":"code-line","data":{},"nodes":[{"type":"text","id":"48o8-1709735832251","leaves":[{"text":" return 0;","marks":[]}]}]},{"type":"block","id":"Dfmh-1709735832254","name":"code-line","data":{},"nodes":[{"type":"text","id":"skK0-1709735832253","leaves":[{"text":"}","marks":[]}]}]}],"state":{}},{"type":"block","id":"7d3j-1709735831615","name":"paragraph","data":{},"nodes":[{"type":"text","id":"nnA7-1709735831616","leaves":[{"text":"同时测试,capture所花费的时间","marks":[]}]}],"state":{}},{"type":"block","id":"jmYr-1709735982846","name":"code","data":{"wrap":false,"language":"","theme":"default"},"nodes":[{"type":"block","id":"MYBF-1709735982845","name":"code-line","data":{},"nodes":[{"type":"text","id":"0hL1-1709735982844","leaves":[{"text":"pico@luckfox:~$ sudo ./captureJpgApp\r","marks":[]}]}]},{"type":"block","id":"UWTf-1709735983096","name":"code-line","data":{},"nodes":[{"type":"text","id":"7Qle-1709735983095","leaves":[{"text":"init success\r","marks":[]}]}]},{"type":"block","id":"wE5b-1709735983098","name":"code-line","data":{},"nodes":[{"type":"text","id":"5lIT-1709735983097","leaves":[{"text":"capturetime spent:0.382155sec\r","marks":[]}]}]},{"type":"block","id":"LcG4-1709735983100","name":"code-line","data":{},"nodes":[{"type":"text","id":"cefT-1709735983099","leaves":[{"text":"savetime spent:0.003527sec\r","marks":[]}]}]},{"type":"block","id":"vyjc-1709735983102","name":"code-line","data":{},"nodes":[{"type":"text","id":"z5TR-1709735983101","leaves":[{"text":"capturetime spent:0.073295sec\r","marks":[]}]}]},{"type":"block","id":"14CE-1709735983104","name":"code-line","data":{},"nodes":[{"type":"text","id":"I340-1709735983103","leaves":[{"text":"savetime spent:0.001937sec\r","marks":[]}]}]},{"type":"block","id":"dH9m-1709735983106","name":"code-line","data":{},"nodes":[{"type":"text","id":"t7vF-1709735983105","leaves":[{"text":"capturetime spent:0.030448sec\r","marks":[]}]}]},{"type":"block","id":"V7hL-1709735983108","name":"code-line","data":{},"nodes":[{"type":"text","id":"fcmr-1709735983107","leaves":[{"text":"savetime spent:0.003411sec\r","marks":[]}]}]},{"type":"block","id":"MhHQ-1709735983110","name":"code-line","data":{},"nodes":[{"type":"text","id":"smeY-1709735983109","leaves":[{"text":"capturetime spent:0.025701sec\r","marks":[]}]}]},{"type":"block","id":"5KOx-1709735983112","name":"code-line","data":{},"nodes":[{"type":"text","id":"YpCJ-1709735983111","leaves":[{"text":"savetime spent:0.001992sec\r","marks":[]}]}]},{"type":"block","id":"uDB6-1709735983114","name":"code-line","data":{},"nodes":[{"type":"text","id":"afdh-1709735983113","leaves":[{"text":"capturetime spent:0.031095sec\r","marks":[]}]}]},{"type":"block","id":"iT0p-1709735983116","name":"code-line","data":{},"nodes":[{"type":"text","id":"mCmS-1709735983115","leaves":[{"text":"savetime spent:0.003586sec\r","marks":[]}]}]},{"type":"block","id":"8KcE-1709735983118","name":"code-line","data":{},"nodes":[{"type":"text","id":"P2fY-1709735983117","leaves":[{"text":"capturetime spent:0.025089sec\r","marks":[]}]}]},{"type":"block","id":"JkDY-1709735983120","name":"code-line","data":{},"nodes":[{"type":"text","id":"7zF9-1709735983119","leaves":[{"text":"savetime spent:0.004171sec\r","marks":[]}]}]},{"type":"block","id":"wU7T-1709735983122","name":"code-line","data":{},"nodes":[{"type":"text","id":"9SLC-1709735983121","leaves":[{"text":"capturetime spent:0.02678sec\r","marks":[]}]}]},{"type":"block","id":"IcPV-1709735983124","name":"code-line","data":{},"nodes":[{"type":"text","id":"Sc34-1709735983123","leaves":[{"text":"savetime spent:0.001836sec\r","marks":[]}]}]},{"type":"block","id":"kijs-1709735983126","name":"code-line","data":{},"nodes":[{"type":"text","id":"CyeJ-1709735983125","leaves":[{"text":"capturetime spent:0.031064sec\r","marks":[]}]}]},{"type":"block","id":"JHGi-1709735983128","name":"code-line","data":{},"nodes":[{"type":"text","id":"ASjx-1709735983127","leaves":[{"text":"savetime spent:0.001938sec","marks":[]}]}]}],"state":{}},{"type":"block","id":"1dDP-1709736019317","name":"paragraph","data":{},"nodes":[{"type":"text","id":"9pft-1709736019318","leaves":[{"text":"可以看到,capture所花费的时间大概在0.03s左右,也就是30ms一帧,对应就是30fps","marks":[]}]}],"state":{}}]">
<p><a href="https://github.com/LitchiCheng/RV1106_Linux/tree/main">LitchiCheng/RV1106_Linux: Linux test for RV1106 dev board (github.com)</a></p>
<p>如下为调用usb摄像头,连续拍照保存的测试代码</p>
<pre>
<code>#include <iostream>
#include "v4l2CapPicTool.h"
#include "timecost.h"
int main(int argc, char const *argv[])
{
v4l2CapPicTool vt("/dev/video0", 1080, 960, "jpg");
vt.init();
while (1){
usleep(100000);
{
timecost tmp("capture");
vt.capture();
}
{
timecost tmp("save");
vt.save("./test.jpg");
}
}
return 0;
}</code></pre>
<p>同时测试,capture所花费的时间</p>
<pre>
<code>pico@luckfox:~$ sudo ./captureJpgApp
init success
capturetime spent:0.382155sec
savetime spent:0.003527sec
capturetime spent:0.073295sec
savetime spent:0.001937sec
capturetime spent:0.030448sec
savetime spent:0.003411sec
capturetime spent:0.025701sec
savetime spent:0.001992sec
capturetime spent:0.031095sec
savetime spent:0.003586sec
capturetime spent:0.025089sec
savetime spent:0.004171sec
capturetime spent:0.02678sec
savetime spent:0.001836sec
capturetime spent:0.031064sec
savetime spent:0.001938sec</code></pre>
<p>可以看到,capture所花费的时间大概在0.03s左右,也就是30ms一帧,对应就是30fps</p>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1tK421x7my&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</p>
<p> </p>
<p> </p>
</article>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看精华帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
}
</script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>感谢楼主分享!</p>
damiaa 发表于 2024-3-10 10:25
感谢楼主分享!
<p>谢谢支持</p>
<p>这玩意 看着是不是和opencv差不多</p>
常见泽1 发表于 2024-3-10 11:38
这玩意 看着是不是和opencv差不多
<p>不是一个概念,opencv调用摄像头也是通过v4l2,但opencv更重要的是图像处理这些东西</p>
<p>请问怎么连接CSI相机呢</p>
页:
[1]