This tutorial shows how to use the LTCodecsImageInfo class to gather information about the various image files supported by LEADTOOLS in a macOS Swift Console application.
Overview | |
---|---|
Summary | This tutorial covers how to use the LTCodecsImageInfo class in a macOS Swift Console application. |
Completion Time | 15 minutes |
Visual Studio Project | Download tutorial project (4 KB) |
Platform | macOS Swift Console Application |
IDE | Xcode |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Extract Image Information - macOS Swift Console tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. This tutorial requires the following Framework references located at <INSTALL_DIR>\LEADTOOLS23\Bin\Xcode\Frameworks\macOS
:
Leadtools.Codecs.Cmp.framework
Leadtools.Codecs.framework
Leadtools.framework
Edit the Leadtools-Bridging-Header.h
file to add the following imports:
#import <Leadtools.Codecs/Leadtools.Codecs.h>
For a complete list of which files are required for your application, refer to Files to be Included in your Application.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
Note: Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, and the license set, coding can begin.
Using the Project Navigator, open main.swift
. Add a new function named RasterCodecsImageInfo(path: String);
. The function's parameter will be the file path to the image from which the information is to be gathered. This new function will be called below the call to SetLicense()
, as shown below.
SetLicense()
RasterCodecsImageInfo(path: "<INSTALL_DIR>/LEADTOOLS23/Resources/Images/image1.cmp")
Add the below code to the RasterCodecsImageInfo()
function to use the LTCodecsImageInfo
class to gather information on the given image file.
func RasterCodecsImageInfo(path: String) {
let codecs = LTRasterCodecs()
let fileName = URL(fileURLWithPath: path)
let info: LTCodecsImageInfo = try! codecs.imageInformation(url: fileName, totalPages: true)
let inputFileName = fileName.deletingPathExtension().lastPathComponent
let codecsInfoString = String(
"Image Format: \(info.format)\n" +
"Information for: \(inputFileName)\n" +
"BitsPerPixel: \(info.bitsPerPixel)\n" +
"BytesPerLine: \(info.bytesPerLine)\n" +
"ColorSpace: \(info.colorSpace)\n" +
"Byte Order: \(info.order)\n" +
"Image Height: \(info.height)\n" +
"Image Width: \(info.width)\n" +
"Image X Resolution: \(info.xResolution)\n" +
"Image Y Resolution: \(info.yResolution)\n" +
"Compression: \(info.compression ?? "")\n" +
"Page Number: \(info.pageNumber)\n" +
"Total Pages: \(info.totalPages)"
)
print(codecsInfoString)
}
Note: There are more properties inside the
LTCodecsImageInfo
class. The snippet above showcases the most commonly used properties.
Clean the project to clear any errors by selecting Product -> Clean Build Folder or Shift + Command + K.
Run the project by selecting Product -> Run or Command + R.
If the steps were followed correctly, the application runs and the console displays the file's information.
This tutorial showed how to use the LTCodecsImageInfo
class to gather information on a given image file.