1 、 جرة : zxing-core-33.3.0.jar http://mvnrepository.com/artifact/com.google.zxing
zxing-javase-3.3.0.jar http://mvnrepository.com/artifact/com.google.zxing/javase
bufferedimageluminancesource.java
package com.webos.util ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.geom.affinetransform ؛ استيراد java.awt.image.bufferedImage ؛ import com.google.zxing.luminancesource ؛ public class bufferedimageluminancyanationsource extendes غادر int النهائي الخاص ؛ قمة int النهائية الخاصة ؛ public bufferedimageluminancesource (صورة bufferedImage) {هذا (الصورة ، 0 ، 0 ، image.getWidth () ، image.getheight ()) ؛ } public bufferedimageluminancesource (صورة bufferedImage ، int اليسار ، int top ، عرض int ، int leight) {super (width ، height) ؛ int sourceWidth = image.getWidth () ؛ int souteheight = image.getheight () ؛ if (Left + Width> sourceWidth || Top + height> sourceHight) {رمي جديد غير alficalArgumentException ("لا يتناسب مستطيل المحاصيل داخل بيانات الصورة.") ؛ } لـ (int y = top ؛ y <top+height ؛ y ++) {for (int x = left ؛ x <left+width ؛ x ++) {if ((image.getrgb (x ، y) & 0xff000000) == 0) {image.setrgb (x ، y ، 0xffffff) ؛ // = white}}} this.image = new BufferEdImage (sourceWidth ، sourceHight ، bufferedImage.type_byte_gray) ؛ this.image.getGraphics (). drawImage (Image ، 0 ، 0 ، null) ؛ this.left = اليسار ؛ this.top = top ؛ } public byte [] getRow (int y ، byte [] row) {if (y <0 || y> = getheight ()) {رمي new alficalArgumentException ("الصف المطلوب خارج الصورة:" + y) ؛ } int width = getWidth () ؛ if (row == null || row.length <width) {row = new byte [width] ؛ } image.getRaster (). getDataelements (يسار ، أعلى + y ، العرض ، 1 ، الصف) ؛ صف العودة } public byte [] getMatrix () {int width = getWidth () ؛ ارتفاع int = getheight () ؛ int مساحة = العرض * الارتفاع ؛ Byte [] Matrix = New Byte [Area] ؛ Image.getRaster (). getDataelements (يسار ، أعلى ، العرض ، الارتفاع ، المصفوفة) ؛ مصفوفة العودة ؛ } iScropSupported () {return true ؛ ) } isrotatesupported () {return true ؛ } luminancesource rotatecounterclocwise () {int sourceWidth = image.getWidth () ؛ int souteheight = image.getheight () ؛ AffinetRans Transform = Affinetransform جديد (0.0 ، -1.0 ، 1.0 ، 0.0 ، 0.0 ، SourceWidth) ؛ bufferedImage rotatedImage = جديد bufferedImage (sourceHight ، sourceWidth ، bufferedImage.type_byte_gray) ؛ graphics2d g = rotatedimage.creategraphics () ؛ G.DrawImage (الصورة ، التحويل ، فارغة) ؛ G.Dispose () ؛ عرض int = getWidth () ؛ إرجاع جديد BufferedImageluminancesource (RotatedImage ، Top ، SourceWidth - (Left + Width) ، Getheight () ، العرض) ؛ }}qrcodeutil.java
package com.webos.util ؛ استيراد java.awt.basicsstroke ؛ استيراد java.awt.graphics ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.image ؛ استيراد java.awt.shape ؛ java.io.file ؛ import java.io.outputstream ؛ import java.util.hashtable ؛ import java.util.random ؛ import javax.imageio com.google.zxing.encodehinttype ؛ import com.google.zxing.multiformatreader ؛ import com.google.zxing.multiformatwriter ؛ import com.google.zxing.common.bitmatrix ؛ import com.google.common.hyherbridbinizer ؛ com.google.zxing.qrcode.decoder.errorcorrectionLevel ؛/*** classname: qrcodeutil* description: 二维码编码* @Author liUy* date 2016 年 7月 9 日 日 3:03:24*/class public qrcodeutil {// // 保存的二维码格式 private Static Final String Format_Name = "JPG" ؛ // 二维码尺寸 Final Static Final int QRCode_Size = 800 ؛ // logo 宽度 宽度 static final logo_width = 80 ؛ // logo 高度 private static final int logo_height = 80 ؛ / ** * title: createImage * description: 将二维码内容创建到 Image 流 * param content 二维码内容 * param imgpath logo 图片地址 * param needcompress 是否压缩 logo 图片大小 * return * @throws استثناء 参数说明 * return bufferedImage 返回类型 * athrows */ private bufferedimage createImage (strepr strepath) hashtable <EncodeHintType ، Object> nints = new hashtable <EncodeHintType ، Object> () ؛ hints.put (EncodeHintType.error_correction ، errorcorrectionlevel.h) ؛ Hints.put (Encodehinttype.character_set ، charset) ؛ Hints.put (EncodeHintType.Margin ، 1) ؛ bitmatrix bitmatrix = جديد متعدد الأشكال () int width = bitmatrix.getWidth () ؛ ارتفاع int = bitmatrix.getheight () ؛ صورة bufferedImage = جديد bufferedImage (العرض ، الارتفاع ، bufferedImage.type_int_rgb) ؛ لـ (int x = 0 ؛ x <width ؛ x ++) {for (int y = 0 ؛ y <height ؛ y ++) {image.setrgb (x ، y ، bitmatrix.get (x ، y)؟ 0xff000000: 0xffffffff) ؛ }} if (logopath == null || "" .equals (logopath)) {return image ؛ } // 插入 logo qrcodeutil.insertimage (الصورة ، logopath ، needcompress) ؛ إرجاع الصورة ؛ } / ** * title: insertimage * description: 将 logo 插入到二维码中 * param المصدر 二维码 الصورة 流 * param imgpath logo 地址 * param needcompress 是否压缩大小 * therws 参数说明 * 参数说明 * 参数说明ملف (logopath) ؛ if (! file.exists ()) {system.err.println ("" + logopath + "该文件不存在!") ؛ يعود؛ } صورة src = imageio.read (ملف جديد (logopath)) ؛ عرض int = src.getWidth (null) ؛ ارتفاع int = src.getheight (null) ؛ if (needcompress) {// 压缩 logo if (width> logo_width) {width = logo_width ؛ } if (height> logo_height) {height = logo_height ؛ } صورة صورة = src.getScaledInstance (العرض ، الارتفاع ، image.scale_smooth) ؛ علامة bufferedImage = جديد bufferedImage (العرض ، الارتفاع ، bufferedImage.type_int_rgb) ؛ الرسومات g = tag.getGraphics () ؛ G.DrawImage (صورة ، 0 ، 0 ، فارغة) ؛ // 绘制缩小后的图 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 (New BasicStroke (3F)) ؛ graph.draw (الشكل) ؛ graph.dispose () ؛ } / ** * title: mkdirs * description: 创建文件夹 * param destpath 文件夹地址 * return void 返回类型 * @throws * / private static boolean mkdirs (string destpath) {file file = new file (destpath) ؛ // 当文件夹不存在时 , mkdirs 会自动创建多层目录 , 区别于 mkdir. (mkdir 如果父目录不存在则会抛出异常) if (! file.exists () &&! file.isdirectory ()) {file.mkdirs () ؛ العودة صحيح. } إرجاع خطأ ؛ } / ** * title: encode * description: 生成二维码 * param content 二维码内容 * param imgpath logo 图片地址 * param destpath 目标保存地址 * param needcompress 是否压缩 logo 图片大小 * throos استثناء 参数说明 * reghrows 返回类型 * @throws * / private void encode ( صورة bufferedImage = qrcodeutil.createImage (المحتوى ، logopath ، Needcompress) ؛ if (mkdirs (destPath)) {string file = new Random (). nextInt (99999999) + ".jpg" ؛ imageio.write (Image ، Format_Name ، ملف جديد (DestPath + "/" + file)) ؛ }} / ** * title: encode * description: 生成二维码 * param content 二维码内容 * param destpath 目标保存地址 * @throws استثناء 参数说明 * return void 返回类型 * @throws * / public static void Encode (content string ، string) reases {qrcodeutil.encode (content ، null ، destpath ، false) ؛ } / ** * title: encode * description: 生成二维码 * param content 二维码内容 * param imgpath logo 图片地址 * param output 输出流 * @param needcompress 是否压缩 logo 图片大小 * thrats exception 参数说明 参数说明 * return 返回类型 throws * / public static void encode (string logopath ، outputester outputing ، booan upergr strows 返回类型 boon صورة bufferedImage = qrcodeutil.createImage (المحتوى ، logopath ، Needcompress) ؛ imageio.write (الصورة ، format_name ، الإخراج) ؛ } / ** * title: encode * description: 生成二维码 * param content 二维码内容 * param output 输出流 * @throws استثناء 参数说明 * return void 返回类型 * @throws * / public static void encode (محتوى السلسلة ، إخراج outputStream) استثمار {qrcodeutil.encode (content ، output ، failure) ؛ } / ** * title: decode * description: 对二维码解码 * param file 文件对象 * regurn 解码后的二维码内容字符串 * @throws استثناء 参数说明 * regurn 返回类型 * throws * / private static string decode (ملف الملف) يلقي استثناء {pufferedImage صورة ؛ Image = imageio.read (file) ؛ if (image == null) {return null ؛ } مصدر bufferedimageluminancesource = جديد bufferedimageluminancesource (Image) ؛ binarybitmap bitmap = جديد binarybitmap (Hybridbinarizer جديد (المصدر)) ؛ hashtable <decodehinttype ، string> تلميحات = new hashtable <decodehinttype ، string> () ؛ hints.put (decodehinttype.character_set ، charset) ؛ إرجاع جديد multiformatreader () decode (صورة نقطية ، تلميحات) .getText () ؛ } / ** * title: decode * description: 对二维码解码 * param path 文件路径 * regurn * @throws استثناء 参数说明 * @regr 返回类型 * @throws * / public static string decode (مسار السلسلة) يلقي استثناء {return qrcodeutil.decode (ملف جديد) ؛ }}qrcodeservlet.java
package com.webos.servlet ؛ import java.io.ioException ؛ import javax.servlet.servletexception ؛ import javax.servlet.annotation.webservlet ؛ import javax.servlet.http.httpservlet ؛ import javax.servlet.http.httplet javax.servlet.http.httpservletresponse ؛ استيراد com.webos.util.qrcodeutil ؛/ * * servlet فئة التنفيذ qrcodeservlet */@webservlet ("/qrcodeservlet") فئة QRCodeservlet public {private static finial = 1L ؛ / * * seee httpservlet#httpservlet () */ public qrcodeservlet () {super () ؛ }/ * * seee httpservlet#doget (طلب httpservletrequest ، httpservletresponse * استجابة) */محمية void doget (httpservletrequest طلب ، httpservletrespons System.CurrentTimeMillis () ؛ qrcodeutil.encode (text ، response.getOutputStream ()) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }} / * * see @httpservlet#dopost (httpservletrequest request ، httpservletresponse * response) * / void dopost dopost (httpservletrequest request ، httpservletresponse) رمي servlexception ، ioexception }}以上就是本文的全部内容 , 希望本文的内容对大家的学习或者工作能带来一定的帮助 , 同时也希望多多支持武林网!