Skip to content

Commit 813f456

Browse files
committed
Add barcode scaner example
1 parent 7920d41 commit 813f456

23 files changed

Lines changed: 5125 additions & 0 deletions
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
# Project name and version
4+
project(CameraProject VERSION 1.0 LANGUAGES CXX)
5+
6+
# Set C++ standard
7+
set(CMAKE_CXX_STANDARD 17)
8+
set(CMAKE_CXX_STANDARD_REQUIRED True)
9+
10+
# Define include directories
11+
set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
12+
13+
# Define source files for the Camera library
14+
if (WIN32)
15+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
16+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebug")
17+
else()
18+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
19+
endif()
20+
21+
set(LIBRARY_SOURCES
22+
src/CameraWindows.cpp
23+
src/CameraPreviewWindows.cpp
24+
)
25+
elseif (UNIX AND NOT APPLE)
26+
set(LIBRARY_SOURCES
27+
src/CameraLinux.cpp
28+
src/CameraPreviewLinux.cpp
29+
)
30+
elseif (APPLE)
31+
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
32+
# Ensure that Objective-C++ source files are compiled as Objective-C++
33+
set(LIBRARY_SOURCES
34+
src/CameraMacOS.mm
35+
src/CameraPreviewMacOS.mm
36+
)
37+
set_source_files_properties(src/CameraMacOS.mm src/CameraPreviewMacOS.mm PROPERTIES COMPILE_FLAGS "-x objective-c++")
38+
39+
# Set main.cpp to be treated as Objective-C++
40+
set_source_files_properties(src/main.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
41+
endif()
42+
43+
# Add JNI wrapper source (common for all platforms)
44+
list(APPEND LIBRARY_SOURCES
45+
src/LiteCamJNI.cpp
46+
)
47+
48+
# Define source files for the executable
49+
set(EXECUTABLE_SOURCES
50+
src/main.cpp
51+
)
52+
53+
# Add the Camera shared library
54+
add_library(litecam SHARED ${LIBRARY_SOURCES})
55+
56+
# Set include directories for the Camera library
57+
target_include_directories(litecam PUBLIC ${INCLUDE_DIR})
58+
59+
# Define the CAMERA_EXPORTS macro for the shared library
60+
target_compile_definitions(litecam PRIVATE CAMERA_EXPORTS)
61+
62+
# Platform-specific dependencies for the Camera library
63+
if (UNIX AND NOT APPLE)
64+
find_package(X11 REQUIRED)
65+
if (X11_FOUND)
66+
target_include_directories(litecam PUBLIC ${X11_INCLUDE_DIR})
67+
target_link_libraries(litecam PRIVATE ${X11_LIBRARIES} pthread)
68+
endif()
69+
elseif (APPLE)
70+
find_library(COCOA_LIBRARY Cocoa REQUIRED)
71+
find_library(AVFOUNDATION_LIBRARY AVFoundation REQUIRED)
72+
find_library(COREMEDIA_LIBRARY CoreMedia REQUIRED)
73+
find_library(COREVIDEO_LIBRARY CoreVideo REQUIRED)
74+
find_library(OBJC_LIBRARY objc REQUIRED) # Add the Objective-C runtime library
75+
76+
target_link_libraries(litecam PRIVATE
77+
${COCOA_LIBRARY}
78+
${AVFOUNDATION_LIBRARY}
79+
${COREMEDIA_LIBRARY}
80+
${COREVIDEO_LIBRARY}
81+
${OBJC_LIBRARY} # Link the Objective-C runtime
82+
)
83+
elseif (WIN32)
84+
target_link_libraries(litecam PRIVATE ole32 uuid mfplat mf mfreadwrite mfuuid)
85+
endif()
86+
87+
# JNI support
88+
find_package(JNI)
89+
if (JNI_FOUND)
90+
message(STATUS "JNI found: ${JNI_INCLUDE_DIRS}")
91+
target_include_directories(litecam PRIVATE ${JNI_INCLUDE_DIRS})
92+
target_compile_definitions(litecam PRIVATE LITECAM_JNI_ENABLED)
93+
else()
94+
message(WARNING "JNI not found; Java wrapper native methods won't be available.")
95+
endif()
96+
97+
# Add the camera_capture executable
98+
add_executable(camera_capture ${EXECUTABLE_SOURCES})
99+
100+
# Link the Camera library to the executable
101+
target_link_libraries(camera_capture PRIVATE litecam)
102+
103+
# Include the shared library's headers in the executable
104+
target_include_directories(camera_capture PRIVATE ${INCLUDE_DIR})
105+
106+
# For macOS, link against the Cocoa framework
107+
if (APPLE)
108+
target_link_libraries(camera_capture PRIVATE
109+
${COCOA_LIBRARY}
110+
${AVFOUNDATION_LIBRARY}
111+
${COREMEDIA_LIBRARY}
112+
${COREVIDEO_LIBRARY}
113+
${OBJC_LIBRARY} # Link the Objective-C runtime
114+
)
115+
endif()
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# PowerShell script to compile Java sources and create litecam.jar
2+
param(
3+
[string]$BuildDir = "build",
4+
# Optional: additional directories containing prebuilt natives to merge (each should contain platform libs)
5+
[string[]]$ExtraNativeDirs = @()
6+
)
7+
8+
$ErrorActionPreference = 'Stop'
9+
10+
$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
11+
Set-Location $projectRoot
12+
13+
$javaSrc = Join-Path $projectRoot 'java-src'
14+
$outDir = Join-Path $projectRoot 'java-build'
15+
if (Test-Path $outDir) { Remove-Item -Recurse -Force $outDir }
16+
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
17+
18+
Write-Host "Compiling Java sources..."
19+
20+
$javaFiles = Get-ChildItem -Recurse $javaSrc -Filter *.java | ForEach-Object { $_.FullName }
21+
& javac -d $outDir $javaFiles
22+
if ($LASTEXITCODE -ne 0) { throw "javac failed" }
23+
24+
$jarPath = Join-Path $projectRoot 'litecam.jar'
25+
if (Test-Path $jarPath) { Remove-Item $jarPath }
26+
27+
# Collect native libraries by platform/arch
28+
$nativeMap = @{}
29+
$dllCandidates = @()
30+
$dllCandidates += (Join-Path $projectRoot 'build/Release/litecam.dll')
31+
$dllCandidates += (Join-Path $projectRoot 'build/litecam.dll')
32+
$dllCandidates += (Join-Path $projectRoot 'build/Release/liblitecam.dylib')
33+
$dllCandidates += (Join-Path $projectRoot 'build/liblitecam.dylib')
34+
$dllCandidates += (Join-Path $projectRoot 'build/Release/liblitecam.so')
35+
$dllCandidates += (Join-Path $projectRoot 'build/liblitecam.so')
36+
37+
function Get-OsToken {
38+
if ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)) { return 'windows' }
39+
elseif ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX)) { return 'macos' }
40+
else { return 'linux' }
41+
}
42+
function Get-ArchToken { if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -in @('Arm64')) { 'arm64' } else { 'x86_64' } }
43+
44+
$osToken = Get-OsToken
45+
$archToken = Get-ArchToken
46+
47+
# Primary host build native bundling
48+
$nativeRoot = Join-Path $outDir 'natives'
49+
New-Item -ItemType Directory -Force -Path $nativeRoot | Out-Null
50+
$nativeDir = Join-Path $nativeRoot "$osToken-$archToken"
51+
New-Item -ItemType Directory -Force -Path $nativeDir | Out-Null
52+
53+
$picked = $false
54+
foreach ($c in $dllCandidates) {
55+
if (Test-Path $c) {
56+
$destName = if ($osToken -eq 'windows') { 'litecam.dll' } elseif ($osToken -eq 'macos') { 'liblitecam.dylib' } else { 'liblitecam.so' }
57+
Copy-Item $c (Join-Path $nativeDir $destName) -Force
58+
Write-Host "Bundling native library: $c -> natives/$osToken-$archToken/$destName"
59+
$picked = $true
60+
break
61+
}
62+
}
63+
if (-not $picked) { Write-Warning "No native library found to bundle for host." }
64+
65+
# Merge additional prebuilt native sets
66+
foreach ($dir in $ExtraNativeDirs) {
67+
if (-not (Test-Path $dir)) { Write-Warning "Extra native dir not found: $dir"; continue }
68+
# Expect structure like <dir>/windows-x86_64/litecam.dll etc. Copy recursively.
69+
Get-ChildItem -Directory $dir | ForEach-Object {
70+
$platformFolder = $_.Name
71+
$target = Join-Path $nativeRoot $platformFolder
72+
New-Item -ItemType Directory -Force -Path $target | Out-Null
73+
Get-ChildItem -File -Recurse $_.FullName | ForEach-Object {
74+
Copy-Item $_.FullName (Join-Path $target ($_.Name)) -Force
75+
}
76+
Write-Host "Merged extra natives: $platformFolder"
77+
}
78+
}
79+
80+
Write-Host "Creating JAR with bundled natives..."
81+
Push-Location $outDir
82+
& jar cf $jarPath .
83+
Pop-Location
84+
85+
Write-Host "Created $jarPath (includes natives/* for packaged platforms)"
86+
87+
# Convenience run script
88+
$runScript = @'
89+
@echo off
90+
setlocal
91+
set JAR=%~dp0litecam.jar
92+
java -cp "%JAR%" com.example.litecam.LiteCamViewer %*
93+
endlocal
94+
'@
95+
Set-Content -Path (Join-Path $projectRoot 'run-litecam.bat') -Value $runScript -Encoding ASCII
96+
Write-Host "Created run-litecam.bat"

examples/barcode-scanner/build-jar.sh

Whitespace-only changes.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#ifndef CAMERA_H
2+
#define CAMERA_H
3+
4+
#include <vector>
5+
#include <string>
6+
#include <iostream>
7+
#include <cstdint>
8+
9+
// Export macro for shared library
10+
#ifdef _WIN32
11+
#ifdef CAMERA_EXPORTS
12+
#define CAMERA_API __declspec(dllexport)
13+
#else
14+
#define CAMERA_API __declspec(dllimport)
15+
#endif
16+
#elif defined(__linux__) || defined(__APPLE__)
17+
#define CAMERA_API __attribute__((visibility("default")))
18+
#else
19+
#define CAMERA_API
20+
#endif
21+
22+
// Platform-specific includes
23+
#ifdef _WIN32
24+
#include <windows.h>
25+
#include <mfapi.h>
26+
#include <mfidl.h>
27+
#include <mfobjects.h>
28+
#include <mfreadwrite.h>
29+
#include <wrl/client.h>
30+
#include <dshow.h>
31+
32+
#elif __linux__
33+
#include <linux/videodev2.h>
34+
#include <fcntl.h>
35+
#include <unistd.h>
36+
#include <sys/ioctl.h>
37+
#include <sys/mman.h>
38+
39+
struct Buffer
40+
{
41+
void *start;
42+
size_t length;
43+
};
44+
#endif
45+
46+
// Struct definitions
47+
struct CAMERA_API FrameData
48+
{
49+
unsigned char *rgbData; // RGB pixel data
50+
int width; // Frame width
51+
int height; // Frame height
52+
size_t size;
53+
};
54+
55+
struct CAMERA_API MediaTypeInfo
56+
{
57+
uint32_t width;
58+
uint32_t height;
59+
#ifdef _WIN32
60+
wchar_t subtypeName[512]; // Wide characters for Windows
61+
#else
62+
char subtypeName[512]; // Narrow characters for Linux/macOS
63+
#endif
64+
};
65+
66+
struct CAMERA_API CaptureDeviceInfo
67+
{
68+
69+
#ifdef _WIN32
70+
wchar_t friendlyName[512];
71+
#else
72+
char friendlyName[512]; // Narrow characters for Linux/macOS
73+
#endif
74+
};
75+
76+
// Exported functions
77+
CAMERA_API std::vector<CaptureDeviceInfo> ListCaptureDevices();
78+
CAMERA_API void ReleaseFrame(FrameData &frame);
79+
CAMERA_API void saveFrameAsJPEG(const unsigned char *data, int width, int height, const std::string &filename);
80+
81+
// Camera class
82+
class CAMERA_API Camera
83+
{
84+
public:
85+
#ifdef _WIN32
86+
Camera();
87+
~Camera();
88+
#elif __linux__
89+
Camera() : frameWidth(640), frameHeight(480), fd(-1), buffers(nullptr), bufferCount(0) {}
90+
~Camera() { Release(); }
91+
#elif __APPLE__
92+
Camera() noexcept; // Add noexcept to match the implementation
93+
~Camera();
94+
#endif
95+
96+
bool Open(int cameraIndex);
97+
void Release();
98+
99+
std::vector<MediaTypeInfo> ListSupportedMediaTypes();
100+
FrameData CaptureFrame();
101+
bool SetResolution(int width, int height);
102+
103+
uint32_t frameWidth;
104+
uint32_t frameHeight;
105+
106+
private:
107+
#ifdef _WIN32
108+
void *reader;
109+
110+
bool initialized;
111+
void InitializeMediaFoundation();
112+
void ShutdownMediaFoundation();
113+
#endif
114+
115+
#ifdef __linux__
116+
int fd;
117+
Buffer *buffers;
118+
unsigned int bufferCount;
119+
120+
bool InitDevice();
121+
void UninitDevice();
122+
bool StartCapture();
123+
void StopCapture();
124+
#endif
125+
126+
#ifdef __APPLE__
127+
void *captureSession; // AVFoundation session object
128+
void *videoOutput;
129+
#endif
130+
};
131+
132+
#endif // CAMERA_H

0 commit comments

Comments
 (0)