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; impor java.awt.graphics2d; impor java.awt.geom.affinetransform; impor java.awt.image.bufferedimage; impor com.google.zxing.luminancesource; class bufferedimageReMeluminancesoranceSource memperluas luminicing surveRceRce; Int final pribadi kiri; Private Final Int Top; publik bufferedimageluminancesource (gambar buferedimage) {this (image, 0, 0, image.getWidth (), image.getHeight ()); } public bufferedimageluminancesource (gambar buferedImage, int kiri, int atas, lebar int, int tinggi) {super (lebar, tinggi); int sourceWidth = image.getWidth (); int sourceHeight = image.getHeight (); if (left + width> sourceWidth || Top + Height> SourceHeight) {lempar IllegalArgumentException baru ("Crop Rectangle tidak sesuai dengan data gambar."); } untuk (int y = top; y <top+tinggi; y ++) {for (int x = kiri; x <kiri+lebar; x ++) {if ((gambar.getrgb (x, y) & 0xfffffff); // = putih}}} this.image = BufferedImage baru (SourceWidth, SourceHeight, bufferedImage.type_byte_gray); this.image.getGraphics (). DrawImage (gambar, 0, 0, null); this.left = kiri; this.top = top; } byte publik [] getrow (int y, byte [] baris) {if (y <0 || y> = getHeight ()) {lempar baru ilegalargumentException ("Baris yang diminta berada di luar gambar:" + y); } int lebar = getWidth (); if (row == null || row.length <width) {row = byte baru [lebar]; } image.getRaster (). getDataelement (kiri, atas + y, lebar, 1, baris); Baris pengembalian; } byte publik [] getMatrix () {int width = getWidth (); int tinggi = getHeight (); Area int = lebar * tinggi; byte [] matriks = byte baru [area]; image.getRaster (). getDataelement (kiri, atas, lebar, tinggi, matriks); matriks pengembalian; } public boolean isCropsupported () {return true; } tanaman luminancesource publik (int kiri, int atas, lebar int, tinggi int) {return baru bufferedimageluminancesource (gambar, this.left + kiri, this.top + atas, lebar, tinggi); } public boolean isRotatesupported () {return true; } luminancesource publik rotatecounterclockwise () {int sourceWidth = image.getWidth (); int sourceHeight = image.getHeight (); Affinetransform transform = affinetransform baru (0,0, -1.0, 1.0, 0.0, 0.0, SourceWidth); BufferedImage rotedImage = BufferedImage baru (SourceHeight, SourceWidth, bufferedImage.type_byte_gray); Grafik2d g = rotedImage.creategraphics (); G.DrawImage (gambar, transformasi, null); g.dispose (); Int width = getWidth (); return baru bufferedimageluminancesource (rotedimage, atas, sourceWidth - (kiri + lebar), getHeight (), lebar); }}Qrcodeutil.java
Paket com.webos.util; impor java.awt.basicstroke; impor java.awt.graphics; impor java.awt.graphics2d; impor java.awt.image; impor java.awt.shape; impor java.awt.geom.roundrectangle; impor java. java.io.file; impor java.io.outputstream; import java.util.hashtable; import java.util.random; impor javax.imageio.imageio; com.ginybitmaporpor.barporpormat; impor Com.google.zxing.binarymap; com.google.zxing.encodehinttype; import com.google.zxing.multiformatreader; import com.google.zxing.multiformatwriter; import com.google.zxing.common.bitmatrix; impor com.google.zxing.common.hyridbin; com.google.zxing.qrcode.decoder.errorcorrectionLevel;/*** @classname: qrcodeutil* @description: 二维码编码* @author liuy* @Date 2016 年 7月 9 日 3:03:24*/Kelas Publik Qrcodeutil {/设置二维码编码格式 3:03:24**/Kelas Publik Qrcodeutil {/设置二维码编码格式 3:03:24** // 保存的二维码格式 String final final final format_name = "jpg"; // 二维码尺寸 int private static 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> hints = hashtable baru <encodehintType, object> (); hints.put (encodehinttype.error_correction, errorCorrectionLevel.h); hints.put (encodehinttype.character_set, charset); hints.put (encodehinttype.margin, 1); Bitmatrix bitmatrix = multiformatwriter baru (). Encode (konten, barcodeformat.qr_code, qrcode_size, qrcode_size, petunjuk); Int width = bitmatrix.getWidth (); int tinggi = bitmatrix.getHeight (); BufferedImage Image = BufferedImage baru (lebar, tinggi, bufferedImage.type_int_rgb); untuk (int x = 0; x <lebar; x ++) {untuk (int y = 0; y <tinggi; y ++) {image.setrgb (x, y, bitmatrix.get (x, y)? 0xff000000: 0xffffffff); }} if (logopath == null || "" .equals (logopath)) {return gambar; } // 插入 Logo qrcodeutil.insertImage (gambar, logopath, needcompress); gambar kembali; } / ** * @Title: InsertImage * @Description: 将 Logo 插入到二维码中 * @param Sumber 二维码 Gambar 流 * @param Imgpath Logo 地址 * @param membutuhkan Compress 是否压缩大小 * @Throws Exception 参数说明 @Return void 返回类型 * @Throws * / Private Static Void Sumber (BufferedImage Sumber {Bu Bufferon {BODROPHESS {throws * @throws * File (logopath); if (! File.exists ()) {System.err.println ("" + Logopath + "该文件不存在!"); kembali; } Image src = ImageIo.read (file baru (logopath)); Int width = src.getWidth (null); int tinggi = src.getHeight (null); if (needsompress) {// 压缩 logo if (width> lOGo_width) {width = logo_width; } if (height> logo_height) {height = logo_height; } Gambar gambar = src.getScaledInstance (lebar, tinggi, gambar.scale_smooth); BufferedImage Tag = BufferedImage baru (lebar, tinggi, bufferedImage.type_int_rgb); Grafik g = tag.getGraphics (); g.drawimage (gambar, 0, 0, null); // 绘制缩小后的图 g.dispose (); src = gambar; } // 插入 Logo Graphics2d Graph = Source.Creategraphics (); int x = (qrcode_size - lebar) / 2; int y = (qrcode_size - tinggi) / 2; graph.drawimage (src, x, y, lebar, tinggi, null); Bentuk bentuk = roundrectangle2d.float (x, y, lebar, lebar, 6, 6); graph.setstroke (New BasicStroke (3F)); graph.draw (bentuk); graph.dispose (); } / ** * @title: mkdirs * @description: 创建文件夹 * @param destpath 文件夹地址 * @return void 返回类型 * @throws * / private static boolean mkdirs (string destpath) {file file = file baru (destPath); // 当文件夹不存在时 , mkdirs 会自动创建多层目录 , 区别于 mkdir. (mkdir 如果父目录不存在则会抛出异常) if (! File.exists () &&! File.isdirectory ()) {file.mkdirs (); Kembali Benar; } return false; } / ** * @title: encode * @description: 生成二维码 * @param konten 二维码内容 * @param imgpath logo 图片地址 * @param destpath 目标保存地址 * @param memerlukan kompres 是否压缩 logo 图片大小 * @THROWS Excection 参数说明 * @return void 返回类型 * @THROWS * / Private Static Encode (String Contact 参数说明 @Return void 返回类型 * @Throws * / Private Static Encode Void ENCODE) {BufferedImage Image = qrcodeutil.createImage (konten, logopath, needcompress); if (mkdirs (destPath)) {string file = new random (). nextInt (99999999) + ".jpg"; ImageIo.write (Image, Format_Name, File Baru (DestPath + "/" + File)); }} / ** * @title: encode * @description: 生成二维码 * @param konten 二维码内容 * @param destpath 目标保存地址 * @throws Exception 参数说明 * @return void 返回类型 * @throws * / public static encode (konten string, string destpath) melempar pengecualian {qrcodeutil.encode, null, null null, nol, nol, nol, nol), nulspath, nol), nol), nol), nol), nol) {qrcodeutil. } / ** * @title: encode * @description: 生成二维码 * @param konten 二维码内容 * @param imgpath logo 图片地址 * @param output 输出流 * @param memerlukan Compres Exception {BufferedImage Image = qrcodeutil.createImage (konten, logopath, needcompress); ImageIO.Write (Image, Format_Name, Output); } /** * @Title: encode * @Description: 生成二维码 * @param content 二维码内容 * @param output 输出流 * @throws Exception 参数说明 * @return void 返回类型 * @throws */ public static void encode(String content, OutputStream output) throws Exception { QRCodeUtil.encode(content, null, output, false); } / ** * @title: decode * @description: 对二维码解码 * @param file 文件对象 * @return 解码后的二维码内容字符串 * @throws Exception 参数说明 * @Return String 返回类型 * @throws * / Private Static String Decode (File File) Melempar Pengecualian {BufferedImage Image; Image = ImageIo.read (file); if (gambar == null) {return null; } Bufferedimageluminancesource Source = Bufferedimageluminancesource baru (gambar); BinaryBitMap Bitmap = BinaryBitMap baru (hybridbinarizer baru (sumber)); Hashtable <decodehinttype, string> hints = hashtable baru <decodehinttype, string> (); hints.put (decodehinttype.character_set, charset); return new MultiformatReader (). Decode (bitmap, petunjuk) .getText (); } / ** * @title: decode * @description: 对二维码解码 * @param path 文件路径 * @return * @throws Exception 参数说明 * @return String 返回类型 * @throws * / public static String decode (jalur string) melempar Exception {return qrcodeutil.decode (file baru); }}Qrcodeservlet.java
Paket com.webos.servlet; impor java.io.ioException; impor javax.servlet.servletException; impor javax.servlet.annotation.webservlet; import javax.servlet.http.htpservlet; impor javax.servlet.htp.htpservlet; impor javax.servlet.htp.htpservlet; impor; 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 (); }/? + System.currentTimemillis (); Qrcodeutil.encode (text, response.getoutputStream ()); } catch (Exception e) {E.PrintStackTrace (); }} /? }}以上就是本文的全部内容 , 希望本文的内容对大家的学习或者工作能带来一定的帮助 , 同时也希望多多支持武林网!