Detecting camera features with Camera2

本文介绍了如何使用Camera2 API来检测设备上相机的各种特性,包括判断是否为前置摄像头等功能,帮助开发者更好地定制照片应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Camera2检测相机功能(Detecting camera features with Camera2)

原文链接:https://medium.com/google-developers/detecting-camera-features-with-camera2-61675bb7d1bf#.ks8l788lx

我手机的相机有它自己的特性。它有闪光灯,连拍,甚至有内置的降噪点。开发者可以使用这些有优势的个性化功能来提供更好的照片应用。但是,并不是所有相机都一样的,有些可能缺少上述的功能。作为一个开发者,如何知道哪些功能能用哪些不能用?Camera2告诉你。

为什么要用Camera2?(Why Camera2)

Camera2 API 在Android L 中引入,它是原始Camera API接口的后继者。

Camera2适用于那些需要直接访问设备相机的照片应用。如果相机仅仅是你应用程序体验上的一个附属功能,那么使用Camera Intent的方式就足够了。但是,如果你的应用需要自定义相机的体验,那么Camera2 API是一个不错的选择。

Camera2沿用了它原始接口的一些优点:

  • 在新硬件上提升性能。

  • 拍照的的间隔时间更快

  • 显示多个相机的预览

  • 直接应用效果和过滤器

另外一些值得注意的优点,你现在可以查询API来检测相机所需的功能。

使用Camera2禁用自身(Banning Selfies with Camera2)

现在,让我们假设你正在编译一个反自拍的照片应用。如果用户尝试翻转前置摄像头,你将发送一个Toast,让他们知道你的应用没有这个功能。

Camera2 API会告诉你是否存在前置摄像头,不过首先你需要先获取设备上可用的相机列表。

Step1:获取Camera(Get a Camera)

Camera2 API的核心就是CameraManager类。你可以通过CameraManager使用getCameraIdList()获取相机的字符串数组id。相机的id代表设备上可用的相机。使用getCameraCharactertics()方法,可以传入相机的id并获取设备可用的设置和输出的参数。

CameraManager manager =
      (CameraManager)getSystemService(CAMERA_SERVICE);
try {
  for (String cameraId : manager.getCameraIdList()) {
    CameraCharacteristics chars
       = manager.getCameraCharacteristics(cameraId);
    // Do something with the characteristics
} catch (CameraAccessException e) {
  e.printStackTrace();
}

获取相机的特性之后,你就可以准备查询了。

Step2:查询特性(Query the characteristics)

一旦你获取CameraCharacteristics对象,你就可以查询设备上可用的特性。 get() 方法需要传入一个CameraCharacteristic域,然后返回这个域对应的值。

// Does the camera have a forwards facing lens?
Integer facing = chars.get(CameraCharacteristics.LENS_FACING);

Step3:禁用自身(Ban Selfies)

facing变量仅仅只是一个整数。你如何知道相机镜头实际所面对的方向呢?这个整数代表一个 CameraMetadata常量。使用 LENS_FACING_FRONT 常量,你可以检测摄像头是否对着脸部。

private void detectSelfieCamera(String cameraId) {
  CameraCharacteristics chars
               = manager.getCameraCharacteristics(cameraId);
  // Does the camera have a forwards facing lens?
  Integer facing = chars.get(CameraCharacteristics.LENS_FACING);
  if (facing != null && facing ==
        CameraCharacteristics.LENS_FACING_FRONT) {
    // No selfies!
    Context context = getApplicationContext();
    CharSequence text = “No selfie for you! Turn the camera around”;
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration)
    toast.show();
    // don’t process anything for the front facing camera
    continue;
  } else {
  // Open the rear facing camera (see github repo below)
  }
}

任务完成。现在任何自拍都会弹出一个提示,让用户知道你的应用并不支持前置摄像头的使用。

检测其他功能(Detecting other features)

使用这个API还能检测什么功能呢?很多,这里有78种CameraCharacteristics,所以,所以这里有相当多的功能可以探索。

  • FLASH_INFO_AVAILABLE: 相机是否有闪光灯?

  • REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE: 相机能否拍摄大于20fps的高分辨率照片?然后你才能添加连拍模式。

  • JPEG_AVAILABLE_THUMBNAIL_SIZES: 设备支持什么JPEG缩略图的尺寸?

Camera2 on Github

如果你准备要更深的了解Camera2,Github官方的示例是一个非常不错的开始。示例有更进一步的介绍,包括如何显示相机预览和拍照。

准备好使用Camera2 API开发新的相机应用程序。

(base) HwHiAiUser@orangepiaipro-20t:~/ros2_ws$ colcon build --event-handlers console_direct+ --cmake-args -DCMAKE_BUILD_TYPE=Release Starting >>> astra_camera_msgs -- The C compiler identification is GNU 11.4.0 -- The CXX compiler identification is GNU 11.4.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found ament_cmake: 1.3.11 (/opt/ros/humble/share/ament_cmake/cmake) -- Found Python3: /usr/local/miniconda3/bin/python3 (found version "3.9.2") found components: Interpreter -- Found rosidl_default_generators: 1.2.0 (/opt/ros/humble/share/rosidl_default_generators/cmake) -- Using all available rosidl_typesupport_c: rosidl_typesupport_fastrtps_c;rosidl_typesupport_introspection_c -- Found rosidl_adapter: 3.1.6 (/opt/ros/humble/share/rosidl_adapter/cmake) -- Using all available rosidl_typesupport_cpp: rosidl_typesupport_fastrtps_cpp;rosidl_typesupport_introspection_cpp -- Found sensor_msgs: 4.8.0 (/opt/ros/humble/share/sensor_msgs/cmake) Traceback (most recent call last): File "/opt/ros/humble/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 22, in <module> from catkin_pkg.package import parse_package_string ModuleNotFoundError: No module named 'catkin_pkg' CMake Error at /opt/ros/humble/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:95 (message): execute_process(/usr/local/miniconda3/bin/python3 /opt/ros/humble/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py /home/HwHiAiUser/ros2_ws/src/ros2_astra_camera/astra_camera_msgs/package.xml /home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/ament_cmake_core/package.cmake) returned error code 1 Call Stack (most recent call first): /opt/ros/humble/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:49 (_ament_package_xml) /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:226 (ament_package_xml) CMakeLists.txt:17 (rosidl_generate_interfaces) -- Configuring incomplete, errors occurred! See also "/home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/CMakeFiles/CMakeOutput.log". --- stderr: astra_camera_msgs Traceback (most recent call last): File "/opt/ros/humble/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 22, in <module> from catkin_pkg.package import parse_package_string ModuleNotFoundError: No module named 'catkin_pkg' CMake Error at /opt/ros/humble/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:95 (message): execute_process(/usr/local/miniconda3/bin/python3 /opt/ros/humble/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py /home/HwHiAiUser/ros2_ws/src/ros2_astra_camera/astra_camera_msgs/package.xml /home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/ament_cmake_core/package.cmake) returned error code 1 Call Stack (most recent call first): /opt/ros/humble/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:49 (_ament_package_xml) /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:226 (ament_package_xml) CMakeLists.txt:17 (rosidl_generate_interfaces) --- Failed <<< astra_camera_msgs [3.85s, exited with code 1] Summary: 0 packages finished [4.58s] 1 package failed: astra_camera_msgs 1 package had stderr output: astra_camera_msgs 1 package not processed
06-26
(base) HwHiAiUser@orangepiaipro-20t:~/ros2_ws$ colcon build --event-handlers console_direct+ --cmake-args -DCMAKE_BUILD_TYPE=Release Starting >>> astra_camera_msgs -- The C compiler identification is GNU 11.4.0 -- The CXX compiler identification is GNU 11.4.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found ament_cmake: 1.3.11 (/opt/ros/humble/share/ament_cmake/cmake) -- Found Python3: /usr/local/miniconda3/bin/python3 (found version "3.9.2") found components: Interpreter -- Found rosidl_default_generators: 1.2.0 (/opt/ros/humble/share/rosidl_default_generators/cmake) -- Using all available rosidl_typesupport_c: rosidl_typesupport_fastrtps_c;rosidl_typesupport_introspection_c -- Found rosidl_adapter: 3.1.6 (/opt/ros/humble/share/rosidl_adapter/cmake) -- Using all available rosidl_typesupport_cpp: rosidl_typesupport_fastrtps_cpp;rosidl_typesupport_introspection_cpp -- Found sensor_msgs: 4.8.0 (/opt/ros/humble/share/sensor_msgs/cmake) CMake Error at /opt/ros/humble/share/rosidl_adapter/cmake/rosidl_adapt_interfaces.cmake:59 (message): execute_process(/usr/local/miniconda3/bin/python3 -m rosidl_adapter --package-name astra_camera_msgs --arguments-file /home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/rosidl_adapter__arguments__astra_camera_msgs.json --output-dir /home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/rosidl_adapter/astra_camera_msgs --output-file /home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/rosidl_adapter/astra_camera_msgs.idls) returned error code 1: AttributeError processing template 'msg.idl.em' Traceback (most recent call last): File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 51, in evaluate_template em.BUFFERED_OPT: True, AttributeError: module 'em' has no attribute 'BUFFERED_OPT' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/miniconda3/lib/python3.9/runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/local/miniconda3/lib/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/__main__.py", line 19, in <module> sys.exit(main()) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/main.py", line 53, in main abs_idl_file = convert_to_idl( File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/__init__.py", line 19, in convert_to_idl return convert_msg_to_idl( File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/msg/__init__.py", line 39, in convert_msg_to_idl expand_template('msg.idl.em', data, output_file, encoding='iso-8859-1') File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 23, in expand_template content = evaluate_template(template_name, data) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 69, in evaluate_template _interpreter.shutdown() AttributeError: 'NoneType' object has no attribute 'shutdown' Call Stack (most recent call first): /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:130 (rosidl_adapt_interfaces) CMakeLists.txt:17 (rosidl_generate_interfaces) -- Configuring incomplete, errors occurred! See also "/home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/CMakeFiles/CMakeOutput.log". --- stderr: astra_camera_msgs CMake Error at /opt/ros/humble/share/rosidl_adapter/cmake/rosidl_adapt_interfaces.cmake:59 (message): execute_process(/usr/local/miniconda3/bin/python3 -m rosidl_adapter --package-name astra_camera_msgs --arguments-file /home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/rosidl_adapter__arguments__astra_camera_msgs.json --output-dir /home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/rosidl_adapter/astra_camera_msgs --output-file /home/HwHiAiUser/ros2_ws/build/astra_camera_msgs/rosidl_adapter/astra_camera_msgs.idls) returned error code 1: AttributeError processing template 'msg.idl.em' Traceback (most recent call last): File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 51, in evaluate_template em.BUFFERED_OPT: True, AttributeError: module 'em' has no attribute 'BUFFERED_OPT' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/miniconda3/lib/python3.9/runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/local/miniconda3/lib/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/__main__.py", line 19, in <module> sys.exit(main()) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/main.py", line 53, in main abs_idl_file = convert_to_idl( File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/__init__.py", line 19, in convert_to_idl return convert_msg_to_idl( File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/msg/__init__.py", line 39, in convert_msg_to_idl expand_template('msg.idl.em', data, output_file, encoding='iso-8859-1') File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 23, in expand_template content = evaluate_template(template_name, data) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/resource/__init__.py", line 69, in evaluate_template _interpreter.shutdown() AttributeError: 'NoneType' object has no attribute 'shutdown' Call Stack (most recent call first): /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:130 (rosidl_adapt_interfaces) CMakeLists.txt:17 (rosidl_generate_interfaces) --- Failed <<< astra_camera_msgs [3.83s, exited with code 1] Summary: 0 packages finished [4.53s] 1 package failed: astra_camera_msgs 1 package had stderr output: astra_camera_msgs 1 package not processed
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值