ตัวอย่างวิธีใช้ง่ายๆ นี้เกี่ยวกับวิธี สร้าง QR code ใน C+* รหัส QR เป็นบาร์โค้ดสองมิติประเภทหนึ่งและมักใช้ในแอปพลิเคชันต่างๆ โดยนักพัฒนา C++ ในแอปพลิเคชัน C++ QR code generator สามารถพัฒนาได้ด้วยไม่กี่ขั้นตอนและการเรียกใช้ API ที่ง่ายมากโดยไม่ต้องพึ่งพา API หรือเครื่องมือของบุคคลที่สาม แอปพลิเคชันที่พัฒนาขึ้นสามารถใช้กับแอปพลิเคชันที่ใช้ C++ ภายในสภาพแวดล้อม MS Windows
ขั้นตอนในการสร้างรหัส QR ใน C ++
- เพิ่ม Aspose.Barcode for C++ โดยใช้เครื่องมือ NuGet package manager ในแอปพลิเคชันของคุณ
- เพิ่มการอ้างอิงถึง Aspose::Barcode และ AsposeBarCode::Generation เนมสเปซ
- รวมการอ้างอิงไฟล์ส่วนหัวที่จำเป็นในแอปพลิเคชันของคุณ
- เริ่มต้นวัตถุคลาส BarcodeGenerator เพื่อสร้างโค้ด QR โดยตั้งค่า QR เป็นประเภทการเข้ารหัส
- ตั้งค่าข้อความรหัส QR และพารามิเตอร์อื่นๆ
- การใช้วิธีบันทึกใน C ++ สร้างรหัส QR ในรูปแบบภาพ PNG
เพื่อพัฒนา ตัวสร้างรหัส QR รหัส C++ ตามที่ระบุไว้ในขั้นตอนข้างต้นสามารถใช้ได้ หลังจากติดตั้งแพ็คเกจ API โดยใช้ NuGet และรวมไฟล์ส่วนหัวที่จำเป็นในแอปพลิเคชันแล้ว เราจะเริ่มกระบวนการด้วยอินสแตนซ์ของคลาส BarcodeGenerator และตั้งค่าประเภทการเข้ารหัสเป็น QR คุณสามารถใช้ EncodingTypes อื่นๆ ได้ แต่ในตัวอย่างนี้ เรากำลังเน้นไปที่คิวอาร์โค้ด หลังจากนั้น เราจะตั้งค่าข้อความรหัส QR และพารามิเตอร์ที่สำคัญอื่นๆ เช่น ความละเอียดของรหัส QR ในที่สุดรหัส QR จะถูกบันทึกไว้ในดิสก์โดยใช้วิธีการบันทึก
ตัวอย่างการสร้าง QR Code ใน C++
#pragma once | |
#include <system/string.h> | |
#include <system/shared_ptr.h> | |
#include <stdio.h> | |
#include <system/console.h> | |
#include <system/environment.h> | |
#include <system/object_ext.h> | |
#include <Licensing/License.h> | |
#include <BarCode.Generation/BarcodeGenerator.h> | |
#include <BarCode.Generation/EncodeTypes/EncodeTypes.h> | |
#include <BarCode.Generation/EncodeTypes/SymbologyEncodeType.h> | |
#include <BarCode.Generation/EncodeTypes/BarcodeClassifications.h> | |
#include <BarCode.Generation/EncodeTypes/BaseEncodeType.h> | |
#include <BarCode.Generation/GenerationParameters/BarCodeImageFormat.h> | |
#include <BarCode.Generation/GenerationParameters/BarcodeParameters.h> | |
#include <BarCode.Generation/GenerationParameters/BaseGenerationParameters.h> | |
#include <BarCode.Generation/Helpers/Unit.h> | |
using namespace System; | |
using namespace Aspose::BarCode; | |
using namespace Aspose::BarCode::Generation; | |
class QRCodeGenerator { | |
public: | |
static void GenerateQRCode() | |
{ | |
// Set the license for Aspose.BarCode for C++ to create QR Code | |
SharedPtr<License> CreateBarcodeLicense = System::MakeObject<License>(); | |
CreateBarcodeLicense->SetLicense(u"Aspose.Barcode.NET.lic"); | |
// Initialize Barcode generator for QR code type | |
System::SharedPtr<BarcodeGenerator> QRGenerator = System::MakeObject<BarcodeGenerator>(EncodeTypes::QR); | |
// Setting QR Code Text | |
QRGenerator->set_CodeText(u"Text To Encode"); | |
// Setting QR code dimension and resolution | |
QRGenerator->get_Parameters()->get_Barcode()->get_XDimension()->set_Millimeters(1.0f); | |
QRGenerator->get_Parameters()->set_Resolution(300); | |
// Save the QR code as PNG image on disk | |
QRGenerator->Save(u"barcode-codetext_out.png", BarCodeImageFormat::Png); | |
} | |
}; |
ในตัวอย่างโค้ดข้างต้น เราเห็นว่ามีการใช้การเรียก API ของ QR โค้ด C++ เราสามารถรับรหัส QR ในรูปแบบภาพเอาต์พุตต่างๆ เช่น PNG, Tiff, JPEG หรือรูปแบบ BMP คุณยังสามารถปรับแต่งความละเอียด สีพื้นหลัง และความกว้างและความสูงของภาพโค้ด QR โดยใช้โค้ด C++ อย่างง่าย
ในหัวข้อนี้ เราได้เห็นแล้วว่าการพัฒนา ตัวสร้างรหัส QR ที่ใช้ C++ API นั้นมีอินเทอร์เฟซและขั้นตอนของ API ที่ง่ายมาก หากคุณต้องการปรับปรุงแอปพลิเคชันของคุณเพิ่มเติมโดยการเพิ่มรูปภาพโค้ด QR ที่สร้างขึ้นใน DOCX โปรดอ่านบทความเกี่ยวกับวิธีการ เพิ่มรูปภาพใน DOCX โดยใช้ C++