Dev Center
Swift
Objective-C
Table of contents

Decode Methods

Method Description
decodeBuffer Decode barcodes from raw buffer.
decodeFileWithName Decode barcodes from a specified image file.
decodeImage Decode barcodes from an image file in memory.
decodeBase64 Decode barcodes from a base64 encoded string.

decodeBuffer

Decode barcodes from the memory buffer containing image pixels in a defined format.

- (NSArray<iTextResult*>* _Nullable)decodeBuffer:(NSData* _Nonnull)buffer withWidth:(NSInteger)width height:(NSInteger)height stride:(NSInteger)stride format:(EnumImagePixelFormat)format templateName:(NSString* _Nonnull)templateName error:(NSError* _Nullable * _Nullable)error;

Parameters

[in] buffer The array of bytes which contain the image data.
[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] templateName For general usage, please set an empty string for the templateName. For further usage, please read the article of parameter configuration.
[in,out] error Input a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

// Template name example.
// The "IP1" is the template name of this template. 
{
   "Version": "3.0",
   "ImageParameter": {                   
      "Name": "IP1",
      "Description": "This is an imageParameter", 
      "BarcodeFormatIds": ["BF_ALL"]
   }
}

Return Value

All successfully decoded barcode text results.

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.

Code Snippet

  • Objective-C
  • Swift
  1. [_dce addListener:self];
    //Get frames in callback methods.
    - (void)frameOutPutCallback:(DCEFrame *)frame timeStamp:(NSTimeInterval)timeStamp{
       NSArray<iTextResult*>* results = [barcodeReader decodeBuffer:frame.imageData withWidth:frame.width height:frame.height stride:frame.stride format:frame.pixelFormat templateName:@"" error:nil];
    }
    
  2. func frameOutPutCallback(_ frame: DCEFrame, timeStamp: TimeInterval){
       let result = try? barcodeReader.decodeBuffer(frame.imageData, withWidth: frame.width, height: frame.height, stride: frame.stride, format: EnumImagePixelFormat(rawValue: frame.pixelFormat) ?? EnumImagePixelFormat.ARGB_8888, templateName: "")
    }
    

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.

Code Snippet

  • Objective-C
  • Swift
  1. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection;
    {
       // Extract image data from sampleBuffer.
       CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
       CVPixelBufferLockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly);
       int bufferSize = (int)CVPixelBufferGetDataSize(imageBuffer);
       int imgWidth = (int)CVPixelBufferGetWidth(imageBuffer);
       int imgHeight = (int)CVPixelBufferGetHeight(imageBuffer);
       size_t bpr = CVPixelBufferGetBytesPerRow(imageBuffer);
       void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
       CVPixelBufferUnlockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly);
       NSData * buffer = [NSData dataWithBytes:baseAddress length:bufferSize];
       startRecognitionDate = [NSDate date];
       NSArray* results = [m_barcodeReader decodeBuffer:buffer withWidth:imgWidth height:imgHeight stride:bpr format: EnumImagePixelFormatARGB_8888 templateName:@"" error:nil];
    }
    
  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)
       startRecognitionDate = NSDate()
       let buffer = Data(bytes: baseAddress!, count: bufferSize)
       guard let results = try? barcodeReader.decodeBuffer(buffer, withWidth: width, height: height, stride: bpr, format: .ARGB_8888, templateName: "")
    }
    

decodeFileWithName

Decode barcodes from a specified image file.

- (NSArray<iTextResult*>* _Nullable)decodeFileWithName:(NSString* _Nonnull)name templateName:(NSString* _Nonnull)templateName error:(NSError* _Nullable * _Nullable)error;

Parameters

[in] name The local path of the file. It supports BMP, TIFF, JPG, PNG and PDF files.
[in] templateName The template name.
[in,out] error Input a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

All successfully decoded barcode text results.

Code Snippet

  • Objective-C
  • Swift
  1. NSArray<iTextResult*>* result = [barcodeReader decodeFileWithName:@"your file path" templateName:@"" error:&error];
    
  2. let error: NSError? = NSError()
    let result = try? barcodeReader.decodeFile(withName: "your file path",templateName:"")
    

decodeImage

Decode barcodes from an image file in memory.

- (NSArray<iTextResult*>* _Nullable)decodeImage:(UIImage* _Nonnull)image withTemplate:(NSString* _Nonnull)templateName error:(NSError* _Nullable * _Nullable)error;

Parameters

[in] image The image file in memory.
[in] templateName The template name.
[in, out] error Input a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

All successfully decoded barcode text results.

Code Snippet

  • Objective-C
  • Swift
  1. UIImage *image = [[UIImage alloc] init];
    NSError __autoreleasing * _Nullable error;
    NSArray<iTextResult*>* result = [barcodeReader decodeImage:image withTemplate:@"" error:&error];
    
  2. let image: UIImage? = UIImage()
    let error: NSError? = NSError()
    let result = try? barcodeReader.decode(image, withTemplate:"")
    

decodeBase64

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

DBR_API int DBR_DecodeBase64String (void* barcodeReader, const char* pBase64String, const char* pTemplateName)

Parameters

[in] base64 A base64 encoded string that represents an image.
[in] templateName The template name.
[in,out] error Input a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

All successfully decoded barcode text results.

Code Snippet

  • Objective-C
  • Swift
  1. NSError __autoreleasing * _Nullable error;
    NSArray<iTextResult*>* result = [barcodeReader decodeBase64:@"file in base64 string" withTemplate:@"" error:&error];
    
  2. let error: NSError? = NSError() 
    let result = try? barcodeReader.decodeBase64("file in base64 string", withTemplate: "")
    

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

version 8.9.3

  • Latest version (10.2.10)
  • Version 10.x
    • Version 10.0.21
    • Version 10.0.20
  • 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 +