การใช้ ZXING เพื่อใช้รูปภาพฝังตัวของเครื่องกำเนิดรหัส QR มีค่าอ้างอิงบางอย่างดังนี้:
แนวคิดพื้นฐานคือการใช้ภาพรหัส QR ที่สร้างขึ้นโดย zxing ก่อนจากนั้นอ่านภาพใส่ไอคอนเข้าไปในนั้นจากนั้นส่งออกภาพทั้งหมด
ในโครงการล่าสุดเราต้องสร้างรหัส QR หลังจากค้นหาตัวอย่างบางส่วนเราได้บรรลุผลสุดท้าย รหัส QR สามารถสร้างรูปแบบภาพ (JPG ฯลฯ ) หรือแสดงบนหน้าเว็บ บทความนี้ถูกบันทึกไว้เท่านั้นและมีความคล้ายคลึงกันมากมายดังนั้นฉันอยากจะลืมมัน - - -
หมายเหตุ: สำหรับเครื่องมือที่ต้องใช้บรรจุภัณฑ์ ZXING กระบวนการทั่วไปคือการอ่านรูปภาพที่ฝังตัวแปลงเนื้อหาเป็นรหัส QR ฝังรูปภาพไว้ในรหัส QR และสร้างรูปภาพ
นี่คือรหัสที่สมบูรณ์:
นำเข้า java.awt.basicstroke; นำเข้า java.awt.color; นำเข้า java.awt.graphics2d; นำเข้า java.awt.image ภาพ java.awt.shape; นำเข้า java.awt.geom.affinetransform; java.awt.image.bufferedimage; นำเข้า java.io.file; นำเข้า java.io.ioexception; นำเข้า java.util.hashmap นำเข้า java.util.map นำเข้า javax.imageio.imageio; com.google.zxing.multiformatwriter; นำเข้า com.google.zxing.writeRexception; นำเข้า com.google.zxing.common.bitmatrix; นำเข้า com.google.zxing.qrcode.decoder.errorcorrectionLevel; 0xFFFFFFFF; // ทั่วไปทั่วไปคงที่ int Image_width = 80; Image_half_width ส่วนตัวคงที่ส่วนตัว = image_width / 2; เฟรม int สุดท้ายคงที่ส่วนตัว frame_width = 2; // qr รหัสเขียนส่วนตัว multiformatwriter mutiwriter = new MultiformatWriter (); โมฆะคงที่สาธารณะหลัก (สตริง [] args) {ลอง {// bitmatrix bitmatrix = multiformatwriter.encode (เนื้อหา, barcodeformat.qr_code, 400, 400, คำแนะนำ); String Content = "134000000"; if (! imageio.write (image, "jpg", ไฟล์ใหม่ ("f: //2122.jpg")))) {โยน iOexception ใหม่ ("ไม่สามารถเขียนภาพของรูปแบบ"); } /** // สลับรหัสด้านบนที่นี่และใช้สตรีมเพื่ออ่านลงในหน้า OutputStream OS = Response.getOutputStream (); if (! imageio.write (รูปภาพ, "jpg", os)) {โยน ioexception ใหม่ ("ไม่สามารถเขียนภาพของรูปแบบ"); } **/} catch (writeRexception e) {e.printstacktrace ();} catch (ioexception e) {// todo catch blocke.printstacktrace ();}} bufferedimage tobufferedimage matrix.getheight (); bufferedimage image = new bufferedimage (ความกว้าง, ความสูง, bufferedimage.type_int_rgb); สำหรับ (int x = 0; x <width; x ++) {สำหรับ (int y = 0; y <ความสูง; y ++) bufferedImage genBarcode (เนื้อหาสตริง, ความกว้าง int, ความสูง int, สตริง srcimagepath) พ่น writeRexception, ioexception {// อ่านแหล่งที่มาของภาพ scaleimage = มาตราส่วน (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, ERRORCORTECTLEVEL.H); // ข้อผิดพลาดระดับระดับคำแนะนำ (ENCODEHINTTYPE.MARGIN, 1); // ตั้งค่าความกว้างของพื้นที่ว่างของเส้นขอบด้านนอกของรหัส QR // สร้างรหัส QR Bitmatrix matrix = mutiwriter.encode (เนื้อหา, barcodeformat.qr_code, ความกว้าง, ความสูง, คำใบ้); // แปลงเมทริกซ์ 2D เป็นอาร์เรย์พิกเซลหนึ่งมิติ 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> halfw - image_half_width && x <halfw+image_half_width && image_half_width) {pixels [y * width + x] = srcpixels [x - halfw + image_half_width] [y - halfh + image_half_width]; } // สร้างเส้นขอบรอบ ๆ ภาพอื่น ๆ ถ้า ((x> halfw - image_half_width - frame_width && x <halfw - image_half_width + frame_width && y> halfh - image_half_width - frame_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 <halfh - image_half_width + frame_width) || + image_half_width + frame_width)) {พิกเซล [y * width + x] = 0xffffffff; } else {// ที่นี่คุณสามารถแก้ไขสีของรหัส QR และคุณสามารถกำหนดสีของรหัส QR และพื้นหลังตามลำดับ พิกเซล [y * width + x] = matrix.get (x, y)? 0xff000000: 0xffffffff; }}} bufferedImage image = new bufferedImage (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); image.getraster (). setDataelements (0, 0, ความกว้าง, ความสูง, พิกเซล); ภาพกลับ; } /** * ปรับขนาดภาพต้นฉบับที่ผ่านด้วยความสูงและความกว้างเพื่อสร้างไอคอนที่ตรงตามข้อกำหนด * * @param srcimageFile * ที่อยู่ไฟล์แหล่งที่มา * @param ความสูง * ความสูงเป้าหมาย * @param ความกว้าง * ความกว้างเป้าหมาย * @param hasfiller * จำเป็นต้องเติม เท็จไม่ได้เติม * @throws ioexception */ สเกลบัฟเฟอร์แบบคงที่ส่วนตัว (สตริง srcimagefile, ความสูง int, ความกว้าง int, boolean 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.pink); graphic.fillrect (10, 10, ความกว้าง, ความสูง); graphic.drawrect (100, 360, ความกว้าง, ความสูง); if (width == destimage.getWidth (null)) {graphic.drawimage (destimage, 0, (ความสูง - destimage.getheight (null)) / 2, destimage.getwidth (null), destimage. -getheight (null), color.white, null) รูปร่างรูปร่าง = ใหม่ RoundRectangle2d.float (0, (ความสูง - destimage.getheight (null)) / 2, ความกว้าง, ความกว้าง, 20, 20); Graphic.setstroke (basicstroke ใหม่ (5f)); graphic.draw (รูปร่าง); } else {graphic.drawimage (destimage, (width - destimage.getWidth (null)) / 2, 0, destimage.getWidth (null), destimage.getheight (null), color.white, null); รูปร่างรูปร่าง = ใหม่ RoundRectangle2d.float ((ความกว้าง - destimage.getWidth (null)) / 2, 0, ความกว้าง, ความกว้าง, 20, 20); Graphic.setstroke (basicstroke ใหม่ (5f)); graphic.draw (รูปร่าง); } graphic.dispose (); destimage = image; } return (bufferedImage) destimage; -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น