Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new function to get horizontal field of view of camera #260

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,24 @@ import { CameraPreviewFlashMode } from '@capacitor-community/camera-preview';
const flashModes = await CameraPreview.getSupportedFlashModes();
const supportedFlashModes: CameraPreviewFlashMode[] = flashModes.result;
```



### getHorizontalFov()

<info>Get the horizontal FOV of camera the camera device currently started. Returns a float <br/>

Please note that camera should be already started to get this value.

```javascript


const cameraFovRequest = await CameraPreview.getHorizontalFov();
const cameraFovRequest = cameraFovRequest.result;
```



### setFlashMode(options)

<info>Set the flash mode. See <code>[FLASH_MODE](#camera_Settings.FlashMode)</code> for details about the possible values for flashMode.</info><br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ public void getSupportedFlashModes(PluginCall call) {
call.resolve(jsObject);
}




@PluginMethod
public void getHorizontalFov(PluginCall call) {
if (this.hasCamera(call) == false) {
call.reject("Camera is not running");
return;
}

Camera camera = fragment.getCamera();
Camera.Parameters params = camera.getParameters();

float horizontalViewAngle = params.getHorizontalViewAngle();

JSObject jsObject = new JSObject();
jsObject.put("result", horizontalViewAngle);
call.resolve(jsObject);
}



@PluginMethod
public void setFlashMode(PluginCall call) {
if (this.hasCamera(call) == false) {
Expand Down
19 changes: 19 additions & 0 deletions ios/Plugin/CameraController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,26 @@ extension CameraController {
return supportedFlashModesAsStrings

}
func getHorizontalFov() throws -> String {
var currentCamera: AVCaptureDevice?
switch currentCameraPosition {
case .front:
currentCamera = self.frontCamera!
case .rear:
currentCamera = self.rearCamera!
default: break
}

guard
let device = currentCamera
else {
throw CameraControllerError.noCamerasAvailable
}

let horizontalFov = String(device.activeFormat.videoFieldOfView)
return horizontalFov

}
func setFlashMode(flashMode: AVCaptureDevice.FlashMode) throws {
var currentCamera: AVCaptureDevice?
switch currentCameraPosition {
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
CAP_PLUGIN_METHOD(captureSample, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(flip, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getSupportedFlashModes, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getHorizontalFov, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setFlashMode, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(startRecordVideo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(stopRecordVideo, CAPPluginReturnPromise);
Expand Down
12 changes: 12 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ public class CameraPreview: CAPPlugin {
}
}




@objc func getHorizontalFov(_ call: CAPPluginCall) {
do {
let horizontalFov = try self.cameraController.getHorizontalFov()
call.resolve(["result": horizontalFov])
} catch {
call.reject("failed to get FOV")
}
}

@objc func setFlashMode(_ call: CAPPluginCall) {
guard let flashMode = call.getString("flashMode") else {
call.reject("failed to set flash mode. required parameter flashMode is missing")
Expand Down
3 changes: 3 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export interface CameraPreviewPlugin {
getSupportedFlashModes(): Promise<{
result: CameraPreviewFlashMode[];
}>;
getHorizontalFov(): Promise<{
result: any;
}>;
setFlashMode(options: { flashMode: CameraPreviewFlashMode | string }): Promise<void>;
flip(): Promise<void>;
setOpacity(options: CameraOpacityOptions): Promise<{}>;
Expand Down
9 changes: 9 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ export class CameraPreviewWeb extends WebPlugin implements CameraPreviewPlugin {
throw new Error('getSupportedFlashModes not supported under the web platform');
}


async getHorizontalFov(): Promise<{
result: any;
}> {
throw new Error('getHorizontalFov not supported under the web platform');
}



async setFlashMode(_options: { flashMode: CameraPreviewFlashMode | string }): Promise<void> {
throw new Error('setFlashMode not supported under the web platform');
}
Expand Down