1、 jar 包: zxing-core-3.3.0.jar http://mvnrepository.com/artifact/com.google.zxing/core
zxing-javase-3.30.jar http://mvnrepository.com/artifact/com.google.zxing/javase
Bufferedimageluminances.java
แพ็คเกจ com.webos.util; นำเข้า java.awt.graphics2d นำเข้า java.awt.geom.affinetransform; นำเข้า java.awt.image.bufferedimage; นำเข้า com.google.zxing.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; } ไบต์สาธารณะ [] 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, แถว); แถวกลับ; } ไบต์สาธารณะ [] getMatrix () {int width = getWidth (); ความสูง int = getheight (); พื้นที่ int = ความกว้าง * ความสูง; ไบต์ [] เมทริกซ์ = ไบต์ใหม่ [พื้นที่]; image.getraster (). getDataelements (ซ้าย, บน, ความกว้าง, ความสูง, เมทริกซ์); กลับมาเมทริกซ์; } บูลีนสาธารณะ iscropsupported () {return true; } การปลูกพืชสาธารณะ luminancesource (int ซ้าย, int top, ความกว้าง int, ความสูง int) {ส่งคืน bufferedimageLuminancesource ใหม่ (ภาพ, this.left + ซ้าย, this.top + ด้านบน, ความกว้าง, ความสูง); } บูลีนสาธารณะ isrotatesupported () {return true; } 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 (), ความกว้าง); -qrcodeutil.java
แพ็คเกจ com.webos.util; นำเข้า java.awt.basicstroke; นำเข้า java.awt.graphics; นำเข้า java.awt.graphics2d นำเข้า java.awt.image นำเข้า Java.awt.shape; นำเข้า Java.awt.geom.roundrectangle2d 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.common.bitmatrix; com.google.zxing.qrcode.decoder.errorcorrectionLevel;/*** @classname: qrcodeutil* @description: 二维码编码* @author liuy* @date 2016 年7月 9 日 3:03:24*/คลาสสาธารณะ // 保存的二维码格式สตริงสุดท้ายคงที่แบบคงที่ private format_name = "jpg"; // 二维码尺寸 int สุดท้ายคงที่ int qrcode_size = 800; // โลโก้宽度โลโก้ int สุดท้ายของส่วนตัวคงที่ = 80; // โลโก้高度โลโก้ int สุดท้ายส่วนตัวคงที่ logo_height = 80; / ** * @title: createimage * @description: 将二维码内容创建到ภาพ流 * @param เนื้อหา二维码内容 * @param imgpath โลโก้图片地址 * @param needcompress 是否压缩โลโก้图片大小 * @return * @throws ยกเว้น参数说明 * @return bufferiMage 返回类型 * @throws * Hashtable <ENCODEHINTTYPE, OBJECT> HINTS = ใหม่ HASHTABLE <ENCODEHINTTYPE, OBJECT> (); Hints.put (encodeHintType.error_Correction, ErrorCorrectionLevel.h); คำแนะนำ (encodehinttype.character_set, charset); คำแนะนำ (encodehinttype.margin, 1); bitmatrix bitmatrix = new MultiformatWriter (). ENCODE (เนื้อหา, BARCODEFORMAT.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, คำแนะนำ); ความกว้าง int = bitmatrix.getWidth (); ความสูง int = bitmatrix.getheight (); BufferedImage Image = New 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: 0xffffffff); }} ถ้า (logopath == null || "" .equals (logopath)) {return image; } // 插入โลโก้ qrcodeutil.insertimage (ภาพ, logopath, needcompress); ภาพกลับ; } / ** * @title: insertImage * @description: 将โลโก้插入到二维码中 * @param แหล่งที่มา二维码ภาพ流 * @param imgpath โลโก้地址 * @param needcompress 是否压缩大小 * @throws ยกเว้น参数说明 * @return void 返回类型 * @throws * ไฟล์ (logopath); if (! file.exists ()) {system.err.println ("" + logopath + "该文件不存在!"); กลับ; } image src = imageio.read (ไฟล์ใหม่ (logopath)); ความกว้าง int = src.getWidth (null); ความสูง int = src.getheight (null); if (needcompress) {// 压缩โลโก้ if (width> logo_width) {width = logo_width; } if (ความสูง> logo_height) {ความสูง = logo_height; } image image = src.getScaledInstance (ความกว้าง, ความสูง, image.scale_smooth); bufferedImage tag = new bufferedImage (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); กราฟิก g = tag.getGraphics (); G.DrawImage (ภาพ, 0, 0, null); // 绘制缩小后的图 G.Dispose (); src = ภาพ; } // 插入 logo graphics2d graph = source.createGraphics (); int x = (qrcode_size - ความกว้าง) / 2; int y = (qrcode_size - ความสูง) / 2; graph.drawimage (src, x, y, ความกว้าง, ความสูง, null); รูปร่างรูปร่าง = ใหม่ RoundRectangle2d.float (x, y, ความกว้าง, ความกว้าง, 6, 6); graph.setstroke (basicstroke ใหม่ (3f)); graph.draw (รูปร่าง); graph.dispose (); } / ** * @title: mkdirs * @description: 创建文件夹 * @param destpath 文件夹地址 * @return เป็นโมฆะ返回类型 * @throws * / บูลีนคงที่ส่วนตัว mkdirs (สตริง destpath) {ไฟล์ไฟล์ = ไฟล์ใหม่ (destpath); // 当文件夹不存在时, mkdirs 会自动创建多层目录, 区别于 mkdir. (mkdir 如果父目录不存在则会抛出异常) ถ้า (! file.exists () &&! file.isdirectory ()) {file.mkdirs (); กลับมาจริง; } return false; } / ** * @title: เข้ารหัส * @description: 生成二维码 * @param เนื้อหา二维码内容 * @param imgpath โลโก้图片地址 * @param destpath 目标保存地址 * @param needcompress 是否压缩 logo 图片大小 * @throws ยกเว้น参数说明 * @return void 返回类型 * @throws * image = qrcodeutil.createImage (เนื้อหา, logopath, needcompress); if (mkdirs (destpath)) {string file = new random (). nextint (99999999) + ".jpg"; imageio.write (image, format_name, ไฟล์ใหม่ (destpath + "/" + ไฟล์)); }} / ** * @title: เข้ารหัส * @description: 生成二维码 * @param เนื้อหา二维码内容 * @param destpath 目标保存地址 * @throws ยกเว้น参数说明 * @return เป็นโมฆะ返回类型 * @throws * / public Static encode (เนื้อหาสตริง } / ** * @title: เข้ารหัส * @description: 生成二维码 * @param เนื้อหา二维码内容 * @param imgpath โลโก้图片地址 * @param output 输出流 * @param needcompress 是否压缩 logo 图片大小 * @throws exception 参数说明 * @return void 返回类型 * @throws * bufferedImage image = qrcodeutil.createImage (เนื้อหา, logopath, needcompress); imageio.write (image, format_name, output); } / ** * @title: เข้ารหัส * @description: 生成二维码 * @param เนื้อหา二维码内容 * @param output 输出流 * @throws Exception 参数说明 * @return เป็นโมฆะ返回类型 * @throws * / void encode คงที่ } / ** * @title: decode * @description: 对二维码解码 * @param ไฟล์文件对象 * @return 解码后的二维码内容字符串 * @throws Exception 参数说明 * @return string 返回类型 * @throws * / สตริงคงที่ส่วนตัว (ไฟล์ไฟล์) โยนข้อยกเว้น {bufferedimage image; image = imageio.read (ไฟล์); if (image == null) {return null; } bufferedimageluminancesource source = ใหม่ bufferedimageluminancesource (ภาพ); BinaryBitMap Bitmap = ใหม่ BinaryBitMap (ใหม่ HybridBinarizer (แหล่งที่มา)); hashtable <decodehinttype, string> hints = new hashtable <decodeHintType, string> (); คำแนะนำ (decodehinttype.character_set, charset); ส่งคืน multiformatReader ใหม่ (). decode (บิตแมปคำแนะนำ) .getText (); } / ** * @title: decode * @description: 对二维码解码 * @param Path 文件路径 * @return * @throws Exception 参数说明 * @return String 返回类型 * @throws * / สตริงคงที่สาธารณะ -qrcodeservlet.java
แพ็คเกจ com.webos.servlet; นำเข้า java.io.ioexception; นำเข้า Javax.servlet.servletexception; นำเข้า Javax.servlet.annotation.webservlet; นำเข้า Javax.servlet.http.httpservlet; javax.servlet.http.httpservletResponse; นำเข้า com.webos.util.qrcodeutil;/ * * การใช้งาน servlet คลาส qrcodeservlet */@webservlet ("/qrcodeservlet") คลาสสาธารณะ qrcodeservlet / * * @SEE httpservlet#httpservlet () */ สาธารณะ qrcodeservlet () {super (); }/ * * @See httpservlet#doget (คำขอ httpservletrequest, httpservletResponse * การตอบสนอง) */การป้องกันโมฆะ doget (httpservletrequest คำขอ, httpservletresponse) โยน servletexception, ioexception System.currenttimemillis (); qrcodeutil.encode (text, response.getOutputStream ()); } catch (exception e) {e.printstacktrace (); }} / * * @See httpservlet#dopost (คำขอ httpservletrequest, httpservletResponse * การตอบสนอง) * / การป้องกันโมฆะ dopost (httpservletrequest คำขอ, httpservletResponse การตอบสนอง) -以上就是本文的全部内容, 希望本文的内容对大家的学习或者工作能带来一定的帮助, 同时也希望多多支持武林网!