Skip to content

Commit 3f9c315

Browse files
committed
chore(ios): working iOS cameraview
1 parent 82c33c9 commit 3f9c315

File tree

7 files changed

+367
-76
lines changed

7 files changed

+367
-76
lines changed

packages/ui-cameraview/platforms/ios/src/CameraView.swift

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import UIKit
2+
import CoreVideo
3+
import CoreMedia
4+
import Foundation
5+
import NextLevel
6+
import AVFoundation
7+
8+
struct RuntimeError: LocalizedError {
9+
let description: String
10+
11+
init(_ description: String) {
12+
self.description = description
13+
}
14+
15+
var errorDescription: String? {
16+
description
17+
}
18+
}
19+
20+
@objcMembers
21+
@objc(NSCameraView)
22+
public class NSCameraView: UIView, NextLevelVideoDelegate {
23+
public weak var processingDelegate: ProcessRawVideoSampleBufferDelegate?
24+
var nextLevel: NextLevel?
25+
26+
override init(frame: CGRect) {
27+
super.init(frame: frame )
28+
commonInit()
29+
}
30+
31+
required init?(coder: NSCoder) {
32+
super.init(coder: coder)
33+
commonInit()
34+
}
35+
deinit {
36+
self.processingDelegate = nil
37+
}
38+
39+
public override var frame: CGRect {
40+
get {
41+
return super.frame
42+
}
43+
set {
44+
super.frame = newValue
45+
self.nextLevel?.previewLayer.frame = self.bounds
46+
}
47+
}
48+
49+
public var flashMode: Int {
50+
get {
51+
return (self.nextLevel?.flashMode ?? NextLevelFlashMode.off).rawValue
52+
}
53+
set {
54+
self.nextLevel?.flashMode = NextLevelFlashMode(rawValue: newValue)!
55+
}
56+
}
57+
public var torchMode: Int {
58+
get {
59+
return (self.nextLevel?.torchMode ?? NextLevelTorchMode.off).rawValue
60+
}
61+
set {
62+
self.nextLevel?.torchMode = NextLevelTorchMode(rawValue: newValue)!
63+
}
64+
}
65+
66+
func commonInit() {
67+
self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
68+
self.backgroundColor = UIColor.black
69+
70+
self.nextLevel = NextLevel()
71+
if let nextLevel = self.nextLevel {
72+
// Configure NextLevel by modifying the configuration ivars
73+
self.layer.addSublayer(nextLevel.previewLayer)
74+
nextLevel.previewLayer.frame = self.bounds
75+
// nextLevel.delegate = self
76+
// nextLevel.deviceDelegate = self
77+
// nextLevel.flashDelegate = self
78+
nextLevel.videoDelegate = self
79+
// nextLevel.photoDelegate = self
80+
// nextLevel.metadataObjectsDelegate = self
81+
82+
nextLevel.automaticallyUpdatesDeviceOrientation = true
83+
84+
// video configuration
85+
nextLevel.videoConfiguration.preset = AVCaptureSession.Preset.hd1280x720
86+
nextLevel.videoConfiguration.bitRate = 5500000
87+
nextLevel.videoConfiguration.maxKeyFrameInterval = 30
88+
nextLevel.videoConfiguration.profileLevel = AVVideoProfileLevelH264HighAutoLevel
89+
90+
// audio configuration
91+
// for now disable audio
92+
nextLevel.captureMode = NextLevelCaptureMode.videoWithoutAudio
93+
// nextLevel.audioConfiguration.bitRate = 96000
94+
// nextLevel.disableAudioInputDevice()
95+
// metadata objects configuration
96+
// nextLevel.metadataObjectTypes = [AVMetadataObject.ObjectType.face, AVMetadataObject.ObjectType.qr]
97+
}
98+
}
99+
100+
func startPreview() throws {
101+
do {
102+
try self.nextLevel?.start()
103+
} catch {
104+
let nextLevelError = error as! NextLevelError
105+
// re throw the error with the description so that N can correctly show the error message
106+
throw RuntimeError(nextLevelError.description)
107+
}
108+
109+
}
110+
111+
func stopPreview() {
112+
self.nextLevel?.stop()
113+
}
114+
func toggleCamera() {
115+
self.nextLevel?.flipCaptureDevicePosition()
116+
}
117+
118+
public func nextLevel(_ nextLevel: NextLevel, willProcessRawVideoSampleBuffer sampleBuffer: CMSampleBuffer, onQueue queue: DispatchQueue) {
119+
self.processingDelegate?.cameraView(self, willProcessRawVideoSampleBuffer: sampleBuffer, onQueue: queue)
120+
}
121+
122+
public func nextLevel(_ nextLevel: NextLevel, renderToCustomContextWithImageBuffer imageBuffer: CVPixelBuffer, onQueue queue: DispatchQueue) {
123+
self.processingDelegate?.cameraView(self, renderToCustomContextWithImageBuffer: imageBuffer, onQueue: queue)
124+
}
125+
126+
public func nextLevel(_ nextLevel: NextLevel, didUpdateVideoZoomFactor videoZoomFactor: Float) {
127+
128+
}
129+
130+
public func nextLevel(_ nextLevel: NextLevel, willProcessFrame frame: AnyObject, timestamp: TimeInterval, onQueue queue: DispatchQueue) {
131+
132+
}
133+
134+
public func nextLevel(_ nextLevel: NextLevel, didSetupVideoInSession session: NextLevelSession) {
135+
136+
}
137+
138+
public func nextLevel(_ nextLevel: NextLevel, didSetupAudioInSession session: NextLevelSession) {
139+
140+
}
141+
142+
public func nextLevel(_ nextLevel: NextLevel, didStartClipInSession session: NextLevelSession) {
143+
144+
}
145+
146+
public func nextLevel(_ nextLevel: NextLevel, didCompleteClip clip: NextLevelClip, inSession session: NextLevelSession) {
147+
148+
}
149+
150+
public func nextLevel(_ nextLevel: NextLevel, didAppendVideoSampleBuffer sampleBuffer: CMSampleBuffer, inSession session: NextLevelSession) {
151+
152+
}
153+
154+
public func nextLevel(_ nextLevel: NextLevel, didSkipVideoSampleBuffer sampleBuffer: CMSampleBuffer, inSession session: NextLevelSession) {
155+
156+
}
157+
158+
public func nextLevel(_ nextLevel: NextLevel, didAppendVideoPixelBuffer pixelBuffer: CVPixelBuffer, timestamp: TimeInterval, inSession session: NextLevelSession) {
159+
160+
}
161+
162+
public func nextLevel(_ nextLevel: NextLevel, didSkipVideoPixelBuffer pixelBuffer: CVPixelBuffer, timestamp: TimeInterval, inSession session: NextLevelSession) {
163+
164+
}
165+
166+
public func nextLevel(_ nextLevel: NextLevel, didAppendAudioSampleBuffer sampleBuffer: CMSampleBuffer, inSession session: NextLevelSession) {
167+
168+
}
169+
170+
public func nextLevel(_ nextLevel: NextLevel, didSkipAudioSampleBuffer sampleBuffer: CMSampleBuffer, inSession session: NextLevelSession) {
171+
172+
}
173+
174+
public func nextLevel(_ nextLevel: NextLevel, didCompleteSession session: NextLevelSession) {
175+
176+
}
177+
178+
public func nextLevel(_ nextLevel: NextLevel, didCompletePhotoCaptureFromVideoFrame photoDict: [String : Any]?) {
179+
180+
}
181+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import CoreVideo
2+
import CoreMedia
3+
import Foundation
4+
5+
@objc(ProcessRawVideoSampleBufferDelegate)
6+
public protocol ProcessRawVideoSampleBufferDelegate: AnyObject {
7+
func cameraView(_ cameraView: NSCameraView, willProcessRawVideoSampleBuffer sampleBuffer: CMSampleBuffer, onQueue queue: DispatchQueue)
8+
func cameraView(_ cameraView: NSCameraView, renderToCustomContextWithImageBuffer imageBuffer: CVPixelBuffer, onQueue queue: DispatchQueue)
9+
}

src/ui-cameraview/index.android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ export class CameraView extends CameraViewBase {
7272

7373
createProcessor() {
7474
if (!this.processor) {
75-
(this.processor = new com.nativescript.cameraview.ImageAnalysisCallback({
75+
this.processor = new com.nativescript.cameraview.ImageAnalysisCallback({
7676
process: (image, info, processor) => {
7777
try {
7878
this.notify({ eventName: 'frame', object: this, image, info, processor });
7979
} catch (err) {
8080
console.log('process error', err, err.stack);
8181
}
8282
}
83-
}));
83+
});
8484
}
8585
}
8686
detachProcessor() {

0 commit comments

Comments
 (0)