1 、 Jar : : 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
Paket com.webos.util; import Java.awt.graphics2d; import Java.awt.geom.Affinetransform; Import Java.awt.image.Buffeedimage; privates Finale int gelassen; Private Final INT Top; public bufferedImageluminancesource (bufferedimage Bild) {this (Bild, 0, 0, Image.getWidth (), Image.Getheight ()); } public bufferedImageluminancesource (bufferedImage -Bild, int links, int oben, int Breite, int Höhe) {Super (Breite, Höhe); int sourcewidth = image.getWidth (); int SourceHeight = Image.Getheight (); if (links + width> SourceWidth || Top + Höhe> SourceHeight) {Wirf neu illegalArgumentException ("Crop -Rechteck passt nicht in Bilddaten."); } für (int y = top; y <top+Höhe; y ++) {für (int x = links; x <links+width; x ++) {if ((image.getRGB (x, y) & 0xff0000000000) == 0) {image.setrgb (x, y, 0xffffffffffr); // = weiß}}} this.image = new bufferedImage (SourceWidth, SourceHeight, BufferedImage.Type_Byte_Gray); this.image.getGraphics (). DrawImage (Bild, 0, 0, null); this.left = links; this.top = top; } public byte [] getRow (int y, byte [] row) {if (y <0 || y> = getheight ()) {werfen neuer illegalArgumentException ("angeforderte Zeile befindet sich außerhalb des Bildes:" + y); } int width = getwidth (); if (row == null || row.length <width) {row = new byte [width]; } image.getRaster (). getDataelements (links, oberst + y, width, 1, row); Rückreihe; } public byte [] getMatrix () {int width = getWidth (); int Höhe = getheight (); int Bereich = Breite * Höhe; byte [] matrix = new Byte [Bereich]; Image.getRaster (). getDataElements (links, oben, Breite, Höhe, Matrix); MATRIX RETURN; } public boolean iscropsupported () {return true; } public luminancesource crop (int links, int top, int width, int lhohe) {return New bufferedImageluminancesource (Bild, this.left + links, this.top + top, width, Höhe); } public boolean isRotateSupported () {return true; } public luminancesource rotateCounterClockwise () {int sourceWidth = image.getWidth (); int SourceHeight = Image.Getheight (); Affinetransform -Transformation = Neue Affinetransform (0,0, -1,0, 1,0, 0,0, 0,0, Quellbreite); BufferedImage rotatedImage = new bufferedImage (SourceHeight, SourceWidth, BufferedImage.Type_Byte_Gray); Graphics2d g = rotatedImage.createGraphics (); G.Drawimage (Bild, Transformation, NULL); g.disponse (); int width = getwidth (); Rückgabe neuer BufferedImageluminancesource (RotatedImage, oben, Quellbreite - (links + Breite), Getheight (), Breite); }}QrcodeUtil.java
Paket com.webos.util; Import Java.awt.BasicsTroke; Import Java.awt.graphics; Import Java.awt.graphics2d; Import Java.awt.image; Import Java.awt.Shape; java.io.file; import java.io.outputstream; import Java.util.hashtable; import Java.util.random; import Javax.imageo.imageo; import com.google.zxing.barcodeformat; com.gxing.biogymap; com.google.zxing.codeHinttype; import com.google.zxing.multiformatreader; import com.google.zxing.multiformatwriter; import com.google.zxing.common.bitmatrix; Import com.ggxing.common.hybridbinarizer; com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/*** @className: QrCodeUtil* @Description: 二维码编码* @Author liuy* @date 2016 年 7月 9 日 下午 3:03:24**/public class Qrcododil {/设置二维码编码格式 设置二维码编码格式 private static edle string string = "/public class qrcododil {/设置二维码编码格式 设置二维码编码格式 设置二维码编码格式 private static edle string string ="/public utf-8 "; // 保存的二维码格式 private statische endgültige Zeichenfolge format_name = "jpg"; // 二维码尺寸 private statische endgültige int qrcode_size = 800; // Logo 宽度 Private statische endgültige int logo_width = 80; // Logo 高度 private statische endgültige 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 <CodesHintType, Object> Hints = new Hashtable <encodeHintType, Object> (); filts.put (EncodehintType.Error_Correction, Fehlerkorrektur -Level.h); Hints.put (EncodehintType.character_set, charSet); Hints.put (Encodehintype.Margin, 1); Bitmatrix bitmatrix = new multiformatWriter (). Codode (Inhalt, barcodeFormat.qr_code, qrcode_size, qrcode_size, Hinweise); int width = bitmatrix.getWidth (); int height = bitmatrix.getheight (); Bufferedimage image = new bufferedImage (width, Höhe, bufferedimage.type_int_rgb); für (int x = 0; x <width; x ++) {für (int y = 0; y <Höhe; y ++) {image.setRGB (x, y, bitmatrix.get (x, y)? 0xff000000: 0xffffffff); }} if (logopath == null || "" .Equals (logopath)) {return Image; } // 插入 Logo qrCodeUtil.insertimage (Bild, Logopath, NeedCompress); Return Image; } /** * @Title: insertImage * @Description: 将logo插入到二维码中 * @param source 二维码Image流 * @param imgPath logo地址 * @param needCompress 是否压缩大小 * @throws Exception 参数说明 * @return void 返回类型 * @throws */ private static void insertImage(BufferedImage source, String logoPath, boolean needCompress) throws Exception { File file = new Datei (Logopath); if (! file.exists ()) {System.err.println ("" + logopath + "该文件不存在!"); zurückkehren; } Image src = imageio.read (neue Datei (Logopath)); int width = src.getwidth (null); int height = src.getheight (null); if (NeedCompress) {// 压缩 Logo if (width> logo_width) {width = logo_width; } if (Höhe> logo_Height) {height = logo_height; } Image Image = src.getScaledInstance (Breite, Höhe, Image.Scale_Smooth); Bufferedimage tag = new bufferedimage (width, Höhe, bufferedimage.type_int_rgb); Grafik g = Tag.getGraphics (); g.drawimage (Bild, 0, 0, null); // 绘制缩小后的图 g.disponse (); src = Bild; } // 插入 Logo Graphics2D Graph = source.CreateGraphics (); int x = (qrcode_size - width) / 2; int y = (qrcode_size - Höhe) / 2; Graph.Drawimage (SRC, X, Y, Breite, Höhe, Null); Formform = neuer RoundRectangle2D.Float (x, y, Breite, Breite, 6, 6); Graph.SetStroke (neuer BasicStroke (3F)); Graph.Draw (Form); graph.dispose (); } / ** * @title: mkdirs * @Description: 创建文件夹 * @param destpath 文件夹地址 * @return void 返回类型 * @throws * / private statische boolean mkdirs (String destpath) {Datei Datei = new Datei (destpath); // 当文件夹不存在时 , mkdirs 会自动创建多层目录 , 区别于 mkdir. (mkdir 如果父目录不存在则会抛出异常) if (! File.exists () &&! File.isdirectory ()) {file.mkdirs (); zurückkehren; } return false; } /** * @Title: encode * @Description: 生成二维码 * @param content 二维码内容 * @param imgPath logo图片地址 * @param destPath 目标保存地址 * @param needCompress 是否压缩logo图片大小 * @throws Exception 参数说明 * @return void 返回类型 * @throws */ private static void encode(String content, String logoPath, String destPath, boolean needCompress) throws Exception { BufferedImage image = QrCodeUtil.createimage (Inhalt, Logopath, NeedCompress); if (mkdirs (destpath)) {String -Datei = new random (). NextInt (99999999999999999999999999999999999999999999999) .jpg "; ImageIo.Write (Bild, format_name, neue Datei (DestPath + "/" + Datei)); }} / ** * @title: codieren * @Description: 生成二维码 * @param content 二维码内容 * @param destpath 目标保存地址 * @Throws Ausnahme 参数说明 * @Return void 返回类型 * @throws * / public static void codes (String Inhalt, String destpat) Ausnahme {qrCodeUtil.codode (content, null, null, destpat, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, teure, destpat, teure, teure, teure, teure, destpath, teastpath); } /** * @Title: encode * @Description: 生成二维码 * @param content 二维码内容 * @param imgPath logo图片地址 * @param output 输出流 * @param needCompress 是否压缩logo图片大小 * @throws Exception 参数说明 * @return void 返回类型 * @throws */ public static void encode(String content, String logoPath, OutputStream output, boolean needCompress) throws Exception { BufferedImage image = QrCodeUtil.createimage (Inhalt, Logopath, NeedCompress); ImageIo.Write (Bild, format_name, Ausgabe); } / ** * @title: codieren * @Description: 生成二维码 * @param content 二维码内容 * @param output 输出流 * @throws Ausnahme 参数说明 * @return void 返回类型 * @throws * / public static void codieren (String -Inhalt, Ausgabestream -Ausgabe) löscht Ausnahme aus {qrCode.encode (content, null, null, fehl); } / ** * @title: decodode * @Description: 对二维码解码 * @param -Datei 文件对象 * @return 解码后的二维码内容字符串 * @throws Exception 参数说明 * @return String 返回类型 * @throws * / private statische Zeichenfolge Decode (Dateidatei) löst Ausnahme {bufferedimage -Bild aus; Image = imageio.read (Datei); if (image == null) {return null; } BufferedImageluminancesource Source = new bufferedImageluminancesource (Bild); Binarybitmap bitmap = new Binarybitmap (neuer Hybridbinarisator (Quelle)); Hashtable <decodeHintType, String> Hints = new Hashtable <decodeHintType, String> (); filts.put (decodeHintType.character_set, charset); return New MultiformatReader (). Decode (Bitmap, Hinweise) .GIETTEXT (); } / ** * @title: decode * @Description: 对二维码解码 * @param path 文件路径 * @return * @throws Exception 参数说明 * @return String 返回类型 * @throws * / public static String decode (String Pfad) löst Ausnahme aus {return qrCodeUtil.decode (neue Datei)); }}Qrcodeservlet.java
Paket com.webos.servlet; import Java.io.ioException; import Javax.servlet.servletException; import Javax.servlet.Annotation javax.servlet.http.HttpServletResponse;import com.webos.util.QRCodeUtil;/* * Servlet implementation class QRCodeServlet */@WebServlet("/QRCodeServlet")public class QRCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; / * * @see httpServlet#httpServlet () */ public qrcodeservlet () {Super (); }/ * * @see httpServlet#dagget (httpServletRequest Request, httpServletResponse * Antwort) */Protected void dagget (httpServletRequest Request, httpServletResponse -Antwort) Throws ServletException, ioException {try {String text = "httpLesponse {{String text =" http. System.currentTimemillis (); QrCodeUtil.encode (text, response.getOutputStream ()); } catch (Ausnahme e) {e.printstacktrace (); }} / * * @see httpServlet#Dopost (httpServletRequest-Anforderung, httpServletResponse * Antwort) * / Protected void dopost (httpServletRequest-Anforderung, httpServletRectRect) löscht ServletException, IOException {// // //, um auto-gern-method-stur (Anfrage zu reagieren). }}以上就是本文的全部内容 , 希望本文的内容对大家的学习或者工作能带来一定的帮助 , 同时也希望多多支持武林网! 同时也希望多多支持武林网!