มีแพ็คเกจการพัฒนามากมายสำหรับการสร้างรหัส QR Java ตัวอย่างเช่น ZXING ได้รับการพัฒนาโดย Google ที่นี่ฉันใช้แพ็คเกจการพัฒนาของ ZXING เพื่อนำไปใช้ ในหลายโครงการเราจำเป็นต้องสร้างรหัส QR แบบไดนามิกเพื่อให้พวกเขาแก่ผู้ใช้เพื่อให้ผู้คนจำนวนมากสามารถสัมผัสกับแอปพลิเคชันของพวกเขาได้ดีผ่านรหัส QR
รหัสถูกโพสต์ด้านล่างและได้รับการทดสอบแล้วและคุณสามารถคัดลอกรหัสโดยตรงเพื่อใช้:
ได้ผลลัพธ์ในที่สุด:
Java สร้างรหัส QR
รหัสมีดังนี้:
นำเข้า java.awt.color; นำเข้า java.awt.graphics2d นำเข้า java.awt.image; นำเข้า java.awt.geom.affinetransform; นำเข้า java.awt.image.affinetransformop; นำเข้า Java.Awt.Image.BufferedImage; java.io.unsupportencodingexception; นำเข้า java.util.hashmap; นำเข้า java.util.map นำเข้า javax.imageio.imageio; นำเข้า com.google.zxing.barcodeformat; นำเข้า com.google.zxing.encodehinttype; นำเข้า com.google.zxing.multiformatwriter; นำเข้า com.google.zxing.writexception; นำเข้า com.google com.google.zxing.qrcode.decoder.errorCorrectionLevel; ชั้นเรียนสาธารณะ Matrixtoimagewriter {ส่วนตัวคงที่ int สุดท้าย image_width = 100; Image_height int สุดท้ายส่วนตัวคงที่ = 100; Image_half_width ส่วนตัวคงที่ส่วนตัว = image_width / 2; เฟรม int สุดท้ายคงที่ส่วนตัว frame_width = 2; MultiformatWriter MultiformatWriter ส่วนตัว = new MultiformatWriter (); โมฆะคงที่สาธารณะเข้ารหัส (เนื้อหาสตริง, ความกว้าง int, ความสูง int, สตริง srcimagepath, สตริง destimagepath) {ลอง {imageio.write (genBarcode (เนื้อหา, ความกว้าง, ความสูง, srcimagepath), "jpg", ไฟล์ใหม่ (destimagepath); } catch (ioexception e) {e.printstacktrace (); } catch (writeRexception e) {e.printStackTrace (); }} ส่วนตัว bufferedImage genBarcode (เนื้อหาสตริง, ความกว้าง int, ความสูง int, สตริง srcimagepath) พ่น writeRexception, ioexception {bufferedimage scaleimage = scale (srcimagepath, image_width, image_height, true); int [] [] srcpixels = new int [image_width] [image_height]; สำหรับ (int i = 0; i <scaleImage.getWidth (); i ++) {สำหรับ (int j = 0; j <scaleimage.getheight (); j ++) {srcpixels [i] [j] = scaleimage.getRgb (i, j); }} แผนที่ <ENCODEHINTTYPE, OBJECT> HINT = ใหม่ HASHMAP <ENCODEHINTTYPE, OBJECT> (); คำใบ้ (encodeHintType.character_set, "UTF-8"); คำใบ้ (encodeHintType.error_Correction, ErrorCorrectionLevel.h); // สร้างรหัส QR Bitmatrix matrix = mutiwriter.encode (เนื้อหา, barcodeformat.qr_code, ความกว้าง, ความสูง, คำใบ้); // แปลงเมทริกซ์สองมิติเป็นอาร์เรย์พิกเซลหนึ่งมิติ int halfw = matrix.getWidth () / 2; int halfh = matrix.getheight () / 2; int [] พิกเซล = new int [ความกว้าง * ความสูง]; สำหรับ (int y = 0; y <matrix.getheight (); y ++) {สำหรับ (int x = 0; x <matrix.getWidth (); x ++) {// สีในมุมซ้ายบนปรับช่วงสีและสีตามความต้องการของคุณ (x> 0 && x <170 && int colorint = color.getRgb (); พิกเซล [y * width + x] = matrix.get (x, y)? Colorint: 16777215; } // อ่านรูปภาพอื่นถ้า (x> halfw - image_half_width && x <halfw + image_half_width && y> halfh - image_half_width && y <halfh + image_half_width) image_half_width]; } else if ((x> halfw - image_half_width - frame_width && x <halfw - image_half_width + frame_width && y> halfh - image_half_width - frame_width && y <halfh + image_half_width x <halfw + image_half_width + frame_width && y> halfw - image_half_width - frame_width && y <halfh + image_half_width + frame_width) || image_half_width - frame_width && y <halfh - image_half_width + frame_width) || (x> halfw - image_half_width - frame_width && y <halfh image_half_width + frame_width && y> halfh + image_half_width - frame_width && y <halfh + image_half_width + frame_width)) {พิกเซล [y * width + x] = 0xffffffff; // สร้างเส้นขอบรอบ ๆ รูปภาพ} else {// qr รหัสสี int num1 = (int) (50 - (50.0 - 13.0) / matrix.getheight () * (y + 1)); int num2 = (int) (165 - (165.0 - 72.0) / matrix.getheight () * (y + 1)); int num3 = (int) (162 - (162.0 - 107.0) / matrix.getheight () * (y + 1)); สีสี = สีใหม่ (num1, num2, num3); int colorint = color.getRgb (); // ที่นี่คุณสามารถแก้ไขสีรหัส QR และคุณสามารถกำหนดรหัส QR และสีพื้นหลังตามลำดับ พิกเซล [y * width + x] = matrix.get (x, y)? Colorint: 16777215; // 0x00000: 0xffffff}}} bufferedImage image = new bufferedImage (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); image.getraster (). setDataelements (0, 0, ความกว้าง, ความสูง, พิกเซล); ภาพกลับ; } สเกล bufferedImage ส่วนตัว (สตริง srcimagefile, ความสูง int, ความกว้าง int, บูลีน hasfiller) พ่น IOException {อัตราส่วนคู่ = 0.0; // ไฟล์สเกลไฟล์ = ไฟล์ใหม่ (srcimageFile); bufferedImage srcimage = imageio.read (ไฟล์); ภาพ destimage = srcimage.getScaledInstance (ความกว้าง, ความสูง, bufferedimage.scale_smooth); // คำนวณสเกลถ้า ((srcimage.getheight ()> ความสูง) || (srcimage.getWidth ()> ความกว้าง)) {ถ้า (srcimage.getheight ()> srcimage.getWidth ()) } else {ratio = (จำนวนเต็มใหม่ (ความกว้าง)). doublevalue () / srcimage.getWidth (); } affineTransformop op = new affineTransformop (affineTransform.getScaleInstance (อัตราส่วน, อัตราส่วน), null); destimage = op.filter (srcimage, null); } if (hasfiller) {// การเติม bufferedImage image = bufferedImage ใหม่ (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); graphics2d graphic = image.createGraphics (); graphic.setColor (color.white); graphic.fillrect (0, 0, ความกว้าง, ความสูง); if (width == destimage.getWidth (NULL)) กราฟิก. DRAWIMAGE (DESTIMAGE, 0, (ความสูง - DESTIMAGE.GETHEIGHT (NULL)) / 2, DESTIMAGE.GETWIDTH (NULL), DESTIMAGE.GETHEIGHT (NULL) graphic.drawimage (Destimage, (Width - Destimage.getWidth (NULL)) / 2, 0, Destimage.getWidth (NULL), Destimage.Getheight (NULL), color.white, Null); graphic.dispose (); destimage = image; } return (bufferedImage) destimage; } โมฆะคงที่สาธารณะหลัก (String [] args) พ่นไม่ได้รับการรับรองความเป็นไปได้ {// ในทางกลับกันเนื้อหา (ไม่รองรับในภาษาจีน), ความกว้าง, ความยาว, เส้นทางไอคอนกลาง, เส้นทางการจัดเก็บ matrixtoimagewriter.encode ("http://www.baidu.com/", 512, 512, 512, 512 "D: //2013-01.jpg"); -คุณสามารถอ้างถึงหัวข้อ: รหัส Java QR สำหรับการเรียนรู้
ข้างต้นเป็นเรื่องเกี่ยวกับบทความนี้ช่วยให้คุณออกแบบรหัส QR ของคุณเอง