如何从多页/帧 GIF 和 TIFF 中读取条形码

Hairil related to 如何从多页/帧 GIF 和 TIFF 中读取条形码
海瑞尔 哈西米 本 奥马尔
2023年十月31日
更新 2024年十二月10日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode 支持多种图像格式进行读取,包括多页和多帧 GIF 和 TIFF图像格式。 这为用户提供了便利,用户可以直接使用图像,而无需手动分离TIFF或GIF文件的帧或页面。让我们探索如何使用IronBarcode来读取这些文件格式。

开始使用 IronBarcode

立即在您的项目中开始使用IronBarcode,并享受免费试用。

第一步:
green arrow pointer


读取多帧 GIF 和 TIFF 图像

使用 IronBarcode 读取多帧 GIF 和 TIFF 图像与读取单个图像一样简单,因为 IronBarcode 可以直接在 BarcodeReader.Read 方法中接受多页图像文件。用户无需进行图像准备,因为所有操作都已在库中内部化。

下面的代码示例演示了如何读取多页GIF和TIFF文件:

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-multi-page-frame-tiff-gif-read-tif.cs
using IronBarCode;
using System;

// Read barcode from TIF image
BarcodeResults results = BarcodeReader.Read("sample.tif");

// Output the barcodes value to console
foreach (var result in results)
{
    Console.WriteLine(result.Value);
}
Imports IronBarCode
Imports System

' Read barcode from TIF image
Private results As BarcodeResults = BarcodeReader.Read("sample.tif")

' Output the barcodes value to console
For Each result In results
	Console.WriteLine(result.Value)
Next result
$vbLabelText   $csharpLabel

将图像转换为GIF和TIFF

了解如何通过使用我们的开源库IronDrawing将图像转换为多页 TIFF 和 GIF。 现在,让我们看一下下面的代码示例,了解如何生成多页的GIF或TIFF图像。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-multi-page-frame-tiff-gif-create-tiff-gif.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Collections.Generic;

// Import images
List<AnyBitmap> images = new List<AnyBitmap>()
{
    AnyBitmap.FromFile("image1.png"),
    AnyBitmap.FromFile("image2.png"),
    AnyBitmap.FromFile("image3.png"),
    AnyBitmap.FromFile("image4.jpg"),
    AnyBitmap.FromFile("image5.jpg")
};

// Convert TIFF from images
AnyBitmap tiffImage = AnyBitmap.CreateMultiFrameTiff(images);

// Export TIFF
tiffImage.SaveAs("multiframetiff.tiff");

// Convert GIF from images
AnyBitmap gifImage = AnyBitmap.CreateMultiFrameGif(images);

// Export GIF
gifImage.SaveAs("multiframegif1.gif");
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Collections.Generic

' Import images
Private images As New List(Of AnyBitmap)() From {AnyBitmap.FromFile("image1.png"), AnyBitmap.FromFile("image2.png"), AnyBitmap.FromFile("image3.png"), AnyBitmap.FromFile("image4.jpg"), AnyBitmap.FromFile("image5.jpg")}

' Convert TIFF from images
Private tiffImage As AnyBitmap = AnyBitmap.CreateMultiFrameTiff(images)

' Export TIFF
tiffImage.SaveAs("multiframetiff.tiff")

' Convert GIF from images
Dim gifImage As AnyBitmap = AnyBitmap.CreateMultiFrameGif(images)

' Export GIF
gifImage.SaveAs("multiframegif1.gif")
$vbLabelText   $csharpLabel

从上面的代码片段中,我们可以首先通过将多个图像文件导入到AnyBitmap对象的列表中来对它们进行分组。 然后可以在调用AnyBitmap.CreateMultiFrameTiffAnyBitmap.CreateMultiFrameGif方法时将此列表用作参数,以分别获取多页TIFF和多页GIF对象。

虽然多页GIF和TIFF都提供了一种将多个图像合并到一个文件中的方式,但这两种格式之间存在几个差异,如下所述:

AspectMultipage GIFMultipage TIFF
CompressionGIF images use lossless compression, meaning that no image data is lost during compression. This results in relatively larger file sizes compared to formats with lossy compression.TIFF files can use various compression methods, including lossless compression (such as LZW) and lossy compression (such as JPEG). This flexibility allows TIFF files to balance between file size and image quality.
Color DepthGIF supports up to 256 colors (8-bit color depth), which is limited compared to other formats. This limited color palette can result in a loss of detail and color accuracy, especially for photographs and images with gradientsTIFF supports various color depths, including 1-bit (binary), 8-bit (256 colors), 24-bit (true color), and more. This flexibility allows TIFF to store images with different levels of color detail.
TransparencyGIF supports binary transparency, which means that a single color can be fully transparent, and the rest of the colors are fully opaque. This lack of partial transparency can sometimes lead to jagged edges in images with smooth transitions.TIFF supports multiple forms of transparency, including binary transparency (similar to GIF) and alpha channel transparency. Alpha channel transparency allows for smooth transitions and semi-transparent pixels, providing high-quality transparency effects.
AnimationGIF supports simple animations by combining multiple frames into a single file. Each frame can have its own time delay, creating a basic form of animation. GIF animations are widely supported on the web.TIFF is not primarily designed for animations. While it can store multiple images, it lacks built-in animation support like GIF. Each page in a multipage TIFF file is typically a separate image rather than a frame in an animation sequence.

高级条码阅读

虽然IronBarcode可以立即使用,但某些图像可能需要配置BarcodeReaderOptions类以实现准确快速的条形码读取。 您可以在《如何从图像文件(jpg、png、gif、tiff、svg、bmp)读取条形码》文章中找到有关此类的更多信息。

下面的代码片段提供了可以在BarcodeReaderOptions类中配置的必要属性示例。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-multi-page-frame-tiff-gif-advance.cs
using IronBarCode;
using System;

// Configure filters
ImageFilterCollection filters = new ImageFilterCollection()
{
    new SharpenFilter(3.5f),
    new ContrastFilter(2)
};

// Configure options
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.QRCode,
    ImageFilters = filters,
    ExpectMultipleBarcodes = true,
    Speed = ReadingSpeed.Balanced
};

// Read barcode from TIF image
BarcodeResults results = BarcodeReader.Read("sample.tif", options);

// Output the barcodes value to console
foreach (var result in results)
{
    Console.WriteLine(result.Value);
}
Imports IronBarCode
Imports System

' Configure filters
Private filters As New ImageFilterCollection() From {
	New SharpenFilter(3.5F),
	New ContrastFilter(2)
}

' Configure options
Private options As New BarcodeReaderOptions() With {
	.ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.QRCode,
	.ImageFilters = filters,
	.ExpectMultipleBarcodes = True,
	.Speed = ReadingSpeed.Balanced
}

' Read barcode from TIF image
Private results As BarcodeResults = BarcodeReader.Read("sample.tif", options)

' Output the barcodes value to console
For Each result In results
	Console.WriteLine(result.Value)
Next result
$vbLabelText   $csharpLabel

在代码片段中,我们不仅设置了BarcodeReaderOptions属性,还应用了一些滤镜,特别是SharpenFilterContrastFilter。 这些过滤器主要用于提高模糊图像中条形码检测和读取的清晰度。 您可以在“如何使用图像校正滤镜”文章中找到有关图像校正滤镜的更多信息。

对于BarcodeReaderOptions对象,我们建议用户包含ExpectMultipleBarcodes以便IronBarcode扫描图像文件中的所有可用条形码,Speed用于在读取准确性和性能之间平衡,ExpectBarcodeTypes以进一步提高性能,以及ImageFilters用于应用ImageFilterCollection对象中设置的滤镜以提高读取准确性。

虽然对于大多数使用场景来说,设置BarcodeReaderOptions对象是可选的,但对于用户来说,要从多个页面的GIF和TIFF图像文件中读取条形码,充分利用IronBarcode是很重要的。

Hairil related to 高级条码阅读
海瑞尔 哈西米 本 奥马尔
软件工程师
像所有优秀的工程师一样,Hairil 是一个热衷学习的人。他正在精进自己的 C#、Python 和 Java 知识,并利用这些知识为 Iron Software 团队成员增添价值。Hairil 毕业于马来西亚的马来西亚工艺大学(Universiti Teknologi MARA),获得了化学与工艺工程学士学位,然后加入了 Iron Software 团队。