capacitor-camera-module 0.0.63 → 0.0.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -51,11 +51,9 @@ public class CameraModulePlugin: CAPPlugin,CAPBridgedPlugin,AVCaptureVideoDataOu
|
|
|
51
51
|
|
|
52
52
|
// MARK: - Lifecycle
|
|
53
53
|
public override func load() {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
webView.backgroundColor = .black
|
|
58
|
-
webView.scrollView.backgroundColor = .black
|
|
54
|
+
// Este método ya no fuerza el color negro
|
|
55
|
+
// El webView será configurado dinámicamente en startPreview/stopPreview
|
|
56
|
+
print("📱 CameraModulePlugin cargado")
|
|
59
57
|
}
|
|
60
58
|
|
|
61
59
|
|
|
@@ -144,117 +142,153 @@ public class CameraModulePlugin: CAPPlugin,CAPBridgedPlugin,AVCaptureVideoDataOu
|
|
|
144
142
|
|
|
145
143
|
// MARK: - Camera Preview
|
|
146
144
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
145
|
+
@objc func startPreview(_ call: CAPPluginCall) {
|
|
146
|
+
DispatchQueue.main.async {
|
|
147
|
+
if self.previewView != nil {
|
|
148
|
+
call.resolve()
|
|
149
|
+
return
|
|
150
|
+
}
|
|
153
151
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
152
|
+
guard AVCaptureDevice.authorizationStatus(for: .video) == .authorized else {
|
|
153
|
+
call.reject("Camera permission not granted")
|
|
154
|
+
return
|
|
155
|
+
}
|
|
158
156
|
|
|
159
|
-
|
|
157
|
+
let session = AVCaptureSession()
|
|
160
158
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
159
|
+
guard let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back),
|
|
160
|
+
let input = try? AVCaptureDeviceInput(device: device) else {
|
|
161
|
+
call.reject("Camera not available")
|
|
162
|
+
return
|
|
163
|
+
}
|
|
166
164
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
165
|
+
if session.canAddInput(input) {
|
|
166
|
+
session.addInput(input)
|
|
167
|
+
}
|
|
170
168
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
169
|
+
if session.canAddOutput(self.photoOutput) {
|
|
170
|
+
session.addOutput(self.photoOutput)
|
|
171
|
+
}
|
|
174
172
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
173
|
+
guard let container = self.bridge?.viewController?.view else {
|
|
174
|
+
call.reject("No container view")
|
|
175
|
+
return
|
|
176
|
+
}
|
|
179
177
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
previewView.isUserInteractionEnabled = true // CAMBIO IMPORTANTE
|
|
178
|
+
guard let webView = self.bridge?.webView as? WKWebView else {
|
|
179
|
+
call.reject("No webView")
|
|
180
|
+
return
|
|
181
|
+
}
|
|
185
182
|
|
|
183
|
+
// 1. Hacer el webView transparente ANTES de crear el preview
|
|
184
|
+
webView.isOpaque = false
|
|
185
|
+
webView.backgroundColor = .clear
|
|
186
|
+
webView.scrollView.backgroundColor = .clear
|
|
186
187
|
|
|
187
|
-
|
|
188
|
+
// Si necesitas mantener scroll pero transparente:
|
|
189
|
+
webView.scrollView.isOpaque = false
|
|
188
190
|
|
|
191
|
+
// 2. Crear previewView
|
|
192
|
+
let previewView = UIView()
|
|
193
|
+
previewView.translatesAutoresizingMaskIntoConstraints = false
|
|
194
|
+
previewView.backgroundColor = .black // Temporal para debug
|
|
195
|
+
previewView.isUserInteractionEnabled = false // Importante: no intercepta toques
|
|
189
196
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
197
|
+
// DEBUG: Borde para ver los límites
|
|
198
|
+
previewView.layer.borderWidth = 2
|
|
199
|
+
previewView.layer.borderColor = UIColor.green.cgColor
|
|
193
200
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
return
|
|
197
|
-
}
|
|
201
|
+
print("🔍 DEBUG: Container frame: \(container.frame)")
|
|
202
|
+
print("🔍 DEBUG: WebView frame: \(webView.frame)")
|
|
198
203
|
|
|
199
|
-
|
|
200
|
-
|
|
204
|
+
// 3. Insertar previewView DETRÁS de todo (posición 0)
|
|
205
|
+
container.insertSubview(previewView, at: 0)
|
|
201
206
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
])
|
|
207
|
+
// 4. Configurar constraints para que ocupe TODA la pantalla
|
|
208
|
+
NSLayoutConstraint.activate([
|
|
209
|
+
previewView.topAnchor.constraint(equalTo: container.topAnchor),
|
|
210
|
+
previewView.bottomAnchor.constraint(equalTo: container.bottomAnchor),
|
|
211
|
+
previewView.leadingAnchor.constraint(equalTo: container.leadingAnchor),
|
|
212
|
+
previewView.trailingAnchor.constraint(equalTo: container.trailingAnchor)
|
|
213
|
+
])
|
|
210
214
|
|
|
211
|
-
|
|
212
|
-
|
|
215
|
+
// 5. Asegurar orden de capas
|
|
216
|
+
previewView.layer.zPosition = -1 // Detrás de todo
|
|
217
|
+
webView.layer.zPosition = 0 // Normal
|
|
213
218
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
previewLayer.videoGravity = .resizeAspectFill
|
|
217
|
-
previewLayer.frame = previewView.bounds
|
|
218
|
-
previewLayer.masksToBounds = true
|
|
219
|
+
// 6. Forzar layout
|
|
220
|
+
container.layoutIfNeeded()
|
|
219
221
|
|
|
220
|
-
|
|
222
|
+
print("🔍 DEBUG: PreviewView frame después de layout: \(previewView.frame)")
|
|
221
223
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
224
|
+
// 7. Crear previewLayer
|
|
225
|
+
let previewLayer = AVCaptureVideoPreviewLayer(session: session)
|
|
226
|
+
previewLayer.videoGravity = .resizeAspectFill
|
|
227
|
+
previewLayer.frame = previewView.bounds
|
|
228
|
+
previewLayer.masksToBounds = true
|
|
226
229
|
|
|
227
|
-
|
|
228
|
-
|
|
230
|
+
// DEBUG: Borde para el layer también
|
|
231
|
+
previewLayer.borderWidth = 1
|
|
232
|
+
previewLayer.borderColor = UIColor.yellow.cgColor
|
|
229
233
|
|
|
230
|
-
|
|
231
|
-
DispatchQueue.global(qos: .userInitiated).async {
|
|
232
|
-
session.startRunning()
|
|
234
|
+
previewView.layer.insertSublayer(previewLayer, at: 0)
|
|
233
235
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
236
|
+
// 8. Guardar referencias
|
|
237
|
+
self.previewView = previewView
|
|
238
|
+
self.videoPreviewLayer = previewLayer
|
|
239
|
+
self.captureSession = session
|
|
240
|
+
|
|
241
|
+
// 9. Configurar sesión
|
|
242
|
+
session.sessionPreset = .photo
|
|
243
|
+
|
|
244
|
+
// 10. Iniciar sesión en background
|
|
245
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
246
|
+
session.startRunning()
|
|
247
|
+
|
|
248
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
|
249
|
+
print("✅ Cámara iniciada")
|
|
250
|
+
print("📍 Preview visible: \(previewView.window != nil ? "SÍ" : "NO")")
|
|
251
|
+
print("📍 WebView transparente: \(webView.isOpaque ? "NO" : "SÍ")")
|
|
252
|
+
|
|
253
|
+
// Quitar borde de debug después de 3 segundos
|
|
254
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
|
|
255
|
+
previewView.layer.borderWidth = 0
|
|
256
|
+
previewLayer.borderWidth = 0
|
|
257
|
+
previewView.backgroundColor = .clear // Ahora sí transparente
|
|
239
258
|
}
|
|
240
259
|
}
|
|
241
|
-
|
|
242
|
-
call.resolve()
|
|
243
260
|
}
|
|
261
|
+
|
|
262
|
+
call.resolve([
|
|
263
|
+
"success": true,
|
|
264
|
+
"message": "Preview iniciado con webView transparente"
|
|
265
|
+
])
|
|
244
266
|
}
|
|
267
|
+
}
|
|
245
268
|
|
|
246
269
|
|
|
247
270
|
@objc func stopPreview(_ call: CAPPluginCall) {
|
|
248
271
|
DispatchQueue.main.async {
|
|
272
|
+
// Detener cámara
|
|
249
273
|
self.captureSession?.stopRunning()
|
|
250
274
|
self.captureSession = nil
|
|
275
|
+
|
|
276
|
+
// Remover preview
|
|
251
277
|
self.videoPreviewLayer?.removeFromSuperlayer()
|
|
252
278
|
self.previewView?.removeFromSuperview()
|
|
253
279
|
self.videoPreviewLayer?.session = nil
|
|
254
|
-
|
|
255
280
|
self.videoPreviewLayer = nil
|
|
256
|
-
|
|
257
281
|
self.previewView = nil
|
|
282
|
+
|
|
283
|
+
// Restaurar webView a estado normal (opcional)
|
|
284
|
+
if let webView = self.bridge?.webView as? WKWebView {
|
|
285
|
+
webView.isOpaque = true
|
|
286
|
+
webView.backgroundColor = .white // O el color de tu app
|
|
287
|
+
webView.scrollView.backgroundColor = .white
|
|
288
|
+
webView.scrollView.isOpaque = true
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
print("🛑 Preview detenido - webView restaurado")
|
|
258
292
|
call.resolve()
|
|
259
293
|
}
|
|
260
294
|
}
|