Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Allow choosing front or back camera #94

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
//import android.graphics.Camera;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -48,7 +49,7 @@ public class BarcodeScanner extends Plugin implements BarcodeCallback {

private BarcodeView mBarcodeView;

private int currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
//private int currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;

private boolean isScanning = false;
private boolean shouldRunScan = false;
Expand Down Expand Up @@ -93,7 +94,7 @@ private boolean hasCamera() {
}
}

private void setupCamera() {
private void setupCamera(String cameraDirection) {
// @TODO(): add support for switching cameras
// @TODO(): add support for toggling torch

Expand All @@ -105,7 +106,9 @@ private void setupCamera() {

// Configure the camera (front/back)
CameraSettings settings = new CameraSettings();
settings.setRequestedCameraId(currentCameraId);
settings.setRequestedCameraId(
"front".equals(cameraDirection) ? Camera.CameraInfo.CAMERA_FACING_FRONT : Camera.CameraInfo.CAMERA_FACING_BACK
);
settings.setContinuousFocusEnabled(true);
mBarcodeView.setCameraSettings(settings);

Expand Down Expand Up @@ -153,13 +156,13 @@ private void dismantleCamera() {
}
}

private void prepare() {
private void _prepare(PluginCall call) {
// undo previous setup
// because it may be prepared with a different config
dismantleCamera();

// setup camera with new config
setupCamera();
setupCamera(call.getString("cameraDirection", "back"));

// indicate this method was run
didRunCameraPrepare = true;
Expand Down Expand Up @@ -225,7 +228,7 @@ private void scan() {
Log.d("scanner", "No permission to use camera. Did you request it yet?");
} else {
shouldRunScan = true;
prepare();
_prepare(getSavedCall());
}
}
} else {
Expand Down Expand Up @@ -310,7 +313,7 @@ public void possibleResultPoints(List<ResultPoint> resultPoints) {}

@PluginMethod
public void prepare(PluginCall call) {
prepare();
_prepare(call);
call.resolve();
}

Expand Down Expand Up @@ -476,7 +479,7 @@ public void checkPermission(PluginCall call) {
_checkPermission(call, true);
} else {
_checkPermission(call, false);
}
}
}

@PluginMethod
Expand Down
34 changes: 20 additions & 14 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public class BarcodeScanner: CAPPlugin, AVCaptureMetadataOutputObjectsDelegate {
var captureVideoPreviewLayer:AVCaptureVideoPreviewLayer?
var metaOutput: AVCaptureMetadataOutput?

var currentCamera: Int = 0;
var frontCamera: AVCaptureDevice?
var backCamera: AVCaptureDevice?

Expand Down Expand Up @@ -134,8 +133,9 @@ public class BarcodeScanner: CAPPlugin, AVCaptureMetadataOutputObjectsDelegate {
return false;
}

private func setupCamera() -> Bool {
private func setupCamera(cameraDirection: String? = "back") -> Bool {
do {
var cameraDir = cameraDirection
cameraView.backgroundColor = UIColor.clear
self.webView!.superview!.insertSubview(cameraView, belowSubview: self.webView!)

Expand All @@ -149,11 +149,17 @@ public class BarcodeScanner: CAPPlugin, AVCaptureMetadataOutputObjectsDelegate {
}
}
// older iPods have no back camera
if(backCamera == nil){
currentCamera = 1
if (cameraDir == "back") {
if (backCamera == nil) {
cameraDir = "front"
}
} else {
if (frontCamera == nil) {
cameraDir = "back"
}
}
let input: AVCaptureDeviceInput
input = try self.createCaptureDeviceInput()
input = try self.createCaptureDeviceInput(cameraDirection: cameraDir)
captureSession = AVCaptureSession()
captureSession!.addInput(input)
metaOutput = AVCaptureMetadataOutput()
Expand All @@ -178,15 +184,15 @@ public class BarcodeScanner: CAPPlugin, AVCaptureMetadataOutputObjectsDelegate {
@available(swift, deprecated: 5.6, message: "New Xcode? Check if `AVCaptureDevice.DeviceType` has new types and add them accordingly.")
private func discoverCaptureDevices() -> [AVCaptureDevice] {
if #available(iOS 13.0, *) {
return AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualCamera, .builtInDualWideCamera, .builtInWideAngleCamera, .builtInUltraWideCamera, .builtInTelephotoCamera, .builtInTrueDepthCamera], mediaType: .video, position: .front).devices
return AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualCamera, .builtInTelephotoCamera, .builtInTrueDepthCamera, .builtInUltraWideCamera, .builtInDualWideCamera, .builtInWideAngleCamera], mediaType: .video, position: .unspecified).devices
} else {
return AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera, .builtInWideAngleCamera, .builtInTelephotoCamera, .builtInTrueDepthCamera], mediaType: .video, position: .front).devices
return AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera, .builtInWideAngleCamera, .builtInTelephotoCamera, .builtInTrueDepthCamera], mediaType: .video, position: .unspecified).devices
}
}

private func createCaptureDeviceInput() throws -> AVCaptureDeviceInput {
private func createCaptureDeviceInput(cameraDirection: String? = "back") throws -> AVCaptureDeviceInput {
var captureDevice: AVCaptureDevice
if(currentCamera == 0){
if(cameraDirection == "back"){
if(backCamera != nil){
captureDevice = backCamera!
} else {
Expand Down Expand Up @@ -218,7 +224,7 @@ public class BarcodeScanner: CAPPlugin, AVCaptureMetadataOutputObjectsDelegate {
self.captureVideoPreviewLayer = nil
self.metaOutput = nil
self.captureSession = nil
self.currentCamera = 0
// self.currentCamera = 0
self.frontCamera = nil
self.backCamera = nil
}
Expand All @@ -234,14 +240,14 @@ public class BarcodeScanner: CAPPlugin, AVCaptureMetadataOutputObjectsDelegate {
}
}

private func prepare() {
private func prepare(_ call: CAPPluginCall? = nil) {
// undo previous setup
// because it may be prepared with a different config
self.dismantleCamera()

DispatchQueue.main.async {
// setup camera with new config
if (self.setupCamera()) {
if (self.setupCamera(cameraDirection: call?.getString("cameraDirection") ?? "back")) {
// indicate this method was run
self.didRunCameraPrepare = true

Expand All @@ -267,7 +273,7 @@ public class BarcodeScanner: CAPPlugin, AVCaptureMetadataOutputObjectsDelegate {
// requestPermission()
} else {
self.shouldRunScan = true
self.prepare()
self.prepare(savedCall)
}
} else {
self.didRunCameraPrepare = false
Expand Down Expand Up @@ -363,7 +369,7 @@ public class BarcodeScanner: CAPPlugin, AVCaptureMetadataOutputObjectsDelegate {
}

@objc func prepare(_ call: CAPPluginCall) {
self.prepare()
self.prepare(call)
call.resolve()
}

Expand Down
7 changes: 6 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface BarcodeScannerPlugin {
prepare(): Promise<void>;
prepare(options?: ScanOptions): Promise<void>;
hideBackground(): Promise<void>;
showBackground(): Promise<void>;
startScan(options?: ScanOptions): Promise<ScanResult>;
Expand Down Expand Up @@ -80,6 +80,10 @@ export enum SupportedFormat {
// END 2D
}

export enum CameraDirection {
FRONT = 'front',
BACK = 'back',
}
export interface ScanOptions {
/**
* This parameter can be used to make the scanner only recognize specific types of barcodes.
Expand All @@ -88,6 +92,7 @@ export interface ScanOptions {
* @since 1.2.0
*/
targetedFormats?: SupportedFormat[];
cameraDirection?: CameraDirection;
}

export interface StopScanOptions {
Expand Down