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.

Decode Methods

Method Description
decodeBuffer Decode barcodes from raw buffer.
decodeFile Decode barcodes from a specified image file.
decodeFileInMemory Decode barcodes from an image file in memory.
decodeBase64String Decode barcodes from a base64 encoded string.
decodeBufferedImage Decodes barcode from a buffered imag (bitmap).

decodeBuffer

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

TextResult[] decodeBuffer(byte[] buffer, int width, int height, int stride, int enumImagePixelFormat, String templateName) throws BarcodeReaderException

Parameters

buffer: The array of bytes which contain the image data.
Width: The width of the image in pixels.
Height: The height of the image in pixels.
Stride: The stride is measured by the byte length of each line in the buffer.
format: The image pixel format used in the image byte array.
templateName: For general usage, please set an empty string for the templateName. For further usage, please read the article of parameter configuration.

// 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 results.

Exceptions

BarcodeReaderException

There are several approaches for you to get a buffered image.

Get ImageData from DCEFrame

You can import CameraEnhancer to acquire buffered video frames from frameOutputCallback or videoBuffer of DCE.

Code Snippet

/*You can get frames from frame output call back if you import dynamsoft camera enhancer package.*/
/*You can get all the required parameters of decodeBuffer from DCEFrame.*/
import com.dynamsoft.dce.CameraEnhancer;

BarcodeReader reader = new BarcodeReader();
mCameraEnhancer.addListener(new DCEFrameListener() {
  @Override
  public void frameOutputCallback(DCEFrame dceFrame, long l) {
    try {
      TextResult[] results = reader.decodeBuffer(dceFrame.getImageData(),dceFrame.getWidth(),dceFrame.getHeight(),dceFrame.getStrides()[0],dceFrame.getPixelFormat(),"");
    } catch (BarcodeReaderException e) {
      e.printStackTrace();
    }
  }
});

Get ImageData from Android Camera2

When you are using Android Camera2, you can get video frames from ImageReader.

Code Snippet

previewReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
  @Override
  public void onImageAvailable(ImageReader reader) {
    Image mImage = reader.acquireLatestImage();
    ByteBuffer bufferY = mImage.getPlanes()[0].getBuffer();
    int strideY = mImage.getPlanes()[0].getRowStride() / mImage.getPlanes()[0].getPixelStride();
    ByteBuffer bufferU = mImage.getPlanes()[1].getBuffer();
    int strideU = mImage.getPlanes()[1].getRowStride() / mImage.getPlanes()[1].getPixelStride();
    ByteBuffer bufferV = mImage.getPlanes()[2].getBuffer();
    int strideV = mImage.getPlanes()[2].getRowStride() / mImage.getPlanes()[2].getPixelStride();
    int padingY = mImage.getPlanes()[0].getRowStride() - mImage.getWidth();
    int padingU = mImage.getPlanes()[1].getRowStride() - mImage.getWidth();
    byte[] newData = new byte[bufferY.limit()];
    newData = new byte[bufferY.limit() + bufferU.limit() + 1 + padingY + padingU];
    bufferV.get(newData, bufferY.limit() + padingY, 1);
    bufferU.get(newData, bufferY.limit() + padingY + 1, bufferU.limit());
    bufferY.get(newData, 0, bufferY.limit());
    int[] strides = new int[]{strideY, strideU, strideV};
    try {
      TextResult[] results = reader.decodeBuffer(newData, strideY, mImage.getHeight(), strideY, 3, "");
    } catch (BarcodeReaderException e) {
      e.printStackTrace();
    }
  }
},handler);

decodeFile

Decode barcodes from a specified image file.

TextResult[] decodeFile(String fileFullPath, String templateName) throws BarcodeReaderException

Parameters

fileFullPath: A string defining the file path. It supports BMP, TIFF, JPG, PNG and PDF files.
templateName: The template name. When you upload settings from JSON String or file, you can add a template name for each group of settings. The template settings will be recorded even if they are overwritten. When using Dynamsoft decode methods, you can specify a template name to apply a previously set template. Otherwise, the currently activated template will take over the barcode decoding.

Return Value

All successfully decoded barcode results.

Exceptions

BarcodeReaderException

Code Snippet

BarcodeReader reader = new BarcodeReader();
/*Init DBR license before decoding*/
/*Read external storage permission is required when decoding from a file*/
TextResult[] result = reader.decodeFile(Environment.getExternalStorageDirectory().toString()+"your file path", "IP1");

decodeFileInMemory

Decode barcodes from an image file in memory.

fileBytes

TextResult[] decodeFileInMemory(byte[] fileBytes, String templateName) throws BarcodeReaderException

Parameters

fileBytes: The image file bytes in memory.
templateName: The template name.

Return Value

All successfully decoded barcode results.

Exceptions

BarcodeReaderException

Code Snippet

BarcodeReader reader = new BarcodeReader();
/*Init DBR license before decoding
get bufferBytes from other component*/
TextResult[] result = reader.decodeFileInMemory(bufferBytes, "");

fileStream

TextResult [] decodeFileInMemory(InputStream fileStream, String templateName) throws BarcodeReaderException, IOException

Parameters

fileStream: The image file bytes in memory.
templateName: The template name.

Return Value

All successfully decoded barcode results.

Exceptions

BarcodeReaderException, IOException

Code Snippet

BarcodeReader reader = new BarcodeReader();
/*Init DBR license before decoding
get bufferBytes from other component*/
TextResult[] result = reader.decodeFileInMemory(fis, "");

decodeBase64String

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

TextResult[] decodeBase64String(String base64, String templateName) throws BarcodeReaderException

Parameters

base64: A base64 encoded string that represents an image.
templateName: The template name.

Return Value

All successfully decoded barcode results.

Exceptions

BarcodeReaderException

Code Snippet

BarcodeReader reader = new BarcodeReader();
/*Init DBR license before decoding*/
TextResult[] result = reader.decodeBase64String("file in base64 string", "");

decodeBufferedImage

Decodes barcode from a buffered image (bitmap).

TextResult[] decodeBufferedImage(Bitmap image, String templateName) throws BarcodeReaderException

Parameters

image: The image to be decoded.
templateName: The template name.

Return Value

All successfully decoded barcode results.

Exceptions

BarcodeReaderException, IOException

Code Snippet

BarcodeReader reader = new BarcodeReader();
/*Init DBR license before decoding*/
/*get BufferedImage input from other component*/
TextResult[] result = reader.decodeBufferedImage(input, "");

initIntermediateResult

Inits an intermediateResult struct with default values.

IntermediateResult initIntermediateResults(int resultType) throws BarcodeReaderException

Parameters

resultType: The type of the intermediate result to init.

Return Value

An intermediateResult struct with default values.

Exceptions

BarcodeReaderException

Code Snippet

BarcodeReader reader = new BarcodeReader();
/*Init DBR license before decoding*/
IntermediateResult imResult = reader.initIntermediateResult(EnumIntermediateResultType.IRT_ORIGINAL_IMAGE);

decodeIntermediateResults

Decodes barcode from intermediate results.

TextResult[] decodeIntermediateResults(IntermediateResult[] results, String templateName) throws BarcodeReaderException

Parameters

results: An array of intermediate result.
templateName: The template name.

Return Value

All barcode text results decoded successfully.

Exceptions

BarcodeReaderException

Code Snippet

BarcodeReader reader = new BarcodeReader();
/*Init DBR license before decoding*/
PublicRuntimeSettings settings = reader.getRuntimeSettings();
settings.intermediateResultTypes = EnumIntermediateResultType.IRT_ORIGINAL_IMAGE;
reader.updateRuntimeSettings(settings);
reader.decodeFile("your file path", "");
IntermediateResult[] irtResult = reader.getIntermediateResults();
TextResult[] result = reader.decodeIntermediateResults(irtResult, "");

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

version 8.9.3

  • Latest version
  • 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.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 +