1 、 pot 包: ZXing-core-3.3.0.jar http://mvnrepository.com/artifact/com.google.zxing/core
Zxing-javase-3.3.0.jar http://mvnrepository.com/artifact/com.google.zxing/javase
BufferedImageLuminancesource.java
package com.webos.util; import java.awt.graphics2d; import java.awt.geom.affinetransform; import java.awt.image.bufferedImage; import com.google.zxing.luminancesource; classe publique tamponedimageminancesource étend Luminancesource {private final buffredimage image; Final privé int Left; Top int privé final; Public BufferedImageLuminancesource (image de tampon) {this (image, 0, 0, image.getWidth (), image.getheight ()); } public BufferedImageLuMinancesource (image de tampon, int Left, int top, int, int hauteur) {super (largeur, hauteur); int sourcewidth = image.getWidth (); int sourceHeight = image.getheight (); if (gauche + largeur> sourcewidth || top + height> SourceHeight) {lancez un nouveau IllégalArgumentException ("Crop Rectangle ne correspond pas aux données de l'image."); } pour (int y = top; y <top + height; y ++) {for (int x = gauche; x <Left + width; x ++) {if ((image.getrgb (x, y) & 0xff000000) == 0) {image.setrgb (x, y, 0xffffffff); // = blanc}}} this.image = new BufferedImage (sourcewidth, sourceHeight, bufferedImage.type_byte_gray); this.image.getGraphics (). DrawImage (image, 0, 0, null); this.left = gauche; this.top = top; } byte public [] getrow (int y, byte [] row) {if (y <0 || y> = getheight ()) {lancez new illégalargumentException ("La ligne demandée est en dehors de l'image:" + y); } int width = getWidth (); if (row == null || row.length <width) {row = new byte [width]; } image.getRaster (). GetDataelements (gauche, haut + y, largeur, 1, ligne); ligne de retour; } byte public [] getMatrix () {int width = getWidth (); int hauteur = getheight (); INT Area = Largeur * Hauteur; octet [] matrice = nouveau octet [zone]; image.getRaster (). GetDataelements (gauche, haut, largeur, hauteur, matrice); matrice de retour; } public boolean isCropsupported () {return true; } public Luminancesource Crop (int Left, int top, int largeth, int height) {return new tampremedImageLuminancesource (image, this.left + gauche, this.top + top, largeur, hauteur); } public boolean isrotatesupported () {return true; } public luminancesource rotateCounterClockWise () {int sourcewidth = image.getWidth (); int sourceHeight = image.getheight (); AffineTransform Transform = Nouveau AffineTransform (0,0, -1,0, 1,0, 0,0, 0,0, SourceWidth); BufferedImage RotateDImage = new BufferedImage (SourceHeight, SourceWidth, BufferedImage.Type_Byte_Gray); Graphics2d g = rotateDImage.Creategraphics (); G.DrawImage (image, transform, null); g.dispose (); int width = getWidth (); Renvoie un nouveau bufferedImageLuminancesource (RotateDImage, haut, SourceWidth - (gauche + largeur), Getheight (), largeur); }}Qrcodeutil.java
Package com.webos.util; import java.awt.basicStroke; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.shape; import java.awt.geom.roundrectangle2d; import java.awt.image.buffremedlimage; java.io.file; import java.io.outputstream; import java.util.hashtable; import java.util.random; import javax.imageio.imageio; import com.google.zxing.barcodeformat; import com.google.zxing.binarybitmap; import com.google.zxing.deCodeHinttype; import com.google.zxing.deCodeHinttype; com.google.zxing.encodehintype; import com.google.zxing.multiformatReader; import com.google.zxing.multiformatwriter; import com.google.zxing.common.bitmatrix; import com.google.zxing.common.hybridbinarizer; importation; com.google.zxing.qrcode.decoder.errorCorrectionLevel; / *** @classname: qrcodeutil * @description: 二维码编码 * @author liuy * @date 2016 年 7月 9 日 下午 3:03:24 * * / public class qrcodeutil {// 设置二维码编码格式 private static final String final charset = "utf-8"; // 保存的二维码格式 STATIQUE STATIQUE PRIVATE FALM FORMAT_NAME = "JPG"; // 二维码尺寸 Final statique privé int qrcode_size = 800; // logo 宽度 private static final int 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 Exception 参数说明 * @return BufferedImage 返回类型 * @throws */ private static BufferedImage createImage(String content, String logoPath, boolean needCompress) throws Exception { HashTable <EncodeHintType, Object> hins = new HashTable <EncodeHintType, Object> (); hint.put (encodeHintType.Error_Correction, errorCorrectionLevel.h); hint.put (encodeHintType.Character_set, charset); Hint.put (EncodeHintType.Margin, 1); BitMatrix BitMatrix = new MultiformatWriter (). Encode (Contenu, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, HINTS); int width = bitMatrix.getWidth (); int height = bitmatrix.getheight (); BufferedImage Image = new BufferedImage (largeur, hauteur, bufferedImage.type_int_rgb); pour (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 (image, logopathe, NeedCompress); Image de retour; } / ** * @title: insertimage * @description: 将 logo 插入到二维码中 * @param source 二维码 Image 流 * @param imgpath logo 地址 * @param NeedCompress 是否压缩大小 * @throws exception 参数说明 * @return void 返回类型 * @throws * / private static insertimage (FiledImage Source, string logopat Fichier (logopathe); if (! file.exists ()) {System.err.println ("" + logopath + "该文件不存在!"); retour; } Image src = imageo.read (nouveau fichier (logopathe)); int width = src.getWidth (null); int height = src.GetHeight (null); if (NeedCompress) {// 压缩 logo if (width> logo_width) {width = logo_width; } if (height> logo_height) {height = logo_height; } Image image = src.getScaledInstance (largeur, hauteur, image.scale_smooth); BufferedImage Tag = new BufferedImage (largeur, hauteur, bufferedImage.type_int_rgb); Graphiques g = tag.getGraphics (); G.DrawImage (image, 0, 0, null); // 绘制缩小后的图 g.dispose (); src = image; } // 插入 logo graphics2d graph = source.creategraphics (); int x = (qrcode_size - largeur) / 2; int y = (qrcode_size - hauteur) / 2; Graph.DrawImage (Src, X, Y, largeur, hauteur, null); Forme de forme = nouveau RoundRectangle2d.float (x, y, largeur, largeur, 6, 6); Graph.SetStroke (New BasicStroke (3F)); graph.-draw (forme); graph.dispose (); } / ** * @title: mKDirs * @description: 创建文件夹 * @param destpath 文件夹地址 * @return void 返回类型 * @throws * / private static booléen mkDirs (String destpath) {file file = new File (destpath); // 当文件夹不存在时 , MKDIRS 会自动创建多层目录 , 区别于 MKDIR. (MkDir 如果父目录不存在则会抛出异常) if (! File.exists () &&! File.isdirectory ()) {file.mkDirs (); Retour Vrai; } return false; } / ** * @Title: Encode * @Description: 生成二维码 * @param Contenu 二维码内容 * @param Imgpath Logo 图片地址 * @param destpath 目标保存地址 * @param NeedCompress 是否压缩 Logo 图片大小 * @throws exception 参数说明 * @return void 返回类型 * @throws * / private static void Encode (contenu de chaîne, rigol logopath, String destpath, boofeaneanedBemberced) exception, string logopat BufferedImage Image = qrcodeUtil.CreateImage (contenu, logopathe, NeedCompress); if (mkDirs (destpath)) {string file = new random (). nextInt (99999999) + ".jpg"; Imageo.write (image, format_name, nouveau fichier (destpath + "/" + fichier)); }} / ** * @Title: Encode * @Description: 生成二维码 * @param Contenu 二维码内容 * @param destpath 目标保存地址 * @throws exception 参数说明 * @return void 返回类型 * @throws * / public static vioid Encode (String Content, string destpath) lance l'exception {qrcodeutil.encode (contenu, null, destpath, false); } / ** * @Title: Encode * @Description: 生成二维码 * @param Contenu 二维码内容 * @param IMGPATH LOGO 图片地址 * @Param Sortie 输出流 * @param NeedCompress 是否压缩 LOGO 图片大小 * @throws exception 参数说明 * @return 返回类型 * @throws * / public static Void Encode (contenu de chaîne d'exception de chaîne, l'exception de la chaîne d'exception de chaîne BufferedImage Image = qrcodeUtil.CreateImage (contenu, logopathe, NeedCompress); Imageo.write (image, format_name, sortie); } / ** * @Title: Encode * @Description: 生成二维码 * @param Contenu 二维码内容 * @param output 输出流 * @throws exception 参数说明 * @return void 返回类型 * @throws * / public static void code (contenu de chaîne, sortie, sortie); } / ** * @Title: Decode * @Description: 对二维码解码 * @param fichier 文件对象 * @return 解码后的二维码内容字符串 * @throws exception 参数说明 * @return String 返回类型 * @throws * / private static String Decode (fichier file) lève une exception {BufferedImage Image; image = imageo.read (fichier); if (image == null) {return null; } BufferedImageLuminancesource Source = new BufferedImageLuminancesource (image); Binarybitmap bitmap = new binarybitmap (new hybridbinarizer (source)); HashTable <CodeHintType, String> hins = new HashTable <CodeHintType, String> (); hint.put (decodehintype.character_set, charset); return new MultiformatReader (). Decode (bitmap, indices) .getText (); } / ** * @Title: Decode * @Description: 对二维码解码 * @param path 文件路径 * @return * @throws exception 参数说明 * @return String 返回类型 * @throws * / public static String Decode (String path) lève l'exception {return qrcodeutil.decode (nouveau fichier (path)); }}Qrcodeservlet.java
package com.webos.servlet; import java.io.ioexception; import javax.servlet.servletException; importer javax.servlet.annotation.webservlet; import javax.servlet.httpservletservlet; import javax.servlet.http.httpservletrlet; javax.servlet.http.httpservletResponse; import com.webos.util.qrcodeutil; / * * Servlet Implémentation Classe Qrcodeservlet * / @ WebServlet ("/ QrcodeServlet") public class QrcodeServlet étend httvlet {private static final sérialsionuid = 1L; / * * @see httpservlet # httpservlet () * / public qrcodeservlet () {super (); } / * * @see httpservlet # doget (httpservletRequest request, httpservletResponse * réponse) * / protected void doget (httpservletRequest request, httpservletResponse réponse) lance le servletException, ioexception {try {string text = "http://www.baid.com?? System.Currenttimemillis (); Qrcodeutil.encode (text, réponse.getOutputStream ()); } catch (exception e) {e.printStackTrace (); }} / * * @see httpServlet # doPost (httpservletRequest request, httpservletResponse * réponse) * / protected void doPost (httpsservletRequest, httpservletResponse réponse) lance servletException, ioException {// TTTODO-généré par la méthode Stub Doget (requête, réponse); }}以上就是本文的全部内容 , 希望本文的内容对大家的学习或者工作能带来一定的帮助 , 同时也希望多多支持武林网!