-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hackday: show a message when the picture will most likely appear too …
…dark (#38) * Show a message when the picture is too dark * Add low light icon * Refactor localizable strings * Update podspec to include resources * Remove comments
- Loading branch information
1 parent
204d8e6
commit 1173ab1
Showing
14 changed files
with
329 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Copyright (c) 2019 FINN.no AS. All rights reserved. | ||
// | ||
|
||
enum LightingCondition: String { | ||
case low | ||
case normal | ||
case high | ||
|
||
init(value: Double) { | ||
switch Int(round(value)) { | ||
case Int.min..<3: | ||
self = .low | ||
case 14...Int.max: | ||
self = .high | ||
default: | ||
self = .normal | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// Copyright (c) 2019 FINN.no AS. All rights reserved. | ||
// | ||
|
||
import CoreMedia | ||
|
||
final class LowLightService { | ||
private let maxNumberOfResults = 3 | ||
private var results = [LightingCondition]() | ||
|
||
func getLightningCondition(from sampleBuffer: CMSampleBuffer) -> LightingCondition? { | ||
let rawMetadata = CMCopyDictionaryOfAttachments( | ||
allocator: nil, | ||
target: sampleBuffer, | ||
attachmentMode: CMAttachmentMode(kCMAttachmentMode_ShouldPropagate) | ||
) | ||
|
||
do { | ||
let metadata = CFDictionaryCreateCopy(nil, rawMetadata) as NSDictionary | ||
let exifData: NSDictionary = try metadata.value(forKey: "{Exif}") | ||
let fNumber: Double = try exifData.value(forKey: kCGImagePropertyExifFNumber) | ||
let exposureTime: Double = try exifData.value(forKey: kCGImagePropertyExifExposureTime) | ||
let isoSpeedRatings: NSArray = try exifData.value(forKey: kCGImagePropertyExifISOSpeedRatings) | ||
|
||
guard let isoSpeedRating = isoSpeedRatings[0] as? Double else { | ||
throw MetatataError() | ||
} | ||
|
||
let explosureValue = log2((100 * fNumber * fNumber) / (exposureTime * isoSpeedRating)) | ||
let lightningCondition = LightingCondition(value: explosureValue) | ||
|
||
results.append(lightningCondition) | ||
|
||
if results.count == maxNumberOfResults + 1 { | ||
results = Array(results.dropFirst()) | ||
} | ||
|
||
return results.count > 1 && Set(results).count == 1 ? lightningCondition : nil | ||
} catch { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Private types | ||
|
||
private extension NSDictionary { | ||
func value<T>(forKey key: CFString) throws -> T { | ||
return try value(forKey: key as String) | ||
} | ||
|
||
func value<T>(forKey key: String) throws -> T { | ||
guard let value = self[key] as? T else { | ||
throw MetatataError() | ||
} | ||
|
||
return value | ||
} | ||
} | ||
|
||
private struct MetatataError: Error {} |
Oops, something went wrong.