usb摄像机直接接入树莓派USB接口

1. 安装motion

  1. pi@raspberrypi:~/code/camera $ sudo apt-get install motion

2.配置motion

打开motion daemon守护进程,让他可以一直后台运行

  1. # 需要root权限
  2. pi@raspberrypi:~/code/camera $ sudo vi /etc/default/motion
  3. # 查看下修改结果
  4. pi@raspberrypi:~/code/camera $ cat /etc/default/motion
  5. # set to 'yes' to enable the motion daemon
  6. start_motion_daemon=yes

修改motion配置文件

  1. #daemon off
  2. daemon on
  3. # 尺寸
  4. # Image width (pixels). Valid range: Camera dependent, default: 320
  5. width 1920
  6. # Image height (pixels). Valid range: Camera dependent, default: 240
  7. height 1080
  8. # 端口和是否只本机查看
  9. webcontrol_port 8080 # 我没动
  10. #webcontrol_localhost on# 改成off
  11. webcontrol_localhost off
  12. # 设置访问密码
  13. stream_localhost off
  14. # 链接需要密码认证
  15. stream_auth_method 1 #密码认证

3. 查看画面

树莓派执行命令

  1. sudo motion

浏览器查看 http://192.168.1.101:8081/

image.png

4. 参数调优,提升流畅度

以上参数虽然可以在浏览器看到画面但延迟非常高,很难试用与智能小车等实时监控,可以通过调整motion配置文件,提升流畅度

  1. # 编辑motion配置文件
  2. pi@raspberrypi:~ $ sudo vi /etc/motion/motion.conf
  3. # 调整帧数
  4. framerate 50 #我调整50 change frame rate
  5. # 调整质量
  6. stream_quality 50 # 如果觉着卡慢可以降低下质量 chenxzongcf
  7. # 默认是1 我调100 超级流畅
  8. stream_maxrate 100

5. 在Android手机上查看画面

5.1 编辑布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <WebView
  9. android:id="@+id/webview"
  10. android:layout_gravity="center_horizontal"
  11. android:layout_alignParentTop="true"
  12. android:layout_centerHorizontal="true"
  13. android:layout_width="800dp"
  14. android:layout_height="400dp"/>
  15. </RelativeLayout>

5.2 MainActivity

  1. package com.chen.raspi;
  2. import android.os.Bundle;
  3. import android.view.KeyEvent;
  4. import android.webkit.WebChromeClient;
  5. import android.webkit.WebSettings;
  6. import android.webkit.WebView;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. public class MainActivity extends AppCompatActivity {
  9. private WebView webview;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. // if (getSupportActionBar() != null) {
  15. // getSupportActionBar().hide();
  16. // }
  17. webview = (WebView) findViewById(R.id.webview);
  18. load();
  19. }
  20. @Override
  21. public boolean dispatchKeyEvent(KeyEvent event) {
  22. return super.dispatchKeyEvent(event);
  23. }
  24. private void load() {
  25. WebSettings webviewSettings = webview.getSettings();
  26. webviewSettings.setJavaScriptEnabled(true);
  27. webviewSettings.setJavaScriptEnabled(true);
  28. webviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  29. webviewSettings.setAllowFileAccess(true);// 设置允许访问文件数据
  30. webviewSettings.setSupportZoom(true);
  31. webviewSettings.setBuiltInZoomControls(true);
  32. webviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  33. webviewSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  34. webviewSettings.setDomStorageEnabled(true);
  35. webviewSettings.setDatabaseEnabled(true);
  36. //设置载入页面自适应手机屏幕,居中显示
  37. webviewSettings.setUseWideViewPort(true);
  38. webviewSettings.setLoadWithOverviewMode(true);
  39. // webview.setWebViewClient(new WebViewClient());
  40. webview.setWebChromeClient(new WebChromeClient());
  41. webview.loadUrl("http://192.168.1.101:8081/");
  42. // webview.loadUrl("https://www.baidu.com/");
  43. }
  44. }

5.3 效果图

image.png