User Guide on iOS
The Dynamsoft Camera Enhancer iOS SDK enables you to easily control cameras from your iOS applications to stream live video and acquire realtime frames.
Example Usage
See how Dynamsoft Camera Enhancer helps in camera control and video recognition:
- Barcode scanning from video stream: check Dynamsoft Barcode Reader iOS User Guide
Step-by-step guide on how to integrate Dynamsoft Camera Enhancer SDK to your iOS app:
App Prerequisites
- Supported OS: iOS 11 or higher (iOS 13 and higher recommended).
- Supported ABI: arm64 and x86_64.
- Development Environment: Xcode 13 and above (Xcode 14.1+ recommended).
Add the SDK
-
Add the frameworks in your Podfile, replace
TargetName
with your real target name.target 'HelloWorld' do use_frameworks! pod 'DynamsoftCameraEnhancer','4.2.10' pod 'DynamsoftCore','3.2.30' pod 'DynamsoftLicense','3.2.20' end
-
Execute the pod command to install the frameworks and generate workspace([TargetName].xcworkspace):
pod install
Build Your First Application with Dynamsoft Camera Enhancer
The following sample will demonstrate how to acquire a frame from video streaming by DCE.
Create a New Project and Include Dynamsoft Camera Enhancer
-
Create a new Objective-C or Swift project.
-
Drag and drop the DynamsoftCameraEnhancer.xcframework into your Xcode project. Make sure to check Copy items if needed and Create groups to copy the framework into your project’s folder.
-
Click on the project. Go to the General –> Frameworks –> Libraries and Embedded Content. Set the Embed type to Embed & Sign.
-
Go to the Build Settings –> Build Options –> Validate Workspace. Set the Validate Workspace to yes.
-
In the
ViewController.m
orViewController.swift
Import Dynamsoft Camera Enhancer.
- Objective-C
- Swift
#import <DynamsoftCameraEnhancer/DynamsoftCameraEnhancer.h>
import DynamsoftCameraEnhancer
Now Dynamsoft Camera Enhancer is added to your project.
License Activation (Optional)
A valid license is required when using the following features:
- Frame Sharpness Filter
- Sensor Filter
- Auto Zoom
- Enhanced Focus
- Fast Mode
- Smart torch
The above features are enabled by triggering method enableFeatures
. If you are not using these features, you can skip the license activation step.
To activate the license:
-
Add DynamsoftLicense.xcframework to your project and include
DynamsoftLicense
in yourAppDelegate
- Objective-C
- Swift
-
#import <DynamsoftLicense/DynamsoftLicense.h>
-
import DynamsoftLicense
-
Initialize the license in your code.
- Objective-C
- Swift
-
@interface AppDelegate ()<DBRLicenseVerificationListener> ... - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [DSLicenseManager initLicense:@"DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" verificationDelegate:self]; ... } - (void)onLicenseVerified:(BOOL)isSuccess error:(NSError *)error { [self verificationCallback:error]; }
-
class AppDelegate: UIResponder, UIApplicationDelegate, DBRLicenseVerificationListener { ... func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { ... LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", verificationDelegate: self) ... } func onLicenseVerified(_ isSuccess: Bool, error: Error?) { if !isSuccess { if let error = error { print("\(error.localizedDescription)") } } } }
Note:
- Network connection is required for the license to work.
- “DLS2***” is a time-limited public trial license used in the sample.
- You can request a 30-day offline trial license via the Request a Trial License link.
Initialize the Camera View and Control the Camera
In this section, we continue working on the ViewController
file in the project. You will learn how to create a simple camera app.
Step 1.1
Delcare the DCE & DCECameraView property.
- Objective-C
- Swift
@interface ViewController ()<DSVideoFrameListener> @property (nonatomic, strong) DSCameraView *cameraView; @property (nonatomic, strong) DSCameraEnhancer *dce; @end
var cameraView:CameraView! let dce:CameraEnhancer = .init()
Step 1.2
Initialize the DCE & DCECameraView in a method.
- Objective-C
- Swift
- (void)setUpCamera { self.cameraView = [[DSCameraView alloc] initWithFrame:self.view.bounds]; self.cameraView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight; [self.view insertSubview:self.cameraView atIndex:0]; self.dce = [[DSCameraEnhancer alloc] init]; self.dce.cameraView = self.cameraView; [self.dce addListener:self]; }
func setUpCamera() { cameraView = .init(frame: view.bounds) cameraView.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.insertSubview(cameraView, at: 0) dce.cameraView = cameraView dce.addListener(self) }
Add the setUpCamera
to the viewDidLoad
method and open the camera when the view appear.
- Objective-C
- Swift
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setUpCamera]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.dce open]; }
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. setUpCamera() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) dce.open() }
Step 1.3
Go to the file info.plist under your project folder. Under Information Property List, add Privacy - Camera Usage Description.
Build the app. Now, a simple camera app is created. After permitting the camera usage, you will see the camera view on the app.
Capture Frames From the Video Streaming
In this section, you will learn how to capture video frames with DynamsoftCameraEnhancer
.
Dynamsoft Camera Enhancer provides two solutions for fetching the video frames:
- Use the method
getFrameFromBuffer
to fetch a single frame from the video buffer. - Use callback method
FrameOutputCallback
to continuously fetching the video frames.
Note:
- All the following code will be added to the
ViewController
file in your project.
Step 2.1
Add DCEFrameListener
to your ViewController
so that you can use FrameOutputCallback
to get video frames.
- Objective-C
- Swift
@interface ViewController ()<DSVideoFrameListener>
class ViewController: UIViewController,VideoFrameListener{ //... }
Add FrameOutputCallback
to your project to get frames from camera output. DCEFrame is the class that stores frame data. You can use Image processing tools to parse the image information from a DCEFrame object or use DCEFrame.toUIImage
to convert it into a UIImage for other usages.
- Objective-C
- Swift
- (void)onFrameOutPut:(nonnull DSImageData *)frame { if (self.isClicked) { self.isClicked = false; dispatch_async(dispatch_get_main_queue(), ^{ self.button.enabled = false; self.imageView.image = [frame toUIImage:nil]; self.imageView.hidden = false; [self addBack]; }); } }
func onFrameOutPut(_ frame: ImageData) { if isClicked { isClicked = false DispatchQueue.main.async { [self] in button.isEnabled = false imageView.image = try? frame.toUIImage() imageView.isHidden = false addBack() } } }
Step 2.2
Add the trigger of the capture button.
- Objective-C
- Swift
@implementation ViewController{ // Add these varibles to capture and display images. UIButton *photoButton; UIImageView* imageView; bool isview; } // The UI for displaying the captured image. - (void)configurationUI{ CGFloat w = [[UIScreen mainScreen] bounds].size.width; CGFloat h = [[UIScreen mainScreen] bounds].size.height; CGFloat SafeAreaBottomHeight = [[UIApplication sharedApplication] statusBarFrame].size.height > 20 ? 34 : 0; photoButton = [[UIButton alloc] initWithFrame:CGRectMake(w / 2 - 60, h - 170 - SafeAreaBottomHeight, 120, 120)]; photoButton.adjustsImageWhenDisabled = NO; [photoButton setImage:[UIImage imageNamed:@"icon_capture"] forState:UIControlStateNormal]; self->imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, w, h)]; [photoButton addTarget:self action:@selector(takePictures) forControlEvents:UIControlEventTouchUpInside]; dispatch_async(dispatch_get_main_queue(), ^{ [self.view addSubview:self->photoButton]; }); } // Method for capturing image - (void)takePictures{ isview = true; } // The captured image will be displayed on another view. Add back button to get back to the camera. - (void)addBack{ self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(BackToHome)]; } - (void)BackToHome{ [imageView removeFromSuperview]; self.navigationItem.leftBarButtonItem = nil; [photoButton setEnabled:true]; }
// Add these varibles to capture and display images. var photoButton:UIButton! = UIButton() var imageView:UIImageView! var isview:Bool = false // The UI for displaying the captured image. func configurationUI() { let w = UIScreen.main.bounds.size.width let h = UIScreen.main.bounds.size.height let safeAreaBottomHeight:CGFloat = UIApplication.shared.statusBarFrame.size.height > 20 ? 34 : 0 photoButton = UIButton(frame: CGRect(x:w / 2 - 60, y: h - 170 - safeAreaBottomHeight, width: 120, height: 120)) photoButton.adjustsImageWhenDisabled = false photoButton.setImage(UIImage(named: "icon_capture"), for: .normal) self.imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: w, height: h)) photoButton.addTarget(self, action: #selector(takePictures), for: .touchUpInside) DispatchQueue.main.async { self.view.addSubview(self.photoButton) } } // Method for capturing image @objc func takePictures() { isview = true } // The captured image will be displayed on another view. Add back button to get back to the camera. func addBack(){ self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: .reply, target: self, action: #selector(backToHome)) } @objc func backToHome(){ self.imageView.removeFromSuperview() self.photoButton?.isEnabled = true self.navigationItem.leftBarButtonItem = nil }
Run the project. Now, you can try to capture video frames with Dynamsoft Camera Enhancer.
Note:
- For more samples on using Dynamsoft Camera Enhancer supporting Barcode Reader please click here.