Dev Center
Swift
Objective-C
Table of contents

Decode Methods

Method Description
decodeBuffer(ImageData) Decode barcodes from a pixel buffer with width, height, stride and pixel format info.
decodeBuffer Decode barcodes from a pixel buffer with width, height, stride and pixel format info.
decodeFileWithName Decode barcodes from a specified image file.
decodeFileInMemory Decode barcodes from a file that is read in the memory.
decodeBase64 Decode barcodes from a base64 encoded string.
decodeImage Decode barcodes from a UIImage.

decodeBuffer(ImageData)

Decode barcodes from a pixel buffer with width, height, stride and pixel format info. Generally, this method is used to process the video streaming. You can get a coordinate transformation matrix if you include the orientation information of the image.

  • Objective-C
  • Swift
  1. - (NSArray<iTextResult*>* _Nullable)decodeBuffer:(iImageData* _Nonnull)imageData
                                           error:(NSError* _Nullable * _Nullable)error;
    
  2. func decodeBuffer(_ imageData: iImageData) throws -> [iTextResult]
    

Parameters

[in] imageData: An iImageData object that includes the pixel buffer, width, height, stride and pixel format of the image. You can get it from AVCaptureVideoDataOutput or DynamsoftCameraEnhancer.
[in,out] error: A pointer to an error object.

An error occurs when:

  • The library failed to read the image.
  • The image data type is not supported.

Return Value

The iTextResult of each successfully decoded barcode.

Code Snippet

Get ImageData from DCEFrame

If you have imported DynamsoftCameraEnhancer.framework, you can get video frames from the frameOutputCallback. DCEFrame object contains all required parameters of decodeBuffer method.

  • Objective-C
  • Swift
  1. [_dce addListener:self];
    //Get frames in callback methods.
    - (void)frameOutPutCallback:(DCEFrame *)frame timeStamp:(NSTimeInterval)timeStamp{
       NSArray<iTextResult*>* barcodeResults = [barcodeReader decodeBuffer:frame.imageData withWidth:frame.width height:frame.height stride:frame.stride format:frame.pixelFormat error:nil];
    }
    
  2. func frameOutPutCallback(_ frame: DCEFrame, timeStamp: TimeInterval){
       do{
          let barcodeResults = try barcodeReader.decodeBuffer(frame.imageData, withWidth: frame.width, height: frame.height, stride: frame.stride, format: EnumImagePixelFormat(rawValue: frame.pixelFormat) ?? EnumImagePixelFormat.ARGB_8888)
       }catch{
          // Add your code to deal with exceptions
       }
    }
    

Get ImageData from CaptureOutput

If you are acquiring video frames from captureOutput callback, you can use the following code to extract the required parameters from sampleBuffer.

  • Objective-C
  • Swift
  1. @property (nonatomic, strong) iImageData *imageData;
    ...
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection;
    {
       // Extract image data from sampleBuffer.
       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]; 
       if (self.imageData == nil) {
          self.imageData = [[iImageData alloc] init];
       }
       // Assign value for the imageData
       self.imageData.bytes = buffer;
       self.imageData.width = width;
       self.imageData.height = height;
       self.imageData.stride = bytesPerRow;
       self.imageData.format = EnumImagePixelFormatARGB_8888;
       // Orientation is an optional property.
       // The library will output a transformation matrix to help you transform the coordinate of the barcode result points when you include the image orientation information.
       self.imageData.orientation = 0;
    }
    
  2. 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)
       if (imageData == nil) {
          imageData = iImageData.init()
       }
       imageData.bytes = buffer
       imageData.width = width
       imageData.height = height
       imageData.stride = bpr
       imageData.format = .ARGB_8888
       // Orientation is an optional property.
       // The library will output a transformation matrix to help you transform the coordinate of the barcode result points when you include the image orientation information.
       imageData.orientation = 0
    }
    

View sample DecodeWithAVCaptureSession to see how to process the video streaming.

decodeBuffer

Decode barcodes from image data that including pixel buffer, width, height, stride and pixel format.

  • Objective-C
  • Swift
  1. - (NSArray<iTextResult*>* _Nullable)decodeBuffer:(NSData* _Nonnull)buffer
                                       withWidth:(NSInteger)width
                                          height:(NSInteger)height
                                          stride:(NSInteger)stride
                                          format:(EnumImagePixelFormat)format
                                           error:(NSError* _Nullable * _Nullable)error;
    
  2. func decodeBuffer(_ buffer: Data, width: Int, height: Int, stride: Int, format: EnumImagePixelFormat) throws -> [iTextResult]
    

Parameters

[in] buffer: The array of bytes that stores the pixel buffer of the image.
[in] width: The width of the image in pixels.
[in] height: The height of the image in pixels.
[in] stride: The stride is measured by the byte length of each line in the buffer.
[in] format: The image pixel format used in the image byte array.
[in,out] error: A pointer to an error object.

An error occurs when:

  • The library failed to read the image.
  • The image data type is not supported.

Return Value

The iTextResult of each successfully decoded barcode.

Code Snippet

  • Objective-C
  • Swift
  1. [_dce addListener:self];
    //Get frames in callback methods.
    - (void)frameOutPutCallback:(DCEFrame *)frame timeStamp:(NSTimeInterval)timeStamp{
       NSArray<iTextResult*>* barcodeResults = [barcodeReader decodeBuffer:frame.imageData withWidth:frame.width height:frame.height stride:frame.stride format:frame.pixelFormat error:nil];
    }
    
  2. func frameOutPutCallback(_ frame: DCEFrame, timeStamp: TimeInterval){
       do{
          let barcodeResults = try barcodeReader.decodeBuffer(frame.imageData, withWidth: frame.width, height: frame.height, stride: frame.stride, format: EnumImagePixelFormat(rawValue: frame.pixelFormat) ?? EnumImagePixelFormat.ARGB_8888)
       }catch{
          // Add your code to deal with exceptions
       }
    }
    

decodeFileWithName

Decode barcodes from a specified image file.

  • Objective-C
  • Swift
  1. - (NSArray<iTextResult*>* _Nullable)decodeFileWithName:(NSString* _Nonnull)path
                                                 error:(NSError* _Nullable * _Nullable)error;
    
  2. func decodeFileWithName(_ path: String) throws -> [iTextResult]
    

Parameters

[in] name: The local path of the file. It supports BMP, TIFF, JPG, PNG and PDF files.
[in,out] error: A pointer to an error object.

An error occurs when:

  • The file is not found.
  • The library failed to read the image.
  • The image data type is not supported.

Return Value

The iTextResult of each successfully decoded barcode.

Code Snippet

  • Objective-C
  • Swift
  1. NSArray<iTextResult*>* barcodeResults = [barcodeReader decodeFileWithName:@"your file path" error:&error];
    
  2. do{
       let barcodeResults = try barcodeReader.decodeFileWithName("your file path")
    }catch{
       // Add your code to deal with exceptions
    }
    

decodeFileInMemory

Decode barcodes from a file that is read in the memory.

  • Objective-C
  • Swift
  1. - (NSArray<iTextResult *> *_Nullable)decodeFileInMemory:(NSData *_Nonnull)buffer error:(NSError *_Nullable *_Nullable)error;
    
  2. func decodeFile(inMemory buffer: Data) throws -> [iTextResult]
    

Parameter

[in] buffer: The image file that is read in memory.
[in,out] error: A pointer to an error object.

An error occurs when:

  • The library failed to read the image.
  • The image data type is not supported.

Return Value

The iTextResult of each successfully decoded barcode.

Code Snippet

  • Objective-C
  • Swift
  1. NSError __autoreleasing * _Nullable error;
    NSData * imageBuffer = [[NSData alloc] initWithContentOfFile:@"The file path"];
    NSArray<iTextResult*>* barcodeResults = [barcodeReader decodeFileInMemory:imageBuffer error:&error];
    
  2. do{
       let imageBuffer = try NSData.init(contentsOfFile:"The file path")
       let barcodeResults = try barcodeReader.decodeFileInMemory(imageBuffer)
    }catch{
       // Add your code to deal with exceptions
    }
    

decodeBase64

Decode barcodes from an image file encoded as a base64 string.

  • Objective-C
  • Swift
  1. - (NSArray<iTextResult*>* _Nullable)decodeBase64:(NSString* _Nonnull)base64
                                           error:(NSError* _Nullable * _Nullable)error;
    
  2. func decodeBase64(_ base64: String) throws -> [iTextResult]
    

Parameters

[in] base64: A base64 encoded string that represents an image.
[in,out] error: A pointer to an error object.

An error occurs when:

  • The library failed to read the image.
  • The image data type is not supported.

Return Value

The iTextResult of each successfully decoded barcode.

Code Snippet

  • Objective-C
  • Swift
  1. NSError __autoreleasing * _Nullable error;
    NSArray<iTextResult*>* barcodeResults = [barcodeReader decodeBase64:@"file in base64 string" error:&error];
    
  2. do{
       let barcodeResults = try barcodeReader.decodeBase64("file in base64 string")
    }catch{
       // Add your code to deal with exceptions
    }
    

decodeImage

Decode barcodes from a UIImage.

  • Objective-C
  • Swift
  1. - (NSArray<iTextResult*>* _Nullable)decodeImage:(UIImage* _Nonnull)image
                                          error:(NSError* _Nullable * _Nullable)error
                                          NS_SWIFT_NAME(decodeImage(_:));
    
  2. func decodeImage(_ image: UIImage) throws -> [iTextResult]
    

Parameters

[in] image: A UIImage object.
[in,out] error: A pointer to an error object.

An error occurs when:

  • The library failed to read the image.
  • The image data type is not supported.

Return Value

The iTextResult of each successfully decoded barcode.

Code Snippet

  • Objective-C
  • Swift
  1. UIImage *image = [[UIImage alloc] init];
    NSError __autoreleasing * _Nullable error;
    NSArray<iTextResult*>* barcodeResults = [_barcodeReader decodeImage:image error:&error];
    
  2. let frameImage = dce.getFrameFromBuffer(true).toUIImage()
    do{
       let barcodeResults = try barcodeReader.decodeImage(frameImage)
    }catch{
       // Add your code to deal with exceptions
    }
    

This page is compatible for:

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest version
  • Version 10.x
    • Version 10.2.1100
    • Version 10.2.1101
    • Version 10.2.10
    • Version 10.0.21
    • Version 10.0.20
    • Version 10.2.10
    • Version 10.0.21
    • Version 10.0.20
  • Version 9.x
    • Version 9.6.40
    • Version 9.6.20
    • Version 9.6.11
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.11
    • Version 9.2.10
    • Version 9.0.2
    • Version 9.0.1
    • Version 9.0.0
    • Version 9.6.40
    • Version 9.6.20
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.13
    • 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 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
    • Version 7.6.0
    • Version 7.5.0
  • Documentation Homepage
Change +