Build the “Hello World” Document Capture View

If you haven't installed the framework in your Xcode project, please refer here. Once installed, you can add code to implement document capture function in your project.

1. Open ViewController.m file

You can see the default ViewController.m as the following:

default view controller

2. Import the framework's header

#import <DcsFramework/DcsView.h>
#import <DcsFramework/DcsUIImageGalleryView.h>
#import <DcsFramework/DcsUIVideoView.h>

3. Make changes to ViewController

@interface ViewController ()<DcsUIVideoViewDelegate, DcsUIImageGalleryViewDelegate>{
    DcsView *dcsView;
    UIButton *openVideoViewButton;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //Please replace **** with your license key copied from the portal. 
    //Check http://developer.dynamsoft.com/dws/ios/How-to-set-license for more details.
    [DcsView setLicense:@"****"]; 
    
    dcsView = [[DcsView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //Show VideoView when DcsView is loaded
    dcsView.currentView = DVE_VIDEOVIEW;
    
    ///////////////////video view settting//////////////////
    //Set the videoview capture mode to document mode
    dcsView.videoView.mode = DME_DOCUMENT;
    dcsView.videoView.delegate = self;
    //Set the next view after the cancel or capture button is clicked.
    dcsView.videoView.nextViewAfterCancel = DVE_IMAGEGALLERYVIEW;
    dcsView.videoView.nextViewAfterCapture= DVE_IMAGEGALLERYVIEW;
    
    [self.view addSubview:dcsView];
    
    CGRect btnFrame = CGRectMake(5,self.view.frame.size.height - 40, 150, 50);
    openVideoViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    openVideoViewButton.frame = btnFrame;
    [openVideoViewButton setTitle:@"Back to Camera" forState:normal];
    openVideoViewButton.backgroundColor = [UIColor clearColor];
    [openVideoViewButton addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:openVideoViewButton];
    openVideoViewButton.hidden = YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
///////////////////////////////////////////////////////
//UIVideoView delegate implement
- (BOOL) onPreCapture:(id)sender{
    //If NO is returned, the capture action will be canceled.
    NSLog(@"PreCapture invoked");
    return YES;
}
- (void) onPostCapture:(id)sender{
    NSLog(@"PostCapture invoked");
    openVideoViewButton.hidden = NO;
}
- (void) onCancelTapped:(id)sender{
    NSLog(@"video view cancel tap invoked");

}
- (void) onCaptureTapped:(id)sender{
    NSLog(@"Capture tap invoked");
    
}

- (void) onCaptureFailure:(id)sender exception:(DcsException *)exception{
    NSLog(@"CaptureFailure  invoked");
}
- (void) onDocumentDetected:(id)sender document:(DcsDocument *)document{
    
}
/////////////////////////////////////
//DcsUIImageGalleryViewDelegate imlement
- (void)onSingleTap:(id)sender index:(NSInteger)index{
    NSLog(@"SingleTap  invoked");
}

- (void)onSelectChanged:(NSArray *)selectedIndice{
    NSLog(@"SelectChanged  invoked");
}

- (void)onLongPress: (id)sender index: (NSInteger)index{
    NSLog(@"LongPress  invoked");
}

- (void) onClick{
    dcsView.currentView = DVE_VIDEOVIEW;
    openVideoViewButton.hidden = YES;
}


- (UIButton *) CreateOpenVideoButton:(CGRect)imageFrame{
    UIButton *imageButton = [[UIButton alloc]initWithFrame:imageFrame];
    [imageButton setTitle: @"CaptureImage" forState:normal];
    return imageButton;
}


@end

4. See it in action

If you launch the Hello World project, it should look like this:

detect doc

Once the capture button is clicked, the detected document will show up in the built-in image gallery view:

doc-gallery view

Note: For iOS 10 and later, please go to the Info tab of your project, under Custom iOS Target Properties section, click +, input "NSCameraUsageDescription" and press the "enter" button, and then set a value for Privacy - Camera Usage Description.

Addcamerasetting

Privancy - Camera Usage Description

As you can see, there are many other sample applications (source code provided) available for you to explore some of the many features of the Dynamsoft Camera SDK. Enjoy!

Step 1: Add a bridging header file to your application

1.1 Add a new header file to your application
step4newfile
step4newhfile
Please make sure to name it as "****-bridging-header.h" like the following
step4newhfilename
1.2 Copy the content from Headers/DCSFramework-Brdging-Header to the newly created header file
step4newhfilenamecontent
step4newhfilenamecopied
1.3 Change the build settings accordingly
Navigate to the "Build Settings" of the application, search for "swift" and copy the bridging header relavant file name to Objective-C Bridging Header in "Swift Compiler - General" like the following:
step4changesetting

Step 2: Add code to capture documents from the live stream

2.1 Open ViewController.swift file
2.2 Import the framework's header

import DcsFramework

2.3 Make changes to ViewController

class ViewController: UIViewController,DcsUIVideoViewDelegate,DcsUIImageGalleryViewDelegate{
    
    
    
    var openVideoViewButton:UIButton!
    var dcsView:DcsView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        dcsView = DcsView.self.init(frame:CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));
        //Show Video View when DcsView is loaded
        dcsView.currentView = DVE_VIDEOVIEW
        
        //////////////video view setting////////////////////////////
        //Set the videoview mode to document
        dcsView.videoView.mode = DME_DOCUMENT;
        dcsView.videoView.delegate = self;
        //set the imageGalleryView skip view after clicked cancel button
        dcsView.videoView.nextViewAfterCancel = DVE_IMAGEGALLERYVIEW;
        dcsView.videoView.nextViewAfterCapture = DVE_IMAGEGALLERYVIEW;
        self.view.addSubview(dcsView);
        
        
        
        openVideoViewButton = CreateOpenVideoButton(imageFrame:CGRect.init(x:(self.view.frame.size.width-84)/2, y:self.view.frame.size.height-111, width:84, height:84));
        openVideoViewButton.addTarget(self, action:#selector(onClick), for:UIControlEvents.touchUpInside);
        
        self.view.addSubview(openVideoViewButton);

        let btnFrame = UIButton(frame: CGRect(x:5,y:self.view.frame.size.height-40,width:150,height:50))
        btnFrame.setTitle("Back to Camera", for: .normal)
        btnFrame.addTarget(self, action:#selector(onClick), for: .touchUpInside)
        
        view.addSubview(btnFrame)
        openVideoViewButton.isHidden=true
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func CreateOpenVideoButton(imageFrame:CGRect)->UIButton{
        var button:UIButton! 
        button = UIButton.init(type:UIButtonType.custom);
        button.frame = imageFrame;
        button.setTitle("CaptureImage", for: .normal)
        return button;
    }
    
    func onClick(){
        dcsView.currentView = DVE_VIDEOVIEW;
        openVideoViewButton.isHidden=true;
        //self.checkCurrentView();
    }
   
    ///////////////////////////////////////////////////////
    //UIVideoView delegate implement
    func onCancelTapped(_ sender: Any!) {
        NSLog("%s", "video view cancel tap invoked");
        //self.checkCurrentView();
    }
    func onPostCapture(_ sender: Any!) {
        NSLog("%s", "PostCapture invoked");
    }
    func onCaptureTapped(_ sender: Any!) {
        NSLog("%s", "CaptureTapped invoked");
        //self.checkCurrentView();
    }
    func onPreCapture(_ sender: Any!) -> Bool {
        NSLog("%s", "PreCapture invoked");
        return true;
    }
    func onCaptureFailure(_ sender: Any!, exception: DcsException!) {
        NSLog("%s", "CaptureFailure invoked");
    }
  
    func onDocumentDetected(_ sender: Any!, document: DcsDocument!) {
        
    }
    
    //////////////////////////////////////////////////////
    //DcsUIImageGalleryViewDelegate implement
    func onSingleTap(_ sender: Any!, index: Int) {
        NSLog("%s", "SingleTap invoked");
    }

    func onLongPress(_ sender: Any!, index: Int) {
        NSLog("%s", "LongPress invoked");
    }
    
    func onSelectChanged(_ selectedIndice: [Any]!) {
        NSLog("%s", "SelectChanged invoked");
    }
    
    
    
}

2.4 See it in action

If you launch the Hello World project, it should look like this:

detect doc

Once the capture button is clicked, the captured image will show up in the built-in image gallery view:

doc-gallery view

Note: For iOS 10 and later, please go to the Info tab of your project, under Custom iOS Target Properties section, click +, input "NSCameraUsageDescription" and press the "enter" button, and then set a value for Privacy - Camera Usage Description.

Addcamerasetting

Privancy - Camera Usage Description

As you can see, there are many other sample applications (source code provided) available for you to explore some of the many features of the Dynamsoft Camera SDK. Enjoy!

Is this page helpful?

Leave a Reply

Your email address will not be published.