nativescript-plugin-firebase-updated
Version:
204 lines (203 loc) • 7.89 kB
JavaScript
import { iOSNativeHelper as iosUtils } from "@nativescript/core/utils";
import * as application from "@nativescript/core/application";
import { MLKitCameraView as MLKitCameraViewBase } from "./mlkit-cameraview-common";
export class MLKitCameraView extends MLKitCameraViewBase {
disposeNativeView() {
super.disposeNativeView();
if (this.captureSession) {
this.captureSession.stopRunning();
this.captureSession = undefined;
}
this.captureDevice = undefined;
this.previewLayer = undefined;
this.cameraView = undefined;
application.off("orientationChanged");
}
createNativeView() {
let v = super.createNativeView();
if (this.canUseCamera()) {
this.initView();
}
else {
console.log("There's no Camera on this device :(");
}
return v;
}
canUseCamera() {
try {
return !!AVCaptureDeviceDiscoverySession &&
AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) !== null &&
NSProcessInfo.processInfo.environment.objectForKey("SIMULATOR_DEVICE_NAME") === null;
}
catch (ignore) {
return false;
}
}
initView() {
this.captureDevice = AVCaptureDeviceDiscoverySession.discoverySessionWithDeviceTypesMediaTypePosition([AVCaptureDeviceTypeBuiltInWideAngleCamera], AVMediaTypeVideo, this.preferFrontCamera ? 2 : 1).devices.firstObject;
if (this.torchOn) {
this.updateTorch();
}
this.captureSession = AVCaptureSession.new();
this.captureSession.sessionPreset = AVCaptureSessionPreset1280x720;
try {
const captureDeviceInput = AVCaptureDeviceInput.deviceInputWithDeviceError(this.captureDevice);
this.captureSession.addInput(captureDeviceInput);
}
catch (e) {
console.log("Error while trying to use the camera: " + e);
return;
}
this.previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(this.captureSession);
this.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
if (iosUtils.isLandscape()) {
const deviceOrientation = UIDevice.currentDevice.orientation;
this.previewLayer.connection.videoOrientation = deviceOrientation === 3 ? 3 : 4;
}
else {
this.previewLayer.connection.videoOrientation = 1;
}
application.off("orientationChanged");
application.on("orientationChanged", this.rotateOnOrientationChange.bind(this));
setTimeout(() => {
if (this.ios) {
this.ios.layer.addSublayer(this.previewLayer);
}
if (!this.pause) {
this.captureSession.startRunning();
}
this.cameraView = TNSMLKitCameraView.alloc().initWithCaptureSession(this.captureSession);
this.cameraView.processEveryXFrames = this.processEveryNthFrame;
if (this.rotateRecording()) {
this.cameraView.imageOrientation = 3;
}
this.cameraView.delegate = TNSMLKitCameraViewDelegateImpl.createWithOwnerResultCallbackAndOptions(new WeakRef(this), data => { }, this.preProcessImage, {});
}, 0);
}
rotateOnOrientationChange(args) {
if (this.previewLayer) {
if (args.newValue === "landscape") {
const deviceOrientation = UIDevice.currentDevice.orientation;
this.previewLayer.connection.videoOrientation = deviceOrientation === 3 ? 3 : 4;
}
else if (args.newValue === "portrait") {
this.previewLayer.connection.videoOrientation = 1;
}
}
}
onLayout(left, top, right, bottom) {
super.onLayout(left, top, right, bottom);
if (this.previewLayer && this.ios && this.canUseCamera()) {
this.previewLayer.frame = this.ios.layer.bounds;
}
}
getVisionOrientation(imageOrientation) {
if (imageOrientation === 0) {
return 1;
}
else if (imageOrientation === 1) {
return 3;
}
else if (imageOrientation === 2) {
return 8;
}
else if (imageOrientation === 3) {
return 6;
}
else if (imageOrientation === 4) {
return 2;
}
else if (imageOrientation === 5) {
return 4;
}
else if (imageOrientation === 6) {
return 5;
}
else if (imageOrientation === 7) {
return 7;
}
else {
return 1;
}
}
updateTorch() {
const device = this.captureDevice;
if (device && device.hasTorch && device.lockForConfiguration()) {
if (this.torchOn) {
device.torchMode = 1;
device.flashMode = 1;
}
else {
device.torchMode = 0;
device.flashMode = 0;
}
device.unlockForConfiguration();
}
}
pauseScanning() {
if (this.captureSession && this.captureSession.running) {
this.captureSession.stopRunning();
}
}
resumeScanning() {
if (this.captureSession && !this.captureSession.running) {
this.captureSession.startRunning();
}
}
runDetector(image, onComplete) {
throw new Error("No custom detector implemented, so 'runDetector' can't do its thing");
}
}
class TNSMLKitCameraViewDelegateImpl extends NSObject {
constructor() {
super(...arguments);
this.detectorBusy = false;
}
static createWithOwnerResultCallbackAndOptions(owner, callback, preProcessImageCallback, options) {
if (TNSMLKitCameraViewDelegateImpl.ObjCProtocols.length === 0 && typeof (TNSMLKitCameraViewDelegate) !== "undefined") {
TNSMLKitCameraViewDelegateImpl.ObjCProtocols.push(TNSMLKitCameraViewDelegate);
}
let delegate = TNSMLKitCameraViewDelegateImpl.new();
delegate.owner = owner;
delegate.options = options;
delegate.resultCallback = callback;
delegate.preProcessImageCallback = preProcessImageCallback;
delegate.detector = owner.get().createDetector();
delegate.onSuccessListener = owner.get().createSuccessListener();
return delegate;
}
cameraDidOutputImage(image) {
if (!image || this.detectorBusy) {
return;
}
this.detectorBusy = true;
const onComplete = () => {
this.detectorBusy = false;
};
this.owner.get().lastVisionImage = image;
if (this.detector.detectInImageCompletion) {
this.detector.detectInImageCompletion(this.uiImageToFIRVisionImage(image), (result, error) => {
this.onSuccessListener(result, error);
onComplete();
});
}
else if (this.detector.processImageCompletion) {
this.detector.processImageCompletion(this.uiImageToFIRVisionImage(image), (result, error) => {
this.onSuccessListener(result, error);
onComplete();
});
}
else {
this.owner.get().runDetector(image, onComplete);
}
}
uiImageToFIRVisionImage(image) {
image = this.preProcessImageCallback(image);
const fIRVisionImage = FIRVisionImage.alloc().initWithImage(image);
const fIRVisionImageMetadata = FIRVisionImageMetadata.new();
fIRVisionImageMetadata.orientation = this.owner.get().getVisionOrientation(image.imageOrientation);
fIRVisionImage.metadata = fIRVisionImageMetadata;
return fIRVisionImage;
}
}
TNSMLKitCameraViewDelegateImpl.ObjCProtocols = [];