รหัส QR มีอยู่ทั่วไปและคุณสามารถสแกนได้และคุณจะได้รับของขวัญ ตอนนี้รหัส QR เป็นที่นิยมมาก ฉันเชื่อว่าทุกคนไม่ชัดเจนเกี่ยวกับวิธีการสร้างรหัส QR ตอนนี้บรรณาธิการจะแบ่งปันวิธีการช่วยให้คุณเรียนรู้วิธีการสร้างรหัส QR โดยการแบ่งปันบทความนี้
ในความเป็นจริงฟังก์ชั่นนี้ส่วนใหญ่ใช้โดยใช้ขวดที่เผยแพร่โดย Goggle
1. การสร้างรหัส QR
เพิ่มแพ็คเกจ zxing-core.jar ลงใน ClassPath
การสร้างรหัส QR ต้องการความช่วยเหลือของคลาส Matrixtoimagewriter คลาสนี้จัดทำโดย Google คุณสามารถคัดลอกคลาสไปยังซอร์สโค้ด ที่นี่ฉันวางซอร์สโค้ดของคลาสนี้และสามารถใช้โดยตรง
รหัสที่สามารถสร้างรหัส QR โดยตรง
โมฆะสาธารณะ test1 () โยนข้อยกเว้น {String content = "www.baidu.com"; String Path = "D: //"; MultiformatWriter MultiformatWriter = New MultiformatWriter (); // zxing เป็นบาร์โค้ดที่ให้โดย Google คำแนะนำแผนที่ = ใหม่ hashmap (); คำแนะนำ (encodeHintType.character_set, "UTF-8"); bitmatrix bitmatrix = multiformatwriter.encode (เนื้อหา, barcodeformat.qr_code, 400, 400, คำแนะนำ); // นี่คือขนาดของไฟล์ไฟล์ไฟล์ไฟล์ 1 = ไฟล์ใหม่ (เส้นทาง "my.jpg"); matrixtoimagewriter.writeTofile (Bitmatrix, "jpg", file1); System.out.println ("เสร็จสิ้นการดำเนินการ"); -เมื่อเราพบว่าหลังจากคัดลอกรหัสนี้เราพบว่ามีข้อผิดพลาดที่รายงานโดย Matrixtoimagewriter ดังนั้นเราจึงต้องมองหามัน แต่ฉันโพสต์รหัสที่นี่และสามารถใช้งานได้โดยตรง
นำเข้า com.google.zxing.common.bitmatrix; นำเข้า Javax.imageio.imageio; นำเข้า Java.io.File; นำเข้า Java.io.OutputStream; นำเข้า java.io.ioException; นำเข้า java.awt.image.bufferedimage; ระดับสุดท้ายระดับสุดท้าย MATRIXTOIMAGEWRITER {ส่วนตัวคงที่ int int Black = 0xFF000000; private static final int white = 0xffffffff; Private Matrixtoimagewriter () {} public bufferedimage tobufferedImage (bitmatrix matrix) {int width = matrix.getWidth (); ความสูง int = matrix.getheight (); BufferedImage Image = New BufferedImage (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); สำหรับ (int x = 0; x <width; x ++) {สำหรับ (int y = 0; y <ความสูง; y ++) {image.setrgb (x, y, matrix.get (x, y)? สีดำ: สีขาว); }} return image; } โมฆะสาธารณะคงที่ writeTofile (bitmatrix matrix, รูปแบบสตริง, ไฟล์ไฟล์) พ่น IOException {bufferedImage image = tobufferedImage (เมทริกซ์); if (! imageio.write (รูปภาพ, รูปแบบ, ไฟล์)) {โยน iOexception ใหม่ ("ไม่สามารถเขียนรูปภาพของรูปแบบ" + รูปแบบ + "ถึง" + ไฟล์); }} โมฆะสาธารณะคงที่ writeToStream (bitmatrix matrix, รูปแบบสตริง, outputstream สตรีม) พ่น IOException {bufferedImage image = tobufferedImage (เมทริกซ์); if (! imageio.write (รูปภาพ, รูปแบบ, สตรีม)) {โยน iOexception ใหม่ ("ไม่สามารถเขียนรูปภาพของรูปแบบ" + รูปแบบ); -ตอนนี้คุณสามารถดาวน์โหลดไดเรกทอรีรูทของดิสก์ D เพื่อดูรหัส QR ที่สร้างขึ้น
การวิเคราะห์รหัส QR
เช่นเดียวกับรุ่นเราจำเป็นต้องมีคลาสเสริม (bufferedimageluminancesource) ซึ่งจัดทำโดย Google ด้วย ที่นี่ฉันยังวางซอร์สโค้ดของคลาสนี้และสามารถคัดลอกและใช้โดยตรงบันทึกปัญหาการค้นหา
Bufferedimageluminancesource นำเข้า com.google.zxing.luminancesource; นำเข้า java.awt.graphics2d; นำเข้า java.awt.geom.affinetransform; นำเข้า java.awt.image.bufferedimage; Public Final Class Bufferedimageluminancesource ขยาย Luminancesource {ภาพบัฟเฟอร์ขั้นสุดท้ายส่วนตัว; INT สุดท้ายส่วนตัว INT สุดท้ายส่วนตัว Public BufferedimageLuminancesource (BufferedImage Image) {นี้ (ภาพ, 0, 0, image.getWidth (), image.getheight ()); } Public BufferedimageLuminancesource (BufferedImage Image, int ซ้าย, int บน, ความกว้าง int, ความสูง int) {super (ความกว้าง, ความสูง); int sourceWidth = image.getWidth (); int sourceHeight = image.getheight (); if (ซ้าย + ความกว้าง> SourceWidth || Top + ความสูง> SourceHeight) {โยน unlegalargumentException ใหม่ ("สี่เหลี่ยมผืนผ้าพืชไม่พอดีกับข้อมูลภาพ"); } สำหรับ (int y = top; y <top+ความสูง; y ++) {สำหรับ (int x = ซ้าย; x <ซ้าย+width; x ++) {ถ้า ((image.getRgb (x, y) & 0xff000000) == 0) {image.setrgb (x, y, 0xffffff // = สีขาว}}} this.image = ใหม่ bufferedImage (SourceWidth, SourceHeight, bufferedImage.type_byte_gray); this.image.getGraphics (). drawImage (ภาพ, 0, 0, null); this.left = ซ้าย; this.top = top; } @Override Public Byte [] getrow (int y, byte [] แถว) {ถ้า (y <0 || y> = getheight ()) {โยน unlegalargumentException ใหม่ ("แถวที่ร้องขออยู่นอกภาพ:" + y); } int width = getWidth (); if (row == null || row.length <width) {row = byte ใหม่ [width]; } image.getRaster (). getDataelements (ซ้าย, บน + y, ความกว้าง, 1, แถว); แถวกลับ; } @Override ไบต์สาธารณะ [] getMatrix () {int width = getWidth (); ความสูง int = getheight (); พื้นที่ int = ความกว้าง * ความสูง; ไบต์ [] เมทริกซ์ = ไบต์ใหม่ [พื้นที่]; image.getraster (). getDataelements (ซ้าย, บน, ความกว้าง, ความสูง, เมทริกซ์); กลับมาเมทริกซ์; } @Override บูลีนสาธารณะ iscropsupported () {return true; } @Override public luminancesource crop (int ซ้าย, int top, ความกว้าง int, ความสูง int) {ส่งคืน bufferedimageluminancesource ใหม่ (ภาพ, this.left + ซ้าย, top + ด้านบน, ความกว้าง, ความสูง); } @Override บูลีนสาธารณะ isrotatesupported () {return true; } @Override สาธารณะ luminancesource rotatecounterClockwise () {int sourceWidth = image.getWidth (); int sourceHeight = image.getheight (); affineTransform transform = new affineTransform (0.0, -1.0, 1.0, 0.0, 0.0, SourceWidth); bufferedImage rotatedImage = bufferedImage ใหม่ (sourceHeight, SourceWidth, bufferedImage.type_byte_gray); graphics2d g = rotatedImage.createGraphics (); G.DrawImage (ภาพ, แปลง, null); G.Dispose (); ความกว้าง int = getWidth (); ส่งคืน bufferedimageluminancesource ใหม่ (rotatedImage, ด้านบน, sourceWidth - (ซ้าย + ความกว้าง), getheight (), ความกว้าง); -รหัสเพื่อแยกวิเคราะห์รหัส QR
multiformatreader formatReader = new MultiformatReader (); String filePath = "เส้นทางของภาพ"; ไฟล์ไฟล์ = ไฟล์ใหม่ (filePath); bufferedImage image = imageio.read (ไฟล์) ;; Luminancesource Source = ใหม่ bufferedimageluminancesource (ภาพ); binarizer binarizer = ใหม่ไฮบริดบินารี (แหล่งที่มา); BinaryBitMap BinaryBitmap = ใหม่ BinaryBitMap (binarizer); คำแนะนำแผนที่ = ใหม่ hashmap (); คำแนะนำ (encodeHintType.character_set, "UTF-8"); ผลลัพธ์ผลลัพธ์ = formatReader.decode (BinaryBitMap, คำแนะนำ); System.out.println ("result ="+ result.toString ()); System.out.println ("resultFormat ="+ result.getBarCodeFormat ()); System.out.println ("resultText ="+ result.getText ()); TCH (Exception E) {E.PrintStackTrace ();ตอนนี้วิธีนี้คุณสามารถดูเนื้อหาของรหัส QR บนคอนโซล
ข้างต้นคือความรู้ที่เกี่ยวข้องเกี่ยวกับวิธีการสร้างรหัส QR โดย ZXING-CORE และวิเคราะห์พวกเขา ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!