ตัวอย่างง่ายๆ นี้แสดงวิธี อ่าน Barcode โดยใช้ C+* เพื่อพัฒนา เครื่องอ่านบาร์โค้ด ซอร์สโค้ด C++ จะใช้ภายในแอปพลิเคชันใดๆ ที่รองรับ C++ ใน MS Windows โดยทำตามขั้นตอนการเข้ารหัสสองสามขั้นตอน เราจะมาดูกันว่าเราสามารถอ่านบาร์โค้ดประเภทต่างๆ พร้อมกันได้อย่างไรจากไฟล์เดียวโดยไม่ต้องพึ่งพาเครื่องมือของบุคคลที่สามหรือ API อื่นๆ
ขั้นตอนการอ่านบาร์โค้ดด้วยภาษา C++
- เพิ่ม Aspose.BarCode for C++ API โดยใช้ NuGet package manager ในแอปพลิเคชันของคุณ
- รวมการอ้างอิงถึงเนมสเปซ Aspose::BarCode และ Aspose::BarCode::BarCodeRecognition
- เพิ่มการอ้างอิงไฟล์ส่วนหัวที่จำเป็นในแอปพลิเคชันของคุณ
- ใช้อาร์เรย์ BaseDecodeType เพื่อตั้งค่าประเภทบาร์โค้ดที่ต้องการให้อ่านจากไฟล์ต้นฉบับ
- เริ่มต้นวัตถุคลาส BarCodeReader เพื่ออ่านบาร์โค้ดจากไฟล์ PNG
- ทำซ้ำผ่านข้อมูลบาร์โค้ดที่แยกออกมาและแสดงในคอนโซล
ในแอปพลิเคชัน C++ Barcode scanner สามารถพัฒนาได้โดยปฏิบัติตามขั้นตอนที่กล่าวถึงข้างต้นและโค้ดไม่กี่บรรทัด ประการแรก คุณจะต้องเพิ่มการอ้างอิง API โดยใช้ NugGet และรวมไฟล์ส่วนหัวที่จำเป็น ในขั้นตอนถัดไป คุณจะตั้งค่าประเภทบาร์โค้ดที่คุณต้องการพัฒนาเครื่องอ่านโดยใช้ตัวแจงนับ DecodeType ด้วยการใช้อินสแตนซ์คลาส BarCodeReader เราจะจัดเตรียมเส้นทางสำหรับไฟล์ PNG ต้นทางที่มีบาร์โค้ดที่แตกต่างกันและอาร์เรย์ประเภทบาร์โค้ดที่ต้องการ สุดท้าย เราจะวนซ้ำผ่านข้อมูลบาร์โค้ดที่อ่านได้ และจะแสดงในคอนโซล
รหัสเพื่ออ่านบาร์โค้ดโดยใช้ Java
#pragma once | |
#include <system/primitive_types.h> | |
#include <system/object.h> | |
#include <system/string.h> | |
#include <system/shared_ptr.h> | |
#include <system/primitive_types.h> | |
#include <system/object.h> | |
#include <system/details/dispose_guard.h> | |
#include <system/console.h> | |
#include <system/collections/list.h> | |
#include <system/collections/ienumerator.h> | |
#include <system/array.h> | |
#include <cstdint> | |
#include <system/text/encoding.h> | |
#include <stdio.h> | |
#include <system/console.h> | |
#include <system/environment.h> | |
#include <Licensing/License.h> | |
#include <BarCodeRecognition/Recognition/RecognitionSession/DecodeTypes/SingleDecodeType.h> | |
#include <BarCodeRecognition/Recognition/RecognitionSession/BarcodeReader.h> | |
#include <BarCodeRecognition/Recognition/RecognitionSession/DecodeTypes/DecodeType.h> | |
#include <BarCodeRecognition/Recognition/RecognitionSession/DecodeTypes/BaseDecodeType.h> | |
#include <BarCodeRecognition/Recognition/RecognitionSession/BarcodeResults/BarCodeResult.h> | |
#include <BarCodeRecognition/Recognition/RecognitionSession/BarcodeResults/BarCodeResult.h> | |
using namespace System; | |
using namespace Aspose::BarCode; | |
using namespace Aspose::BarCode::BarCodeRecognition; | |
class BarcodeReader { | |
public: | |
static void ReadBarcode() | |
{ | |
// Set the license for Aspose.BarCode for C++ to create Barcode | |
SharedPtr<License> CreateBarcodeLicense = System::MakeObject<License>(); | |
CreateBarcodeLicense->SetLicense(u"Aspose.Barcode.NET.lic"); | |
// Set Barcode types for decoding | |
System::ArrayPtr<System::SharedPtr<BaseDecodeType>> barcodeTypes = System::MakeArray<System::SharedPtr<BaseDecodeType>> | |
({ DecodeType::Code39Standard, DecodeType::Pdf417, DecodeType::DataMatrix, DecodeType::QR, | |
DecodeType::Code39Extended, DecodeType::Code128, DecodeType::RM4SCC }); | |
System::SharedPtr <BarCodeReader> BarcodeReader = System::MakeObject<BarCodeReader>(u"multiple_codes.png", barcodeTypes); | |
System::Console::Write(u"ReadSimpleExample:"); | |
for (System::SharedPtr <BarCodeResult> codeResult : BarcodeReader->ReadBarCodes()) { | |
System::Console::WriteLine(System::String(u"Symbology type: ") + codeResult->get_CodeType()); | |
System::Console::WriteLine(System::String(u"Codetext: ") + codeResult->get_CodeText()); | |
} | |
} | |
}; |
ในตัวอย่างข้างต้น เพื่อสร้าง *เครื่องอ่านบาร์โค้ด แอปพลิเคชันที่ใช้ C++ ได้รับการพัฒนาโดยใช้โค้ดที่เข้าใจง่ายไม่กี่บรรทัด ซึ่งสามารถสแกนบาร์โค้ดประเภทต่างๆ ได้ ในแอปพลิเคชันนี้ เราได้มุ่งเน้นการพัฒนาเครื่องอ่านบาร์โค้ดสำหรับประเภท QR, Code 39 Standard, Pdf 417, Data Matrix, Code 128 และ RM 4 SCC แต่ API รองรับบาร์โค้ดประเภทอื่นๆ อีกมากมายที่คุณสามารถสำรวจได้ใน DecodeType enumerator เราใช้การเรียก API อย่างง่ายเพื่อพัฒนาแอปพลิเคชันการอ่าน C++ Barcode
ในตัวอย่างนี้ เราได้สำรวจแล้วว่าในการพัฒนา *Barcode Scanner ที่ใช้ API ที่ใช้ C++ และสามารถใช้คุณสมบัติที่เกี่ยวข้องได้ หากคุณสนใจสร้างคิวอาร์โค้ด โปรดดูบทความวิธีการ สร้าง QR Code โดยใช้ C++