Barcode Detection and Generation

You can perceive and understand an image or extract information from images in your application.

The main barcode detection and generation features include the following:

Prerequisites

To enable your application to use the barcode detection and generation functionality, proceed as follows:

  1. Install the NuGet packages for media vision.

  2. To use the methods and properties of the barcode detection and generation classes and to handle camera preview, include the Tizen.Multimedia and Tizen.Multimedia.Vision namespaces in your application:

    C#
    Copy
    using Tizen.Multimedia; using Tizen.Multimedia.Vision;

Prepare the barcode engines

To initialize the barcode detection and generation engines for use, proceed as follows:

  1. For barcode detection:

    • Create an instance of the Tizen.Multimedia.Vision.BarcodeDetectionConfiguration class and set the Target property as a value of the Tizen.Multimedia.Vision.BarcodeDetectionTarget enumeration:

      C#
      Copy
      static BarcodeDetectionConfiguration configDetection = new BarcodeDetectionConfiguration(); /// To detect all barcode types configDetection.Target = BarcodeDetectionTarget.All; /// To detect only 1D barcodes /// configDetection.Target = BarcodeDetectionTarget.Barcode1D; /// To detect only 2D barcodes (QR codes) /// configDetection.Target = BarcodeDetectionTarget.Barcode2D;
    • Create an instance of the Tizen.Multimedia.Vision.MediaVisionSource class with raw image buffer data and its corresponding width, height, and color space:

      C#
      Copy
      /// Assume that there is a decoded raw data buffer of the byte[] type, and /// it has 640x480 resolution with an RGB888 color space MediaVisionSource source = new MediaVisionSource(bytes, width, height, ColorSpace.Rgb888);

      The source stores the barcode to be detected and all related data.

    • To provide camera preview images, define a camera preview event handler for the Preview event of the Tizen.Multimedia.Camera class and create an instance of that class:

      C#
      Copy
      /// Define a camera preview event handler static void PreviewCallback(object sender, PreviewEventArgs e) { PreviewData preview = e.Preview; SinglePlane singlePlane = (SinglePlane)preview.Plane; if (preview.Format == CameraPixelFormat.Rgb888) { MediaVisionSource source = new MediaVisionSource(singlePlane.Data, preview.width, preview.height, ColorSpace.Rgb888); } } /// Create the Tizen.Multimedia.Camera instance static Camera camera = null; try { camera = new Camera(CameraDevice.Rear); } catch (NotSupportedException) { Log.Info("Barcode Sample", "NotSupported"); }
    • Set the camera display, register the camera preview event handler, and start the camera preview with the StartPreview() method:

      C#
      Copy
      /// Set the camera display camera.Display = new Display(new Window("Preview")); /// Register the camera preview event handler camera.Preview += PreviewCallback; IList previewFormats = camera.Feature.SupportedPreviewPixelFormats.ToList(); foreach (CameraPixelFormat previewFormat in previewFormats) { camera.Setting.PreviewPixelFormat = previewFormat; break; } /// Start the camera preview camera.StartPreview();
  2. For barcode generation, create an instance of the Tizen.Multimedia.Vision.BarcodeGenerationConfiguration class and set its properties:

    C#
    Copy
    static BarcodeGenerationConfiguration configGeneration = new BarcodeGenerationConfiguration(); /// To show message on the generated barcode image configGeneration.TextVisibility = Visibility.Visible; /// To hide message on the generated barcode image /// configGeneration.TextVisibility = Visibility.Invisible; /// To change the foreground or background color /// For this example, the foreground and background are set as black and white, respectively configGeneration.ForegroundColor = Color.Black; configGeneration.BackgroundColor = Color.White;

Detect barcodes

To detect barcodes, proceed as follows:

  1. To access the camera preview data from which to detect barcodes, create a new instance of the Tizen.Multimedia.Vision.MediaVisionSource class in the camera preview event handler:

    C#
    Copy
    static void PreviewCallback(object sender, PreviewEventArgs e) { PreviewData preview = e.Preview; SinglePlane singlePlane = (SinglePlane)preview.Plane; if (preview.Format == CameraPixelFormat.Rgb888) { MediaVisionSource source = new MediaVisionSource(singlePlane.Data, preview.width, preview.height, ColorSpace.Rgb888); }
  2. Detect barcodes in the image using the DetectAsync() method of the Tizen.Multimedia.Vision.BarcodeDetector class:

    C#
    Copy
    Point point = new Point(0,0); Size size = new Size((int)source.Width, (int)source.Height); Rectangle roi = new Rectangle(point, size); var barcodeLists = await BarcodeDetector.DetectAsync(source, roi, configDetection); foreach (Barcode barcode in barcodeLists) { Log.Info("Barcode sample", $"Barcode type is {barcode.Type}"); Log.Info("Barcode sample", $"Barcode message is {barcode.Message}"); } }

    The ROI (region of interest) feature allows you to define a rectangular region of the image in which to detect barcodes. In the above example, the whole image is set as the ROI.

  3. When barcode detection is no longer needed, deregister the camera preview event handler, stop the camera preview, and destroy the camera instance:

    C#
    Copy
    camera.Preview -= PreviewCallback; camera.StopPreview(); camera.Dispose();

    For more information, see the Tizen.Multimedia.Camera class.

Generate barcodes

To generate a barcode, proceed as follows:

  1. To generate the barcode into memory:

    • To generate a 1D barcode, create a source instance using the GenerateSource() method of the Tizen.Multimedia.Vision.BarcodeGenerator class with a message and a barcode type:

      C#
      Copy
      string message = "0123455"; /// For a Code 128 type barcode var source = BarcodeGenerator.GenerateSource(message, BarcodeType.Code128); /// If you want to change the barcode color or change the visibility of the text, give an instance /// of the Tizen.Multimedia.Vision.BarcodeGenerationConfiguration class as an additional parameter: /// var source = BarcodeGenerator.GenerateSource(message, BarcodeType.code128, configGeneration);
    • To generate a QR code:

      • To create the QR code configuration, create an instance of the Tizen.Multimedia.Vision.QrConfiguration class with the QR code encoding mode as a value of the Tizen.Multimedia.Vision.QrMode enumeration, the QR code error correction level as a value of the Tizen.Multimedia.Vision.ErrorCorrectionLevel enumeration, and the QR code version:

        C#
        Copy
        string message = "Tizen QR"; /// For the UTF8 encoding type QrConfiguration qrConfig = new QrConfiguration(QrMode.Utf8, ErrorCorrectionLevel.Medium, 30);
      • Create a source instance using the GenerateSource() method of the Tizen.Multimedia.Vision.BarcodeGenerator class with a message and the QR code configuration:

        C#
        Copy
        var source = BarcodeGenerator.GenerateSource(message, qrConfig); /// If you want to change the QR code color, give an instance /// of the Tizen.Multimedia.Vision.BarcodeGenerationConfiguration class as an additional parameter: /// var source = BarcodeGenerator.GenerateSource(message, qrConfig, configGeneration);
  2. To generate the barcode into a file:

    • To generate a 1D barcode:

      • Create an instance of the Tizen.Multimedia.Vision.BarcodeImageConfiguration class with the file format as a value of the Tizen.Multimedia.Vision.BarcodeImageFormat enumeration, the image file resolution, and a path where the file is to be saved:

        C#
        Copy
        int width = 300; int height = 100; BarcodeImageFormat format = BarcodeImageFormat.Jpeg; string path = "/tmp/tizen_barcode.jpg"; BarcodeImageConfiguration imageConfig = new BarcodeImageConfiguration(width, height, path, format);
      • Generate the barcode using the GenerateImage() method of the Tizen.Multimedia.Vision.BarcodeGenerator class:

        C#
        Copy
        string message = "0123455"; BarcodeType type = BarcodeType.Code128; BarcodeGenerator.GenerateImage(message, type, imageConfig);
    • To generate a QR code, create instances of the Tizen.Multimedia.Vision.BarcodeImageConfiguration and Tizen.Multimedia.Vision.QrConfiguration classes as above, and generate the QR code using the GenerateImage() method of the Tizen.Multimedia.Vision.BarcodeGenerator class:

      C#
      Copy
      int width = 300; int height = 300; BarcodeImageFormat format = BarcodeImageFormat.Jpeg; string path = "/tmp/tizen_qr.jpg"; BarcodeImageConfiguration imageConfig = new BarcodeImageConfiguration(width, height, path, format); Qrconfiguration qrConfig = new QrConfiguration(QrMode.Utf8, ErrorCorrectionLevel.Medium, 30); string message = "Tizen QR" BarcodeGenerator.GenerateImage(message, qrConfig, imageConfig);

Barcode specifications

The following tables provide more information on the barcode generation specifications.

Table: Supported barcode types

You can scroll this table.
1D or 2D Type Description Example
1-D UPC-A Universal product code with numeric 12-digit UPC-A
1-D UPC-E Universal product code with numeric 6-digit UPC-E
1-D EAN-8 International article number with numeric 8-digit EAN-8
1-D EAN-13 International article number with numeric 13-digit EAN-13
1-D CODE-128 Code 128; supports alphanumeric or numeric-only CODE-128
1-D CODE-39 Code 39; supports 34 characters consisting of uppercase letters (A to Z), numeric digits (0 to 9), and special characters (-, ., $, /, %, space) CODE-39
1-D INTERLEAVED 2 of 5 Interleaved 2 of 5 with numeric digits UPC-A
2-D QR code Quick Response code UPC-A

Table: Supported QR code specifications

You can scroll this table.
Specification Support type Description
Error Correction Code (ECC) Level ECC Low Recovery up to 7% damage
Error Correction Code (ECC) Level ECC Medium Recovery up to 15% damage
Error Correction Code (ECC) Level ECC Quartile Recovery up 25% damage
Error Correction Code (ECC) Level ECC High Recovery up to 30% damage
Encoding mode Numeric Numeric digits (‘0’, ‘1’, …, ‘9’)
Encoding mode Alphanumeric Alphanumeric characters: numeric (0, 1, …, 9), characters (A, B, …, Z), and punctuation (’ ', $, %, *, +, -, ‘.’, /, ‘:’)
Encoding mode Byte 8-bit Raw 8-bit bytes
Encoding mode UTF-8 Universal character set and Transformation Format 8-bit, encoding characters

Design QR

Design QR sample Design QR sample2

Important

The design QR feature is supported since Tizen 8.0.

Design QR is an extension of the existing standards QR code, allowing users to create a more recognizable and beautiful QR code. Existing QR codes are black and white rectangles, people find it hard to know what kind of information the provider wants to convey until they actually scan the code. Therefore, design QR makes it possible to infer what kind of information the QR code is trying to convey even before scanning, and allows a familiar approach with excellent identification.

Note

In the case of design QR, it is outside the official QR code specification. Therefore, it may not be recognized depending on the device.

Design QR offers the following 4 options to decorate:

  1. Finder pattern shape
  2. Data pattern shape
  3. Foreground/background color
  4. Logo image

All of these features can be easily configured by engine_config.

Finder pattern shape

rect_finder round_rect_finder circle_finder

Standard QR codes only support square shapes, but in the case of design QR, two additional shapes (circle and rounded rectangle) are supported. To set the finder pattern spape, you can use FinderShape property of Tizen.Multimedia.Vision.QrConfiguration and the finder shape attribute could be one out of the three options QrShape.Rectangular(default), QrShape.RoundRectangular, QrShape.Circle.

C#
Copy
var qrConfig = new QrConfiguration(QrMode.Utf8, ErrorCorrectionLevel.Medium, 30); qrConfig.FinderSharp = QrShape.Circle;

Data pattern shape

circle_data

Data pattern shape is similar to finder pattern but offers only two shape attribute options and the data pattern is set to DataShape property. The data pattern shape could be one of QrShape.Rectangular(default), QrShape.Circle.

C#
Copy
var qrConfig = new QrConfiguration(QrMode.Utf8, ErrorCorrectionLevel.Medium, 30); qrConfig.DataShape = QrShape.Circle;

Color

circle_data

Users can select foreground and background colors using Tizen.Multimedia.Vision.BarcodeGenerationConfiguration class:

C#
Copy
var configGeneration = new BarcodeGenerationConfiguration(); /// To hide message on the generated barcode image configGeneration.TextVisibility = Visibility.Invisible; /// To change the foreground or background color /// For this example, the foreground and background are set as black and white, respectively configGeneration.ForegroundColor = Color.Black; configGeneration.BackgroundColor = Color.White;
Warning

Foreground/background must be distinguishable with clear color differences. Below image has minor color differences and will not be recognized.

circle_data

Logo image

logo_image

Users can select the image path using EmbedImagePath property:

Note

The shape of the logo automatically changes to match the shape of the finder.

C#
Copy
var qrConfig = new QrConfiguration(QrMode.Utf8, ErrorCorrectionLevel.Medium, 30); qrConfig.EmbedImagePath = "/tmp/embed_image.jpg";
  • Dependencies
    • Tizen 4.0 and Higher
Visual Detection and Recognition
Next Deep Learning Based Face Recognition
Submit your feedback to GitHub