Java: ตัวอย่างตัวสร้างรหัส QR อย่างง่าย – สร้างรหัส QR ฟรี
เผยแพร่แล้ว: 2020-01-16
สแกนสิ่งนี้: คุณจะถูกเปลี่ยนเส้นทางไปที่ https://crunchify.com
QR code
(ย่อมาจาก Quick Response Code
) เป็นเครื่องหมายการค้าของบาร์โค้ดประเภทเมตริกซ์ (หรือบาร์โค้ดสองมิติ) ที่ออกแบบมาสำหรับอุตสาหกรรมยานยนต์ในญี่ปุ่นเป็นครั้งแรก บาร์โค้ดคือฉลากที่เครื่องอ่านค่าได้ซึ่งติดมากับสินค้าที่บันทึกข้อมูลที่เกี่ยวข้องกับสินค้านั้น ผู้ถือสิทธิบัตรได้เลือกที่จะไม่ใช้สิทธิดังกล่าวในขั้นต้นซึ่งได้รับสิทธิบัตรในขั้นต้น เมื่อเร็วๆ นี้ ระบบ QR Code ได้รับความนิยมนอกอุตสาหกรรมยานยนต์ เนื่องจากสามารถอ่านได้รวดเร็วและมีพื้นที่จัดเก็บที่มากกว่าเมื่อเทียบกับบาร์โค้ด UPC มาตรฐาน
รหัสประกอบด้วยโมดูลสีดำ (จุดสี่เหลี่ยม) ที่จัดเรียงเป็นตารางสี่เหลี่ยมบนพื้นหลังสีขาว ZXING
เป็นไลบรารีประมวลผลภาพบาร์โค้ด 1D / 2D หลายรูปแบบพร้อมไคลเอนต์สำหรับ Android, Java เป็นไลบรารีประมวลผลภาพบาร์โค้ด 1D/2D แบบโอเพนซอร์สที่มีหลายรูปแบบซึ่งใช้งานใน Java พร้อมพอร์ตไปยังภาษาอื่นๆ
ตัวอย่างการชำระเงินสด:
เรามุ่งเน้นที่การใช้กล้องในตัวบนโทรศัพท์มือถือเพื่อสแกนและถอดรหัสบาร์โค้ดบนอุปกรณ์ โดยไม่ต้องสื่อสารกับเซิร์ฟเวอร์ อย่างไรก็ตาม โครงการนี้สามารถใช้เพื่อเข้ารหัสและถอดรหัสบาร์โค้ดบนเดสก์ท็อปและเซิร์ฟเวอร์ได้เช่นกัน
นี่คือ Java Code ง่ายๆ ที่สร้างรหัส QR ให้กับคุณ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
package crunchify . com . tutorials ; import com . google . zxing . BarcodeFormat ; import com . google . zxing . EncodeHintType ; import com . google . zxing . WriterException ; import com . google . zxing . common . BitMatrix ; import com . google . zxing . qrcode . QRCodeWriter ; import com . google . zxing . qrcode . decoder . ErrorCorrectionLevel ; import javax . imageio . ImageIO ; import java . awt . * ; import java . awt . image . BufferedImage ; import java . io . File ; import java . io . IOException ; import java . util . EnumMap ; import java . util . Map ; /** * @author Crunchify.com * Simple QR Code Generator Example - Create QR codes for free. */ public class CrunchifyQRCodeGenerator { public static void main ( String [ ] args ) { String myCodeText = "https://crunchify.com" ; String filePath = "//cdn.crunchify.com/Users/app/Document/Crunchify.com-QRCode.png" ; int size = 512 ; String crunchifyFileType = "png" ; File crunchifyFile = new File ( filePath ) ; try { Map < EncodeHintType , Object > crunchifyHintType = new EnumMap < EncodeHintType , Object > ( EncodeHintType . class ) ; crunchifyHintType . put ( EncodeHintType . CHARACTER_SET , "UTF-8" ) ; // Now with version 3.4.1 you could change margin (white border size) crunchifyHintType . put ( EncodeHintType . MARGIN , 1 ) ; /* default = 4 */ Object put = crunchifyHintType . put ( EncodeHintType . ERROR_CORRECTION , ErrorCorrectionLevel . H ) ; QRCodeWriter mYQRCodeWriter = new QRCodeWriter ( ) ; // throws com.google.zxing.WriterException BitMatrix crunchifyBitMatrix = mYQRCodeWriter . encode ( myCodeText , BarcodeFormat . QR_CODE , size , size , crunchifyHintType ) ; int CrunchifyWidth = crunchifyBitMatrix . getWidth ( ) ; // The BufferedImage subclass describes an Image with an accessible buffer of crunchifyImage data. BufferedImage crunchifyImage = new BufferedImage ( CrunchifyWidth , CrunchifyWidth , BufferedImage . TYPE_INT_RGB ) ; // Creates a Graphics2D, which can be used to draw into this BufferedImage. crunchifyImage . createGraphics ( ) ; // This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. // This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform. Graphics2D crunchifyGraphics = ( Graphics2D ) crunchifyImage . getGraphics ( ) ; // setColor() sets this graphics context's current color to the specified color. // All subsequent graphics operations using this graphics context use this specified color. crunchifyGraphics . setColor ( Color . white ) ; // fillRect() fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. crunchifyGraphics . fillRect ( 0 , 0 , CrunchifyWidth , CrunchifyWidth ) ; // TODO: Please change this color as per your need crunchifyGraphics . setColor ( Color . BLUE ) ; for ( int i = 0 ; i < CrunchifyWidth ; i ++ ) { for ( int j = 0 ; j < CrunchifyWidth ; j ++ ) { if ( crunchifyBitMatrix . get ( i , j ) ) { crunchifyGraphics . fillRect ( i , j , 1 , 1 ) ; } } } // A class containing static convenience methods for locating // ImageReaders and ImageWriters, and performing simple encoding and decoding. ImageIO . write ( crunchifyImage , crunchifyFileType , crunchifyFile ) ; System . out . println ( "\nCongratulation.. You have successfully created QR Code.. \n" + "Check your code here: " + filePath ) ; } catch ( WriterException e ) { System . out . println ( "\nSorry.. Something went wrong...\n" ) ; e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } } |
ผลลัพธ์: (สแกนด้วยตัวเอง)

ฉันต้องดาวน์โหลดห้องสมุดใด
หากคุณมีโปรเจ็กต์ Maven ให้รวมการพึ่งพานี้กับไฟล์ pom.xml ของคุณ

1 2 3 4 5 6 |
< dependency > < groupId > com . google . zxing < / groupId > < artifactId > core < / artifactId > < version > 3.4.1 < / version > < / dependency > |

หรือดาวน์โหลดไฟล์ .jar ด้วยตนเอง
ขั้นตอนที่ 1:
ลิ้งค์ดาวน์โหลด.

ขั้นตอนที่ 2
เมื่อคุณดาวน์โหลดแล้ว คุณจะต้องรวมไว้ใน classpath ของโปรเจ็กต์ วิธีเพิ่มไฟล์ .jar ให้กับ Project Build Path ใน Eclipse

แจ้งให้เราทราบหากคุณประสบปัญหาใด ๆ ในการสร้างรหัส QR