Skip to content

Commit

Permalink
It works
Browse files Browse the repository at this point in the history
  • Loading branch information
niclasschuemann committed Oct 30, 2023
1 parent 8c4c1d5 commit b22f847
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3

COCOAPODS: 1.12.1
COCOAPODS: 1.13.0
6 changes: 3 additions & 3 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = 3K73F77M82;
DEVELOPMENT_TEAM = 7UUP6MKG3S;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -494,7 +494,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = 3K73F77M82;
DEVELOPMENT_TEAM = 7UUP6MKG3S;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -521,7 +521,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = 3K73F77M82;
DEVELOPMENT_TEAM = 7UUP6MKG3S;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down
5 changes: 3 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class _MyAppState extends State<MyApp> {
preferredResolution: PreferredResolution.x1920x1080,
lensDirection: LensDirection.back,
preferredFrameRate: PreferredFrameRate.fps240,
useDepthCamera: false,
);
_shootEffectController = CameraShootEffectController();
// setUpStream();
Expand All @@ -63,12 +64,12 @@ class _MyAppState extends State<MyApp> {
Future<void> shareFaceIdData(
TakePictureResult result, BuildContext context) async {
print('writing to file');
// final filepath = await writePlyFile(result.faceIdSensorData!);
final filepath = await writePlyFile(result.faceIdSensorData!);
final bytes = ImageBuilder.fromCameraImage(result.cameraImage).asJpg();
final imagePath = await writeImageFile(bytes);

await ShareExtend.shareMultiple([
// filepath,
filepath,
imagePath,
], 'file');
showCopiedToClipboardNotification(context);
Expand Down
4 changes: 2 additions & 2 deletions ios/Classes/NativeCameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ class FLNativeView: NSObject, FlutterPlatformView {
return CameraOptions(
lensDirection: lensDirection,
enableDistortionCorrection: enableDistortionCorrection,
useDepthCamera: args["useDepthCamera"] as! Bool,
objectDetectionOptions: objectDetectionRange,
preferredFrameRate: preferredFrameRate,
preferredResolution: preferredResolution,
useDepthCamera: true
preferredResolution: preferredResolution
)
}

Expand Down
39 changes: 15 additions & 24 deletions ios/Classes/ScannerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SceneKit
import VideoToolbox
import CoreGraphics


class ScannerController: NSObject, AVCaptureDataOutputSynchronizerDelegate, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureFileOutputRecordingDelegate {

private enum SessionSetupResult {
Expand Down Expand Up @@ -538,15 +539,17 @@ class ScannerController: NSObject, AVCaptureDataOutputSynchronizerDelegate, AVCa
/// Sets up current capture session.
private func configureSession() {

let resolver = DeviceConstraintResolver()
let resolveDeviceResult: (AVCaptureDevice, Format, AVFrameRateRange)? = resolver.solve()
let resolver = DeviceConstraintResolver(cameraOptions: cameraOptions)
let resolveDeviceResult: (AVCaptureDevice, AVCaptureDevice.Format, AVFrameRateRange)? = resolver.solve()
print("\(resolveDeviceResult)")
guard let (videoDevice, format, frameRateRange) = resolveDeviceResult else {
print("Could not find any video device.")
return
}

print("Formats: \(format.supportedDepthDataFormats)")
canUseDepthCamera = !format.supportedDepthDataFormats.isEmpty
if(!canUseDepthCamera && cameraOptions.useDepthCamera) {
if (!canUseDepthCamera && cameraOptions.useDepthCamera) {
print("Depth camera is not available.")
}

Expand Down Expand Up @@ -578,8 +581,8 @@ class ScannerController: NSObject, AVCaptureDataOutputSynchronizerDelegate, AVCa
do {
try videoDevice.lockForConfiguration()
videoDevice.activeFormat = format
videoDevice.activeVideoMinFrameDuration = frameRateRange!.minFrameDuration
videoDevice.activeVideoMaxFrameDuration = frameRateRange!.minFrameDuration
videoDevice.activeVideoMinFrameDuration = frameRateRange.minFrameDuration
videoDevice.activeVideoMaxFrameDuration = frameRateRange.minFrameDuration

videoDevice.unlockForConfiguration()
} catch {
Expand Down Expand Up @@ -695,34 +698,22 @@ class ScannerController: NSObject, AVCaptureDataOutputSynchronizerDelegate, AVCa
class DeviceConstraintResolver {
private let cameraOptions: CameraOptions!

func solve() -> (AVCaptureDevice, Format, AVFrameRateRange)? {
let devices = solveForLensDirection()
init(cameraOptions: CameraOptions!) {
self.cameraOptions = cameraOptions
}

var bestDevice: AVCaptureDevice?
func solve() -> (AVCaptureDevice, AVCaptureDevice.Format, AVFrameRateRange)? {
let devices = solveForLensDirection()
var bestFrameRateDiff = Int.max
var discoveredFrameRate: Int?

let filteredDevices: [AVCaptureDevice] = []
var result: (AVCaptureDevice, AVCaptureDevice.Format, AVFrameRateRange)?
for device in devices {
if(!cameraOptions.useDepthCamera) {
filteredDevices.append(device)
continue
}
for format in device.formats {
let filtered = format.supportedDepthDataFormats.filter({
CMFormatDescriptionGetMediaSubType($0.formatDescription) == kCVPixelFormatType_DepthFloat16
})
if(filtered.isEmpty) {
if (filtered.isEmpty && cameraOptions.useDepthCamera) {
continue;
}
filteredDevices.append(device)
break
}
}
var bestFrameRateDiff = Int.max
var result: (AVCaptureDevice, Format, AVFrameRateRange)?
for device in filteredDevices {
for format in device.formats {
for range in format.videoSupportedFrameRateRanges {
let frameRate = Int(range.maxFrameRate)
let frameRateDiff = abs(frameRate - cameraOptions.preferredFrameRate.frameRate())
Expand Down
2 changes: 2 additions & 0 deletions lib/cv_camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ abstract class CvCamera {
ObjectDetectionOptions? objectDetectionOptions,
PreferredResolution? preferredResolution,
PreferredFrameRate? preferredFrameRate,
bool? useDepthCamera,
}) =>
CvCameraPlatform.instance.getCameraController(
lensDirection: lensDirection,
enableDistortionCorrection: enableDistortionCorrection,
objectDetectionOptions: objectDetectionOptions,
preferredFrameRate: preferredFrameRate,
preferredResolution: preferredResolution,
useDepthCamera: useDepthCamera,
);
}
2 changes: 2 additions & 0 deletions lib/cv_camera_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class MethodChannelCvCamera extends CvCameraPlatform {
PreferredResolution? preferredResolution,
PreferredFrameRate? preferredFrameRate,
Clock? clock,
bool? useDepthCamera,
}) {
return CameraControllerImpl(
lensDirection: lensDirection,
Expand All @@ -39,6 +40,7 @@ class MethodChannelCvCamera extends CvCameraPlatform {
objectDetectionOptions: objectDetectionOptions,
preferredFrameRate: preferredFrameRate ?? PreferredFrameRate.fps30,
preferredResolution: preferredResolution ?? PreferredResolution.x640x480,
useDepthCamera: useDepthCamera ?? false,
);
}
}
1 change: 1 addition & 0 deletions lib/cv_camera_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ abstract class CvCameraPlatform extends PlatformInterface {
ObjectDetectionOptions? objectDetectionOptions,
PreferredResolution? preferredResolution,
PreferredFrameRate? preferredFrameRate,
bool? useDepthCamera,
});
}
2 changes: 2 additions & 0 deletions lib/src/controller/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ abstract class CameraController {

PreferredFrameRate get preferredFrameRate;

bool get useDepthCamera;

PreferredResolution get preferredResolution;

/// Gets the calibration data of the current camera.
Expand Down
4 changes: 4 additions & 0 deletions lib/src/controller/camera_controller_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class CameraControllerImpl implements CameraController {
@override
late Size previewSize;

@override
final bool useDepthCamera;

CameraControllerImpl({
LensDirection? lensDirection,
required this.eventChannel,
Expand All @@ -51,6 +54,7 @@ class CameraControllerImpl implements CameraController {
this.enableDistortionCorrection = true,
required this.preferredFrameRate,
required this.preferredResolution,
required this.useDepthCamera,
Clock? clock,
}) : _lensDirection = lensDirection ?? LensDirection.front,
clock = clock ?? const Clock(),
Expand Down
1 change: 1 addition & 0 deletions lib/src/preview/camera_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class _CameraPreviewState extends State<CameraPreview> {
widget.controller.objectDetectionOptions.toJson(),
"preferredResolution": widget.controller.preferredResolution.name,
"preferredFrameRate": widget.controller.preferredFrameRate.name,
"useDepthCamera": widget.controller.useDepthCamera,
};

return Stack(
Expand Down

0 comments on commit b22f847

Please sign in to comment.