Build the “Hello World” Image Capture View

If you haven't installed the Dynamsoft Camera SDK framework in your Xcode project, please refer here. Once installed, you can add code to implement image 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 <DynamsoftCameraSDK/DcsView.h>
#import <DynamsoftCameraSDK/DcsUIImageGalleryView.h>
#import <DynamsoftCameraSDK/DcsUIVideoView.h>

3. Make changes to ViewController

@interface ViewController () {
    DcsView *dcsView;
    UIButton *openVideoViewButton;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    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 image mode
    dcsView.videoView.mode = DME_IMAGE;
    
    //Set the next view after the cancel or capture button is clicked.
    dcsView.videoView.nextViewAfterCancel = DVE_IMAGEGALLERYVIEW;
    dcsView.videoView.nextViewAfterCapture= DVE_EDITORVIEW;
    
    [self.view addSubview:dcsView];
    
    //Add a capture button on video view
    openVideoViewButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.center.x-100, self.view.center.y-20, 200, 40)];
    [openVideoViewButton setTitle:@"Back to Camera" forState:normal];
    [openVideoViewButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [openVideoViewButton addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
    
    [dcsView.imageGalleryView addSubview:openVideoViewButton];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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



@end


4. See it in action

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

IMG_5992

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

image in editor

After editing, you can click "OK" to see the edited image in the gallery view:

hello world-image capture

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

addaheaderfile
choosehfile
Please make sure to name it as "****-bridging-header.h" like the following

Screen Shot 2017-11-21 at 2.20.53 PM
1.2 Add the following codes to the newly created header file

#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>
#import <DynamsoftCameraSDK/DcsIo.h>

fillinthecontent

1.3 Change the build settings accordingly
Navigate to the "Build Settings" of the application, search for "swift" and copy the bridging header relevant file path to Objective-C Bridging Header in "Swift Compiler - General" like the following:

addthepath

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

2.1 Open ViewController.swift file
2.2 Make changes to ViewController

class ViewController: UIViewController {
    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 VideoView when DcsView is loaded
        dcsView.currentView=DVE_VIDEOVIEW
        
        ///////////////////video view settting//////////////////
        //Set the videoview capture mode to image mode
        dcsView.videoView.mode = DME_IMAGE
        
        //Set the next view after the cancel or capture button is clicked.
        dcsView.videoView.nextViewAfterCancel = DVE_IMAGEGALLERYVIEW
        dcsView.videoView.nextViewAfterCapture = DVE_EDITORVIEW
        
        view.addSubview(dcsView)
        
        openVideoViewButton = UIButton.init(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.addTarget(self, action:#selector(onClick), for:UIControlEvents.touchUpInside);
        self.view.addSubview(openVideoViewButton);
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @objc func onClick(){
        dcsView.currentView=DVE_VIDEOVIEW
    }
}

2.4 See it in action

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

IMG_5992

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

image in editor

After editing, you can click "OK" to see the edited image in the gallery view:

IMG_5993

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.