บทช่วยสอนฉบับย่อนี้จะอธิบายขั้นตอนในการ สร้างบาร์โค้ดแบบหมุนใน Java เป็นเรื่องปกติมากที่จะใช้ภาพบาร์โค้ดที่มุมต่างๆ ในเอกสารหรือบนบรรจุภัณฑ์ ฯลฯ ตามข้อกำหนดการออกแบบเฉพาะ คุณสามารถ ตั้งค่าการหมุนบาร์โค้ดใน java โดยมีขนาดและการวางแนวต่างๆ ตามความต้องการของคุณ และบันทึกเป็น PNG หรือรูปภาพประเภทอื่นๆ
ขั้นตอนในการสร้าง Rotated Barcode ใน Java
- กำหนดค่าโครงการของคุณเพื่อเพิ่มไลบรารี Aspose.BarCode จาก Maven Repository
- สร้างอินสแตนซ์ใหม่ของ BarcodeGenerator และตั้งค่าการเข้ารหัสเริ่มต้นเป็น Code128 โดยใช้ EncodeTypes
- ตั้งค่าข้อความบางส่วนที่จะเข้ารหัสในภาพบาร์โค้ดที่ส่งออก
- กำหนดมุมการหมุนของภาพที่ส่งออกโดยกำหนดจำนวนองศาที่ต้องการ
- บันทึกภาพบาร์โค้ดที่หมุนเอาต์พุตเป็นไฟล์ PNG ลงในแผ่นดิสก์
จำเป็นต้องใช้อินสแตนซ์คลาส BarcodeGenerator สำหรับ การสร้างและการหมุนบาร์โค้ดใน Java สามารถใช้เพื่อตั้งค่าเริ่มต้นการเข้ารหัส ข้อความบาร์โค้ด และมุมการหมุนเป็นองศา ในที่สุดคุณสามารถบันทึกภาพบาร์โค้ดที่หมุนแล้วเป็น PNG หรือภาพประเภทอื่น ๆ ตามสภาพแวดล้อม
รหัสเพื่อตั้งค่าการหมุนบาร์โค้ดใน Java
import com.aspose.barcode.License; | |
import com.aspose.barcode.generation.BarCodeImageFormat; | |
import com.aspose.barcode.generation.BarcodeGenerator; | |
import com.aspose.barcode.generation.EncodeTypes; | |
public class CreateRotatedBarCodeInJava { | |
public static void main(String[] args) throws Exception { // main method to rotate barcode image using Java | |
// Set Aspose.Barcode license to avoid trial version watermark before generating barcode and rotating it | |
License barCodeLicense = new License(); | |
barCodeLicense.setLicense("Aspose.BarCode.lic"); | |
// Instantiate a barcode generator and set the encoding type to Code128 | |
BarcodeGenerator BarCodeToRotate = new BarcodeGenerator(EncodeTypes.CODE_128); | |
BarCodeToRotate.setCodeText("Sample barcode text"); | |
// Set the rotation angle of the barcode image to 60 | |
BarCodeToRotate.getParameters().setRotationAngle(60); | |
// Save the barcode image as a PNG file after rotating it with 60-degree angle | |
BarCodeToRotate.save("BarcodeImageRotatedAtAnAngle.png", BarCodeImageFormat.PNG); | |
} | |
} |
โค้ดตัวอย่างนี้ใช้อ็อบเจกต์คลาส BarcodeGenerator เพื่อทำงานนี้ สำหรับการตั้งค่าประเภทการเข้ารหัสจะใช้ Enumerator EncodeTypes ซึ่งมีประเภทการเข้ารหัสทั่วไปทั้งหมดที่ใช้ในบาร์โค้ด ในทำนองเดียวกัน ขณะบันทึกรูปภาพผลลัพธ์สุดท้าย ให้ใช้ตัวแจงนับ BarCodeImageFormat เพื่อเลือกประเภทรูปภาพ
บทช่วยสอนนี้ สร้างภาพบาร์โค้ดแบบหมุนใน Java อย่างไรก็ตาม หากคุณต้องการสร้าง QR Code อย่างง่าย โปรดดูบทความใน วิธีสร้าง QR Code โดยใช้ Java