Barcode Detection and Generation
PUBLISHED
You can perceive and understand an image or extract information from images in your application.
The main barcode detection and generation features include:
- Handling images
You can handle images with the Tizen.Multimedia.Vision.MediaVisionSource [1] class. You can create the source instance using raw buffer data or an instance of the Tizen.Multimedia.MediaPacket [2] class.
- Detecting barcodes
You can detect barcodes in an image or from camera preview streams, and then decrypt them to display messages to the user.
Before detecting a barcode, you must define the barcode detection target as a value of the Tizen.Multimedia.Vision.BarcodeDetectionTarget [3] enumeration:
- Detect both 1D and 2D barcodes
- Detect 1D barcodes only
- Detect 2D barcodes only
- Generating barcodes
You can encrypt a given message, generate a barcode from it, and save it in a memory or as an image file.
Before generating a barcode, you must define the text visibility as a value of the Tizen.Multimedia.Visibility [4] enumeration:
- Generate barcode without an input message
- Generate barcode with an input message (supports only 1D barcodes)
You must also define the following barcode specifications:
- Barcode type
- QR code specification (if the QR code barcode type is used)
- Image format (if the barcode is saved as a file)
- JPEG
- BMP
- PNG
Optionally, you can change foreground or background color for the barcode by setting the
ForegroundColor
orBackgroundColor
properties of the Tizen.Multimedia.Vision.BarcodeGenerationConfiguration [5] class. Their default values are black and white, respectively.
Prerequisites
To enable your application to use the barcode detection and generation functionality:
- Install the NuGet packages for Media Vision and Camera.
- To use the methods and properties of the barcode detection and generation classes and to handle camera preview, include the Tizen.Multimedia [6] and Tizen.Multimedia.Vision [7] namespaces in your application:
using Tizen.Multimedia; using Tizen.Multimedia.Vision;
Preparing the Barcode Engines
To initialize the barcode detection and generation engines for use:
- For barcode detection:
- Create an instance of the Tizen.Multimedia.Vision.BarcodeDetectionConfiguration [8] class and set the
Target
property as a value of the Tizen.Multimedia.Vision.BarcodeDetectionTarget [3] enumeration: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 [1] class with raw image buffer data and its corresponding width, height, and color space:
/// 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 [9] class and create an instance of that class:
/// 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:
/// 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();
- Create an instance of the Tizen.Multimedia.Vision.BarcodeDetectionConfiguration [8] class and set the
- For barcode generation, create an instance of the Tizen.Multimedia.Vision.BarcodeGenerationConfiguration [5] class and set its properties:
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;
Detecting Barcodes
To detect barcodes:
- To access the camera preview data from which to detect barcodes, create a new instance of the Tizen.Multimedia.Vision.MediaVisionSource [1] class in the 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); }
- Detect barcodes in the image using the
DetectAsync()
method of the Tizen.Multimedia.Vision.BarcodeDetector [10] class:
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.
- When barcode detection is no longer needed, deregister the camera preview event handler, stop the camera preview, and destroy the camera instance:
camera.Preview -= PreviewCallback; camera.StopPreview(); camera.Dispose();
For more information, see the Tizen.Multimedia.Camera [9] class.
Generating Barcodes
To generate a barcode:
- 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 [11] class with a message and a barcode type: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 [5] 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 [12] class with the QR code encoding mode as a value of the Tizen.Multimedia.Vision.QrMode [13] enumeration, the QR code error correction level as a value of the Tizen.Multimedia.Vision.ErrorCorrectionLevel [14] enumeration, and the QR code version:
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 theTizen.Multimedia.Vision.BarcodeGenerator
class with a message and the QR code configuration:
var source = BarcodeGenerator.GenerateSource(message, qrConfig); /// If you want to change the QR code color, give an instance /// of the Tizen.Multimedia.Vision.BarcodeGenerationConfiguration [5] class as an additional parameter: /// var source = BarcodeGenerator.GenerateSource(message, qrConfig, configGeneration);
- To create the QR code configuration, create an instance of the Tizen.Multimedia.Vision.QrConfiguration [12] class with the QR code encoding mode as a value of the Tizen.Multimedia.Vision.QrMode [13] enumeration, the QR code error correction level as a value of the Tizen.Multimedia.Vision.ErrorCorrectionLevel [14] enumeration, and the QR code version:
- To generate a 1D barcode, create a source instance using the
- To generate the barcode into a file:
- To generate a 1D barcode:
- Create an instance of the Tizen.Multimedia.Vision.BarcodeImageConfiguration [15] class with the file format as a value of the Tizen.Multimedia.Vision.BarcodeImageFormat [16] enumeration, the image file resolution, and a path where the file is to be saved:
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 theTizen.Multimedia.Vision.BarcodeGenerator
class:
string message = "0123455"; BarcodeType type = BarcodeType.Code128; BarcodeGenerator.GenerateImage(message, type, imageConfig);
- Create an instance of the Tizen.Multimedia.Vision.BarcodeImageConfiguration [15] class with the file format as a value of the Tizen.Multimedia.Vision.BarcodeImageFormat [16] enumeration, the image file resolution, and a path where the file is to be saved:
- To generate a QR code, create instances of the
Tizen.Multimedia.Vision.BarcodeImageConfiguration
andTizen.Multimedia.Vision.QrConfiguration
classes as above, and generate the QR code using theGenerateImage()
method of theTizen.Multimedia.Vision.BarcodeGenerator
class:
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);
- To generate a 1D barcode:
Barcode Specifications
The following tables provide more information on the barcode generation specifications.
Table: Supported barcode types
1D or 2D | Type | Description | Example |
---|---|---|---|
1-D | UPC-A | Universal product code with numeric 12-digit | |
UPC-E | Universal product code with numeric 6-digit | ||
EAN-8 | International article number with numeric 8-digit | ||
EAN-13 | International article number with numeric 13-digit | ||
CODE-128 | Code 128; supports alphanumeric or numeric-only | ||
CODE-39 | Code 39; supports 34 characters consisting of uppercase letters (A to Z), numeric digits (0 to 9), and special characters (-, ., $, /, %, space) | ||
INTERLEAVED 2 of 5 | Interleaved 2 of 5 with numeric digits | ||
2-D | QR code | Quick Response code |
Table: Supported QR code specifications
Specification | Support type | Description |
---|---|---|
Error Correction Code (ECC) Level | ECC Low | Recovery up to 7% damage |
ECC Medium | Recovery up to 15% damage | |
ECC Quartile | Recovery up 25% damage | |
ECC High | Recovery up to 30% damage | |
Encoding mode | Numeric | Numeric digits ('0', '1', ..., '9') |
Alphanumeric | Alphanumeric characters: numeric (0, 1, ..., 9), characters (A, B, ..., Z), and punctuation (' ', $, %, *, +, -, '.', /, ':') | |
Byte 8-bit | Raw 8-bit bytes | |
UTF-8 | Universal character set and Transformation Format 8-bit, encoding characters |