Dev Center
Swift
Objective-C
Table of contents

Thanks for downloading Dynamsoft Barcode Reader Package!

Your download will start shortly. If your download does not begin, click here to retry.

ImageSource

Protocol for producers of images. It can be implemented by developers to support other image sources, such as external cameras or image filesets.

How to Use:

  1. Use BarcodeReader.setImageSource to set ImageSource as the source of image.
  2. Add code in method getImage. When DBR finishes the current process and trying to start decoding from another image, it will call the getImage.
  3. Trigger BarcodeReader.startScanning to start the barcode decoding thread when all above are configured.
  4. When barcode decoding thread is started, users can stop the thread by calling BarcodeReader.stopScanning
  • Objective-C
  • Swift
  1. @protocol ImageSource <NSObject>
    
  2. protocol ImageSource : NSObjectProtocol
    
Method Description
getImage The method for users for complete. Get image/video from external sources and out put the data as iImageData.

getImage

The method for users for complete which returns iImageData. When using external sources, users can generate the external image source into iImageData and output them in method getImage so that the image can be recognized by the Barcode Reader.

The barcode reader will continuously use getImage to acquire iImageData for barcode decoding when:

  • Objective-C
  • Swift
  1. - (iImageData *_Nullable)getImage;
    
  2. func getImage() -> iImageData?
    

Return Value

An object of iImageData.

Code Snippet

Here we use AVFoundation as the example of the image source. The following code displays how to use AVFoundation to capture video frames and tranfer the video frames into iImageData.

  • Objective-C
  • Swift
  1. // Add property imageData in your project to receive the image data.
    @property (nonatomic, strong) iImageData *imageData;
    //Start barcode decoding when the view appears.
    - (void)viewWillAppear:(BOOL)animated {
       [super viewWillAppear:animated];
       [self.barcodeReader startScanning];
    }
    //Stop barcode decoding when the view disappears.
    - (void)viewWillDisappear:(BOOL)animated {
       [super viewWillDisappear:animated];
       [self.barcodeReader stopScanning];
    }
    - (void)configurationDBR {
       self.barcodeReader = [[DynamsoftBarcodeReader alloc] init];
       // Set image source
       [self.barcodeReader setImageSource:self];
       [self.barcodeReader setDBRTextResultListener:self];
    }
    ...
    // Get the buffer image from AVCaptureOutput and generate the image into iImageData.
    - (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
       CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
       if (imageBuffer == nil) {
          return;
       }
       CVPixelBufferLockBaseAddress(imageBuffer, 0);
       void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
       size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);
       size_t width = CVPixelBufferGetWidth(imageBuffer);
       size_t height = CVPixelBufferGetHeight(imageBuffer);
       size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
       CVPixelBufferUnlockBaseAddress(imageBuffer,0);
       NSData *buffer = [NSData dataWithBytes:baseAddress length:bufferSize];
       // Initialize the image data and allocate the value.
       if (self.imageData == nil) {
          self.imageData = [[iImageData alloc] init];
       }
       self.imageData.bytes = buffer;
       self.imageData.width = width;
       self.imageData.height = height;
       self.imageData.stride = bytesPerRow;
       self.imageData.format = EnumImagePixelFormatARGB_8888;
    }
    // Configure the getImage method.
    - (iImageData *)getImage {
       return self.imageData;
    }
    - (void)textResultCallback:(NSInteger)frameId imageData:(iImageData *)imageData results:(NSArray<iTextResult *> *)results {
       // Add your code to execute when iTextResult is received.
    }
    
  2. class CamerViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, ImageSource, DBRTextResultListener{
       // Add property imageData in your project to receive the image data.
       var imageData:iImageData! = nil
       //Start barcode decoding when the view appears.
       override func viewWillAppear(_ animated: Bool) {
          barcodeReader.startScanning()
       }
       //Stop barcode decoding when the view disappears.
       override func viewWillDisappear(_ animated: Bool) {
          barcodeReader.stopScanning()
       }
       func setDBR() {
          barcodeReader = DynamsoftBarcodeReader.init()
          // Set image source
          barcodeReader.setImageSource(self)
          barcodeReader.setDBRTextResultListener(self)
       }
       // Get the buffer image from AVCaptureOutput and generate the image into iImageData.
       func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
       {
          let imageBuffer:CVImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
          CVPixelBufferLockBaseAddress(imageBuffer, .readOnly)
          let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)
          let bufferSize = CVPixelBufferGetDataSize(imageBuffer)
          let width = CVPixelBufferGetWidth(imageBuffer)
          let height = CVPixelBufferGetHeight(imageBuffer)
          let bpr = CVPixelBufferGetBytesPerRow(imageBuffer)
          CVPixelBufferUnlockBaseAddress(imageBuffer, .readOnly)
          let buffer = Data(bytes: baseAddress!, count: bufferSize)
          // Initialize the image data and allocate the value
          if (imageData == nil) {
             imageData = iImageData.init()
          }
          imageData.bytes = buffer
          imageData.width = width
          imageData.height = height
          imageData.stride = bpr
          imageData.format = .ARGB_8888
       }
       // Configure the getImage method.
       func getImage() -> iImageData? {
          return imageData
       }
       func textResultCallback(_ frameId: Int, imageData: iImageData, results: [iTextResult]?){
          // Add your code to execute when iTextResult is received.
       }
    }
    

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest version
  • Version 9.x
    • Version 9.6.20
    • Version 9.6.11
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.13
    • Version 9.2.11
    • Version 9.2.10
    • Version 9.0.2
    • Version 9.0.1
    • Version 9.0.0
  • Version 8.x
    • Version 8.9.3
    • Version 8.9.1
    • Version 8.9.0
    • Version 8.8.0
    • Version 8.6.0
    • Version 8.4.0
    • Version 8.2.1
    • Version 8.2.0
    • Version 8.1.2
    • Version 8.1.0
    • Version 8.0.0
  • Version 7.x
    • Version 7.6.0
    • Version 7.5.0
Change +