Skip to content

Instantly share code, notes, and snippets.

@Ar-Ray-code
Last active March 7, 2023 04:57
Show Gist options
  • Save Ar-Ray-code/500cb7a215bd2fc5796b68fd8f45250d to your computer and use it in GitHub Desktop.
Save Ar-Ray-code/500cb7a215bd2fc5796b68fd8f45250d to your computer and use it in GitHub Desktop.
libcaemra-find (check picamera)
// Copyright 2023 Ar-Ray-code.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <libcamera/camera.h>
#include <libcamera/camera_manager.h>
#include <libcamera/control_ids.h>
#include <libcamera/property_ids.h>
#include <libcamera/transform.h>
using CameraManager = libcamera::CameraManager;
using Camera = libcamera::Camera;
std::unique_ptr<CameraManager> camera_manager_;
bool camera_is_connected()
{
using namespace libcamera;
std::unique_ptr<CameraManager> cm = std::make_unique<CameraManager>();
int ret = cm->start();
if (ret)
{
std::cout << "camera manager failed to start, code " << ret << std::endl;
return 1;
}
std::vector<std::shared_ptr<Camera>> cameras = cm->cameras();
// Do not show USB webcams as these are not supported in libcamera-apps!
auto rem = std::remove_if(cameras.begin(), cameras.end(),
[](auto &cam) { return cam->id().find("/usb") != std::string::npos; });
cameras.erase(rem, cameras.end());
if (cameras.size() != 0)
{
unsigned int idx = 0;
// print number of cameras
std::cout << "Number of cameras: " << cameras.size() << std::endl;
for (auto const &cam : cameras)
{
cam->acquire();
std::cout << idx++ << " : " << *cam->properties().get(libcamera::properties::Model);
auto area = cam->properties().get(properties::PixelArrayActiveAreas);
if (area)
std::cout << " [" << (*area)[0].size().toString() << "]";
std::cout << " (" << cam->id() << ")" << std::endl;
std::unique_ptr<CameraConfiguration> config = cam->generateConfiguration({libcamera::StreamRole::Raw});
if (!config)
{
std::cout << "failed to generate capture configuration" << std::endl;
return 1;
}
cam->release();
}
}
else
{
std::cout << "No cameras available!" << std::endl;
return 1;
}
cameras.clear();
cm->stop();
return 0;
}
int main(void)
{
return camera_is_connected();
}
cmake_minimum_required(VERSION 3.6)
project(libcaemra-find)
set(CMAKE_CXX_STANDARD 17)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
add_definitions(-D_FILE_OFFSET_BITS=64)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBCAMERA REQUIRED libcamera)
message(STATUS "libcamera library found:")
message(STATUS " version: ${LIBCAMERA_VERSION}")
message(STATUS " libraries: ${LIBCAMERA_LINK_LIBRARIES}")
message(STATUS " include path: ${LIBCAMERA_INCLUDE_DIRS}")
include_directories(include ${LIBCAMERA_INCLUDE_DIRS})
set(TARGET libcaemra-find)
add_executable(${TARGET} 00-${TARGET}.cpp)
target_link_libraries(${TARGET} ${LIBCAMERA_LINK_LIBRARIES})
@Ar-Ray-code
Copy link
Author

Download : v0.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment