สอนคุณทีละขั้นตอนโดยใช้ Java Design เพื่อสร้างรหัส QR
ในยุคของ Internet of Things รหัส QR เป็นสิ่งที่สำคัญมาก ตอนนี้ไม่ว่าคุณจะเป็นอะไรคุณต้องได้รับโลโก้รหัส QR เพราะกลัวว่าจะล้าสมัยคุณไม่สามารถใช้รหัส QR เพื่อระบุได้ บางทีวันหนึ่งสูติบัตรหรือสมุดลงทะเบียนครัวเรือนจะถูกระบุด้วยรหัส QR วันนี้ฉันอยู่ในความตั้งใจและเห็นว่าคนอื่น ๆ ได้เพิ่มรหัส QR ลงในบล็อกของพวกเขาและฉันก็ต้องการทำการทดสอบด้วย
ส่วนใหญ่จะใช้เพื่อให้ได้สองจุด:
1. สร้างรหัส QR ด้วยข้อความใด ๆ
2. เพิ่มภาพที่อยู่ตรงกลางของรหัส QR
1. การเตรียมงาน
เตรียมแพ็คเกจ QR QR CORE 3.0 และรูปภาพ JPG
ดาวน์โหลดแพ็คเกจ QR QR Code
ก่อนอื่นฉันต้องดาวน์โหลดแพ็คเกจ zxing.jar ฉันใช้แพ็คเกจหลัก 3.0 ที่นี่
ที่อยู่ดาวน์โหลด: ตอนนี้มันถูกย้ายไปที่ GitHub: https://github.com/zxing/zxing/wiki/getting-started-developing
แน่นอนคุณสามารถดาวน์โหลดแพ็คเกจ JAR จากที่เก็บ Maven: http://central.maven.org/maven2/com/google/zxing/core/
2. การเขียนโปรแกรม
1. เริ่มต้นคราสสร้างโครงการ Java ใหม่และค้นหาชื่อโครงการ (ในตัวอย่างนี้ตั้งชื่อมัน qrcodesoft) คลิกถัดไป:
2. นำเข้าแพ็คเกจ zxing.jar ฉันใช้แพ็คเกจหลักของ 3.0 ที่นี่: คลิก "เพิ่มขวดภายนอก (x) ... "
3. สร้างคลาสใหม่สองคลาสคือ:
Bufferedimageluminances.java
qrcodeutil.java
รหัสคีย์คือ: bufferedimageluminancesource.java และ qrcodeutil.java ซึ่งวิธีการที่ทดสอบหลักอยู่ใน qrcodeutil.java
Bufferedimageluminancesource.java รหัสโปรแกรม:
แพ็คเกจ com.yihaomen.barcode; นำเข้า java.awt.graphics2d นำเข้า java.awt.eom.affinetransform; นำเข้า java.awt.image.bufferedimage; นำเข้า com.google.zxing.luminancesource; ด้านบน; bufferedimageluminancesource (ภาพบัฟเฟอร์เมจ) {นี้ (ภาพ, 0, 0, image.getWidth (), image. -getheight ());} public Bufferedimageluminancesource (Bufferedimage image, int ซ้าย image.getheight (); ถ้า (ซ้าย + ความกว้าง> SourceWidth || Top + Height> SourceHeight) {โยน ungloralargumentException ใหม่ ("สี่เหลี่ยมผืนผ้าพืชไม่พอดีกับข้อมูลภาพ");} สำหรับ (int y = top; y <top + ความสูง; y ++) {สำหรับ (int x = ซ้าย 0xff000000) == 0) {image.setrgb (x, y, 0xffffff); // = สีขาว}}} this.image = ใหม่ bufferedImage (SourceWidth, SourceHeight, bufferedImage.type_byte_gray); this.image.getGraphics (). drawImage (ภาพ, 0, 0, null); > = getheight ()) {โยน unlegalargumentException ใหม่ ("แถวที่ร้องขออยู่นอกรูปภาพ:" + y);} int width = getWidth (); ถ้า (แถว == null || row.length <width) {แถว = นิวบิท getMatrix () {int width = getWidth (); ความสูง int = getheight (); พื้นที่ int = ความกว้าง * ความสูง; byte [] matrix = ไบต์ใหม่ [พื้นที่]; image.getRaster (). getDataelements (ซ้าย, ความกว้าง, ความสูง, matrix); ซ้าย, int ด้านบน, ความกว้าง int, ความสูง int) {ส่งคืน bufferedimageluminancesource ใหม่ (ภาพ, this.left + ซ้าย, this.top + ด้านบน, ความกว้าง, ความสูง); image.getheight (); affineTransform transform = new affineTransform (0.0, -1.0, 1.0, 0.0, SourceWidth); bufferedimage rotatedimage = bufferedimage ใหม่ NULL); G.Dispose (); int width = getWidth (); คืน bufferedimageluminancesource ใหม่ (rotatedImage, ด้านบน, sourceWidth - (ซ้าย + ความกว้าง), getheight (), ความกว้าง);}}}รหัสโปรแกรม qrcodeutil.java:
แพ็คเกจ com.yihaomen.barcode; นำเข้า java.awt.basicstroke; นำเข้า java.awt.graphics; นำเข้า java.awt.graphics2d นำเข้า java.awt.image; นำเข้า java.awt.shape; java.io.file; นำเข้า java.io.OutputStream; นำเข้า java.util.hashtable; นำเข้า java.util.random; นำเข้า javax.imageio.imageio; นำเข้า com.google.zxing.barcodeformat; com.google.zxing.encodehinttype; นำเข้า com.google.zxing.multiformatreader; นำเข้า com.google.zxing.multiformatwriter; นำเข้า com.google.zxing.result; นำเข้า com.google.zxing.Common.bitmatrix; com.google.zxing.qrcode.decoder.errorcorrectionLevel;/*** qr รหัสเครื่องมือคลาส**/คลาสสาธารณะ qrcodeutil {สตริงคงสุดท้ายส่วนตัว charset = "UTF-8"; 60; // ความสูงของโลโก้ความสูงคงที่ระดับสุดท้าย = 60; bufferedImage แบบคงที่ส่วนตัว createimage (เนื้อหาสตริง, สตริง imgpath, บูลีน needcompress) โยนข้อยกเว้น {hashtable <encodeHinttype, Object> คำแนะนำ = ใหม่ hashtable <ENCODEHINTTYPE วัตถุ> () ErrorCorrectionLevel.h); hints.put (encodehinttype.character_set, charset); hints.put (encodehinttype.margin, 1); bitmatrix bitmatrix = new Multiformatwriter () bitmatrix.getWidth (); int height = bitmatrix.getheight (); bufferedimage image = bufferedimage ใหม่ (ความกว้าง, ความสูง, bufferedimage.type_int_rgb); สำหรับ (int x = 0; x <width; x ++) {สำหรับ (int y = 0; y <ความสูง; y ++) {image.setrgb (x, y, bitmatrix.get (x, y)? 0xff000000: 0xfffffff);}} if (imgpath == null || " Needcompress); return image;}/** * แทรกโลโก้ * * @param แหล่งที่มา * qr รหัสรูปภาพ * @param imgpath * ที่อยู่รูปภาพโลโก้ * @param needcompress * ไม่ว่าจะบีบอัด * @throws ยกเว้น */โมฆะคงที่ {system.err.println (""+imgpath+"ไฟล์นี้ไม่มีอยู่!"); return;} image src = imageio.read (ไฟล์ใหม่ (imgpath)); int width = src.getwidth (null); int height = src.getheight (null) ความสูง) {ความสูง = ความสูง;} ภาพ = src.getScaledInstance (ความกว้าง, ความสูง, image.scale_smooth); bufferedimage tag = bufferedimage ใหม่ (ความกว้าง, ความสูง, bufferedimage.type_int_rgb); graphics g = tag.getGraphics (); // วาดกราฟที่ลดลง g.dispose (); src = image;} // แทรก logographics2d กราฟ = source.creategraphics (); int x = (qrcode_size - ความกว้าง)/ 2; int y = (qrcode_size - ความสูง)/ 2; กราฟ Roundrectangle2d.float (x, y, ความกว้าง, ความกว้าง, 6, 6); graph.setstroke (basicstroke ใหม่ (3f)); graph.draw (รูปร่าง); graph.dispose ();}/** * สร้างรหัส QR * @loga โลโก้ * @throws Exception */public static void encode (เนื้อหาสตริง, imgpath สตริง, สตริง destpath, boolean needcompress) โยนข้อยกเว้น {bufferedimage image = qrcodeutil.createImage (เนื้อหา, imgpath, needcompress); mkdirs (destpath); format_name, ไฟล์ใหม่ (destpath+"/"+ไฟล์));}/*** เมื่อไม่มีโฟลเดอร์ mkdir จะสร้างไดเรกทอรีหลายชั้นโดยอัตโนมัติซึ่งแตกต่างจาก mkdir (หากไม่ได้มีไดเรกทอรีหลักจะมีข้อยกเว้น) * @author lanyuan * อีเมล: [email protected] * @date 2013-12-11 10:16:36 am * @param destpath store directory */public static mkdirs // เมื่อไม่มีโฟลเดอร์ MKDIRS จะสร้างไดเรกทอรีหลายชั้นโดยอัตโนมัติซึ่งแตกต่างจาก MKDIR (mkdir จะโยนข้อยกเว้นหากไม่ได้มีไดเรกทอรีหลัก) ถ้า (! file.exists () &&! file.isdirectory ()) {file.mkdirs ();}}/** * สร้างรหัส QR * @paramated * * * * ** string imgpath, สตริง destpath) พ่นข้อยกเว้น {qrcodeutil.encode (เนื้อหา, imgpath, destpath, false);}/** * สร้างรหัส QR * * @param เนื้อหา * เนื้อหา * @param destpath * ที่อยู่ที่เก็บ * @param needcompress * ไม่ว่าจะบีบอัดโลโก้ * @throws {qrcodeutil.encode (เนื้อหา, null, destpath, needcompress);}/*** สร้างรหัส QR** @param เนื้อหา* เนื้อหา* @param destpath* ที่อยู่ที่เก็บของ* @throws Exception*/public Static encode โลโก้) * * @param เนื้อหา * เนื้อหา * @param imgpath * ที่อยู่โลโก้ * @param output * idput stream * @param needcompress * ไม่ว่าจะบีบอัดโลโก้ * @throws ยกเว้น */โมฆะคงที่สาธารณะ needcompress); imageio.write (image, format_name, output);}/*** สร้างรหัส QR** @param เนื้อหา* เนื้อหา* @param output* umput stream* @throws Exception*/void encode แบบคงที่ ไฟล์ * qr รหัสภาพ * @return * @throws Exception */String String สาธารณะ Decode (ไฟล์ไฟล์) พ่นข้อยกเว้น {bufferedImage image; image = imageio.read (ไฟล์); ถ้า (image == null) {return null;} bufferedimageluminancesource HybridBinarizer (แหล่งที่มา)); ผลลัพธ์ผลลัพธ์; Hashtable <DecodeHintType, Object> คำใบ้ = hashtable ใหม่ <decodeHintType, Object> (); Hints.put (DecodeHintType.character_set, Charset); ผลลัพธ์ = new Multiformatreader () * การแยกวิเคราะห์รหัส QR * * @Param Path * QR รหัสที่อยู่ภาพ * @return * @throws Exception */Public String Decode (Path String Path) พ่นข้อยกเว้น {return qrcodeutil.decode (ไฟล์ใหม่ (พา ธ ));} โมฆะคงที่สาธารณะ "http://www.yihaomen.com"; qrcodeutil.encode (ข้อความ, "c: /me.jpg", "c:/บาร์โค้ด", จริง);}}สร้างรหัส QR โดยไม่มีโลโก้
รหัสโปรแกรมมีดังนี้:
โมฆะคงที่สาธารณะหลัก (สตริง [] args) โยนข้อยกเว้น {สตริงข้อความ = "http://www.dans88.com.cn"; qrcodeutil.encode (text, "", "d:/myworkdoc", true);}เรียกใช้วิธีการทดสอบนี้และรหัส QR ที่สร้างขึ้นไม่มีโลโก้สไตล์มีดังนี้:
หากคุณสนใจคุณสามารถสแกนด้วยโทรศัพท์ของคุณ
สร้างรหัส QR ด้วยโลโก้
โลโก้คุณสามารถใช้อวตารของคุณเองหรือรูปภาพที่คุณชอบและใช้รหัสต่อไปนี้รหัสโปรแกรมมีดังนี้:
โมฆะคงที่สาธารณะหลัก (สตริง [] args) โยนข้อยกเว้น {สตริงข้อความ = "http://www.dans88.com.cn"; qrcodeutil.encode (text, "d:/myworkdoc/my180.jpg", "d:/myworkdoc", จริง); -ความแตกต่างเพียงอย่างเดียวคือที่อยู่ของโลโก้ถูกระบุบนพื้นฐานก่อนหน้านี้และไฟล์รูปภาพของดิสก์ C ถูกใช้ที่นี่
สแกนด้วยโทรศัพท์มือถือของคุณและข้อความที่คุณต้องการปรากฏขึ้นสามารถปรากฏขึ้นได้ คลิกเพื่อเข้าสู่เว็บไซต์ของคุณเอง
ข้างต้นคือการใช้การออกแบบ Java เพื่อสร้างรหัส QR เพื่อนที่สนใจสามารถอ้างถึงได้ ขอบคุณสำหรับการสนับสนุนเว็บไซต์นี้!