Skip to content

Commit

Permalink
Update libs for Windows and Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Jan 7, 2025
1 parent b5e8183 commit 9653a79
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 119 deletions.
4 changes: 3 additions & 1 deletion litecam/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.15)

# Project name and version
project(CameraProject VERSION 1.0 LANGUAGES CXX)
Expand All @@ -12,6 +12,8 @@ set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)

# Define source files for the Camera library
if (WIN32)
#set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")

set(LIBRARY_SOURCES
src/CameraWindows.cpp
src/CameraPreviewWindows.cpp
Expand Down
30 changes: 1 addition & 29 deletions litecam/dist/include/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,6 @@ struct Buffer
};
#endif

///////////////////////////////////////////////////////////////////////////////
// Save a frame as a JPEG image using the STB library
// https://github.com/nothings/stb/blob/master/stb_image_write.h
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void saveFrameAsJPEG(const unsigned char *data, int width, int height, const std::string &filename)
{
// Simple image saving using STB library or another JPEG encoding method
if (stbi_write_jpg(filename.c_str(), width, height, 3, data, 90))
{
std::cout << "Saved frame to " << filename << std::endl;
}
else
{
std::cerr << "Error saving frame as JPEG." << std::endl;
}
}
///////////////////////////////////////////////////////////////////////////////

unsigned char clamp(double value, double min, double max)
{
if (value < min)
Expand Down Expand Up @@ -139,16 +120,7 @@ struct CAMERA_API CaptureDeviceInfo
// Exported functions
CAMERA_API std::vector<CaptureDeviceInfo> ListCaptureDevices();
CAMERA_API void ReleaseFrame(FrameData &frame);

void ReleaseFrame(FrameData &frame)
{
if (frame.rgbData)
{
delete[] frame.rgbData;
frame.rgbData = nullptr;
frame.size = 0;
}
}
CAMERA_API void saveFrameAsJPEG(const unsigned char *data, int width, int height, const std::string &filename);

// Camera class
class CAMERA_API Camera
Expand Down
4 changes: 2 additions & 2 deletions litecam/dist/include/CameraPreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class CAMERA_API CameraWindow
{
public:
CameraWindow(int width, int height, const std::string &title);
CameraWindow(int width, int height, const char *title);
~CameraWindow();

bool Create();
Expand All @@ -48,7 +48,7 @@ class CAMERA_API CameraWindow
private:
int width;
int height;
std::string title;
char *title;

#ifdef _WIN32
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Expand Down
Binary file modified litecam/dist/lib/linux/liblitecam.so
Binary file not shown.
Binary file modified litecam/dist/lib/windows/debug/litecam.dll
Binary file not shown.
Binary file modified litecam/dist/lib/windows/debug/litecam.lib
Binary file not shown.
Binary file modified litecam/dist/lib/windows/release/litecam.dll
Binary file not shown.
Binary file modified litecam/dist/lib/windows/release/litecam.lib
Binary file not shown.
5 changes: 3 additions & 2 deletions litecam/examples/barcode/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.15)
project(BarcodeScanner)

if(WIN32)

# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../dist/lib/windows/release ${CMAKE_CURRENT_SOURCE_DIR}/../../../examples/10.x/sdk/platforms/win/lib)
else()
Expand Down
30 changes: 1 addition & 29 deletions litecam/include/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,6 @@ struct Buffer
};
#endif

///////////////////////////////////////////////////////////////////////////////
// Save a frame as a JPEG image using the STB library
// https://github.com/nothings/stb/blob/master/stb_image_write.h
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void saveFrameAsJPEG(const unsigned char *data, int width, int height, const std::string &filename)
{
// Simple image saving using STB library or another JPEG encoding method
if (stbi_write_jpg(filename.c_str(), width, height, 3, data, 90))
{
std::cout << "Saved frame to " << filename << std::endl;
}
else
{
std::cerr << "Error saving frame as JPEG." << std::endl;
}
}
///////////////////////////////////////////////////////////////////////////////

unsigned char clamp(double value, double min, double max)
{
if (value < min)
Expand Down Expand Up @@ -139,16 +120,7 @@ struct CAMERA_API CaptureDeviceInfo
// Exported functions
CAMERA_API std::vector<CaptureDeviceInfo> ListCaptureDevices();
CAMERA_API void ReleaseFrame(FrameData &frame);

void ReleaseFrame(FrameData &frame)
{
if (frame.rgbData)
{
delete[] frame.rgbData;
frame.rgbData = nullptr;
frame.size = 0;
}
}
CAMERA_API void saveFrameAsJPEG(const unsigned char *data, int width, int height, const std::string &filename);

// Camera class
class CAMERA_API Camera
Expand Down
4 changes: 2 additions & 2 deletions litecam/include/CameraPreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class CAMERA_API CameraWindow
{
public:
CameraWindow(int width, int height, const std::string &title);
CameraWindow(int width, int height, const char *title);
~CameraWindow();

bool Create();
Expand All @@ -48,7 +48,7 @@ class CAMERA_API CameraWindow
private:
int width;
int height;
std::string title;
char *title;

#ifdef _WIN32
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Expand Down
114 changes: 72 additions & 42 deletions litecam/src/CameraLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <vector>
#include <string>
#include <map>
#include <cstring>

bool Camera::Open(int cameraIndex)
{
Expand Down Expand Up @@ -196,48 +197,6 @@ void Camera::StopCapture()
ioctl(fd, VIDIOC_STREAMOFF, &type);
}

CAMERA_API std::vector<CaptureDeviceInfo> ListCaptureDevices()
{
std::vector<CaptureDeviceInfo> devices;

// Scan for /dev/videoX devices
for (int i = 0; i < 10; ++i) // Check first 10 devices (can be increased if needed)
{
std::string devicePath = "/dev/video" + std::to_string(i);

// Open the device
int fd = open(devicePath.c_str(), O_RDWR | O_NONBLOCK, 0);
if (fd == -1)
{
continue; // Skip if the device cannot be opened
}

// Query device capabilities
struct v4l2_capability cap;
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0)
{
// Check if it's a video capture device
if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)
{
CaptureDeviceInfo deviceInfo = {};

// Copy the friendly name directly into the `friendlyName` buffer
strncpy(deviceInfo.friendlyName, reinterpret_cast<const char *>(cap.card), sizeof(deviceInfo.friendlyName) - 1);

// Null-terminate the friendlyName string
deviceInfo.friendlyName[sizeof(deviceInfo.friendlyName) - 1] = '\0';

devices.push_back(deviceInfo);
}
}

// Close the device
close(fd);
}

return devices;
}

std::vector<MediaTypeInfo> Camera::ListSupportedMediaTypes()
{
std::vector<MediaTypeInfo> mediaTypes;
Expand Down Expand Up @@ -297,3 +256,74 @@ std::vector<MediaTypeInfo> Camera::ListSupportedMediaTypes()

return mediaTypes;
}

std::vector<CaptureDeviceInfo> ListCaptureDevices()
{
std::vector<CaptureDeviceInfo> devices;

// Scan for /dev/videoX devices
for (int i = 0; i < 10; ++i) // Check first 10 devices (can be increased if needed)
{
std::string devicePath = "/dev/video" + std::to_string(i);

// Open the device
int fd = open(devicePath.c_str(), O_RDWR | O_NONBLOCK, 0);
if (fd == -1)
{
continue; // Skip if the device cannot be opened
}

// Query device capabilities
struct v4l2_capability cap;
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0)
{
// Check if it's a video capture device
if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)
{
CaptureDeviceInfo deviceInfo = {};

// Copy the friendly name directly into the `friendlyName` buffer
strncpy(deviceInfo.friendlyName, reinterpret_cast<const char *>(cap.card), sizeof(deviceInfo.friendlyName) - 1);

// Null-terminate the friendlyName string
deviceInfo.friendlyName[sizeof(deviceInfo.friendlyName) - 1] = '\0';

devices.push_back(deviceInfo);
}
}

// Close the device
close(fd);
}

return devices;
}

void ReleaseFrame(FrameData &frame)
{
if (frame.rgbData)
{
delete[] frame.rgbData;
frame.rgbData = nullptr;
frame.size = 0;
}
}

///////////////////////////////////////////////////////////////////////////////
// Save a frame as a JPEG image using the STB library
// https://github.com/nothings/stb/blob/master/stb_image_write.h
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void saveFrameAsJPEG(const unsigned char *data, int width, int height, const std::string &filename)
{
// Simple image saving using STB library or another JPEG encoding method
if (stbi_write_jpg(filename.c_str(), width, height, 3, data, 90))
{
std::cout << "Saved frame to " << filename << std::endl;
}
else
{
std::cerr << "Error saving frame as JPEG." << std::endl;
}
}
///////////////////////////////////////////////////////////////////////////////
31 changes: 30 additions & 1 deletion litecam/src/CameraMacOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,33 @@ - (void)captureOutput:(AVCaptureOutput *)output
}

return mediaTypes;
}
}

void ReleaseFrame(FrameData &frame)
{
if (frame.rgbData)
{
delete[] frame.rgbData;
frame.rgbData = nullptr;
frame.size = 0;
}
}

///////////////////////////////////////////////////////////////////////////////
// Save a frame as a JPEG image using the STB library
// https://github.com/nothings/stb/blob/master/stb_image_write.h
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void saveFrameAsJPEG(const unsigned char *data, int width, int height, const std::string &filename)
{
// Simple image saving using STB library or another JPEG encoding method
if (stbi_write_jpg(filename.c_str(), width, height, 3, data, 90))
{
std::cout << "Saved frame to " << filename << std::endl;
}
else
{
std::cerr << "Error saving frame as JPEG." << std::endl;
}
}
///////////////////////////////////////////////////////////////////////////////
11 changes: 8 additions & 3 deletions litecam/src/CameraPreviewLinux.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#include "CameraPreview.h"
#include <iostream>
#include <algorithm>
#include <cstring>

CameraWindow::CameraWindow(int w, int h, const std::string &t)
: width(w), height(h), title(t), display(nullptr), window(0), gc(nullptr)
CameraWindow::CameraWindow(int w, int h, const char *title)
: width(w), height(h), display(nullptr), window(0), gc(nullptr)
{
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
}

CameraWindow::~CameraWindow()
{
delete[] title;

if (gc)
{
XFreeGC(display, gc);
Expand Down Expand Up @@ -47,7 +52,7 @@ bool CameraWindow::Create()
return false;
}

XStoreName(display, window, title.c_str());
XStoreName(display, window, title);

gc = XCreateGC(display, window, 0, nullptr);
if (!gc)
Expand Down
13 changes: 10 additions & 3 deletions litecam/src/CameraPreviewMacOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>
#include <string>
#include <utility>
#include <cstring>

// Define a C++ implementation struct to hold drawing data
struct CameraContentViewImpl {
Expand Down Expand Up @@ -179,10 +180,16 @@ - (BOOL)windowShouldClose:(id)sender {
@end

// Implementation of CameraWindow class
CameraWindow::CameraWindow(int width, int height, const std::string &title)
: width(width), height(height), title(title), nsWindow(nullptr), contentView(nullptr) {}
CameraWindow::CameraWindow(int width, int height, const char *title)
: width(width), height(height), nsWindow(nullptr), contentView(nullptr) {

this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
}

CameraWindow::~CameraWindow() {
delete[] title;

if (contentView) {
CameraContentView *cv = (__bridge CameraContentView *)contentView;
[cv removeFromSuperview];
Expand Down Expand Up @@ -214,7 +221,7 @@ - (BOOL)windowShouldClose:(id)sender {
return false;
}

[window setTitle:[NSString stringWithUTF8String:title.c_str()]];
[window setTitle:[NSString stringWithUTF8String:title]];
[window makeKeyAndOrderFront:nil];

// Initialize the custom content view
Expand Down
Loading

0 comments on commit 9653a79

Please sign in to comment.