How to Use Dynamsoft iOS Camera SDK

A few weeks ago, Dynamsoft released an iOS camera SDK that aims to help developers quickly build a document scanning app for iOS platform. In this post, I will share how to configure the SDK in Xcode, as well as how to create a simple document scanner app from scratch.

Prerequisites

Structure of Dynamsoft iOS Camera SDK

Before using the SDK, you’d better understand the relationship between different classes.

structure of iOS camera SDK

structure of iOS camera SDK

Building an iOS Document Scanner

Open Xcode, and press Shift+Command+N to create a Single View App.

ios single view app

Task failed with exit 1 signal

When building the project in Xcode, I got the following error:

task failed with exit

Here is the solution from StackOverflow:

  1. Close Xcode.
  2. Open Key Chain.
  3. Find the iOS Developer cert in Local.
  4. Drag and drop the cert from Local to the System tab.
  5. Enter admin password when prompted.
  6. Start Xcode and build project for your device.

DynamsoftCameraSDK.framework

Drag DynamsoftCameraSDK.framework to your project. Make sure ‘Copy Item if needed’ is checked.

The SDK depends on sqlite3 and stdc++. Click project settings > General to add libsqlite3.tbd and libstdc++.tbd.

ios camera dependency

If you want to use a video view to scan documents in real-time, click project settings > Info to add ‘Privacy – Camera Usage Description’.

ios camera permission

If you don’t have this property, you will get the error message:

2018-02-02 15:55:08.511904+0800 docscanner[4310:948418] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles

2018-02-02 15:55:08.512628+0800 docscanner[4310:948418] [MC] Reading from public effective user settings.

2018-02-02 15:55:08.665667+0800 docscanner[4310:948447] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.

(lldb)

There is one more step – click project settings > Build Settings > Other Linker Flags to add ‘-ObjC‘.

ios linker flag

Without this flag, you will get errors as follows:

2018-02-02 16:11:59.018002+0800 docscanner[4416:964122] +[UIImage getImageFromeDcsResourceBundleWithName:]: unrecognized selector sent to class 0x1b676db58

2018-02-02 16:11:59.023812+0800 docscanner[4416:964122] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIImage getImageFromeDcsResourceBundleWithName:]: unrecognized selector sent to class 0x1b676db58'

Create a bridging header file docscanner-Bridging-Header.h.:

#ifndef Bridging_Header_h

#define Bridging_Header_h

#import <DynamsoftCameraSDK/DcsView.h>

#import <DynamsoftCameraSDK/DcsDocument.h>

#import <DynamsoftCameraSDK/DcsImage.h>

#import <DynamsoftCameraSDK/DcsUIImageEditorView.h>

#import <DynamsoftCameraSDK/DcsException.h>

#import <DynamsoftCameraSDK/DcsBuffer.h>

#import <DynamsoftCameraSDK/DcsUIDocumentEditorView.h>

#import <DynamsoftCameraSDK/DcsUIImageGalleryView.h>

#import <DynamsoftCameraSDK/DcsUIVideoView.h>

#endif /* Bridging_Header_h */

Select ViewController.swift. In viewDidLoad() function, create a DcsView as the root view:

dcsView = DcsView.self.init(frame:CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
view.addSubview(dcsView)

So far, a simple document scanner app is done. You don’t need to create any UI element on the storyboard. By default, the DcsView will show you an image gallery. If you want to open camera for scanning documents, change the current view to video view:

dcsView.currentView = DVE_VIDEOVIEW
dcsView.videoView.mode = DME_DOCUMENT

There are a cancel button and a capture button on video view. You can navigate to other views by touching them:

dcsView.videoView.nextViewAfterCancel = DVE_IMAGEGALLERYVIEW
dcsView.videoView.nextViewAfterCapture = DVE_EDITORVIEW

Also, you can create a button to trigger the view change:

openVideoViewButton = UIButton(frame:CGRect(x:self.view.center.x-100, y:self.view.center.y-20, width: 200, height: 40))
openVideoViewButton.setTitle("Back to Camera", for: .normal)
openVideoViewButton.setTitleColor(UIColor.blue, for: .normal)
openVideoViewButton.addTarget(self, action:#selector(onClick), for:UIControlEvents.touchUpInside);
dcsView.imageGalleryView.addSubview(openVideoViewButton);

@objc func onClick(){
        dcsView.currentView = DVE_VIDEOVIEW
}

Build and run the iOS document scanner.

ios camera without ui

DynamsoftCameraSDKResource.bundle

Dynamsoft iOS Camera SDK also provides UI resources.

Drag DynamsoftCameraSDKResource.bundle to your project. Make sure ‘Copy Item if needed’ is checked.

Build and re-run the project. Now you can see the icons of the touch button and camera.

ios camera ui

Documentation

https://developer.dynamsoft.com/dws/ios-edition

Source Code

https://github.com/yushulx/ios-document-scanner