ZXING เป็นเครื่องมือแยกวิเคราะห์เกี่ยวกับบาร์โค้ด (รหัสหนึ่ง R, รหัส QR) ที่ Google จัดทำโดย Google มันมีวิธีการในการสร้างและวิเคราะห์รหัส QR ตอนนี้ฉันจะแนะนำการใช้ Java เพื่อสร้างและวิเคราะห์รหัส QR โดยใช้ ZXING
1. การสร้างรหัส QR
1.1 เพิ่มแพ็คเกจ ZXING-core.jar ลงใน ClassPath
1.2 การสร้างรหัส QR ต้องการความช่วยเหลือของคลาส Matrixtoimagewriter คลาสนี้จัดทำโดย Google คุณสามารถคัดลอกคลาสไปยังซอร์สโค้ด ที่นี่ฉันวางซอร์สโค้ดของคลาสนี้และสามารถใช้โดยตรง
นำเข้า 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 ใหม่ ("ไม่สามารถเขียนรูปภาพของรูปแบบ" + รูปแบบ); -1.3 เขียนรหัสการใช้งานที่สร้างรหัส QR
ลอง {สตริงเนื้อหา = "120605181003; http: //www.cnblogs.com/jtmjx"; String Path = "C:/ผู้ใช้/ผู้ดูแลระบบ/เดสก์ท็อป/ทดสอบ"; MultiformatWriter MultiformatWriter = new MultiformatWriter (); คำแนะนำแผนที่ = ใหม่ hashmap (); คำแนะนำ (encodeHintType.character_set, "UTF-8"); Bitmatrix BitMatrix = MultiformatWriter.ENCODE (เนื้อหา, BARCODEFORMAT.QR_CODE, 400, 400, คำแนะนำ); ไฟล์ไฟล์ 1 = ไฟล์ใหม่ (พา ธ , "nambeginner.jpg"); matrixtoimagewriter.writeTofile (Bitmatrix, "jpg", file1); } catch (exception e) {e.printstacktrace (); -ตอนนี้หลังจากทำงานแล้วคุณสามารถสร้างภาพรหัส QR มันไม่ง่ายมากเหรอ? ถัดไปลองดูวิธีการแยกรหัส QR
2. การวิเคราะห์รหัส QR
2.1 เพิ่มแพ็คเกจ zxing-core.jar ไปยัง ClassPath
2.2 เช่นเดียวกับรุ่นเราต้องการคลาสเสริม (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 (), ความกว้าง); -2.3 เขียนรหัสการใช้งานที่แยกวิเคราะห์รหัส QR
ลอง {multiformatreader formatReader = new MultiformatReader (); String filePath = "C: /users/administrator/desktop/testimage/test.jpg"; ไฟล์ไฟล์ = ไฟล์ใหม่ (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 ()); } catch (exception e) {e.printstacktrace (); -ตอนนี้หลังจากทำงานแล้วคุณจะเห็นว่าคอนโซลพิมพ์เนื้อหาของรหัส QR
จนถึงตอนนี้ ZXING ใช้ ZXING เพื่อสร้างและแยกวิเคราะห์รหัส QR และการสาธิตเสร็จสมบูรณ์ส่วนใหญ่ทำบันทึกสำหรับตัวคุณเองและในเวลาเดียวกันทำให้ความสะดวกสำหรับผู้ที่ต้องการ ฮิฮิ
ตัวอย่างง่าย ๆ ข้างต้นของ Java โดยใช้ ZXING เพื่อสร้างรหัส QR คือเนื้อหาทั้งหมดที่ฉันแบ่งปันกับคุณ ฉันหวังว่าคุณจะให้ข้อมูลอ้างอิงและฉันหวังว่าคุณจะสนับสนุน wulin.com มากขึ้น