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
package com.webos.util;import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import com.google.zxing.LuminanceSource;public class BufferedImageLuminanceSource extends LuminanceSource { private final BufferedImage image;私人決賽國際左右;私人最終INT頂部; public bufferedimageluminancesource(bufferedImage image){this(image,0,0,image.getWidth(),image.getheight()); } public buffer bufferedimageluminancesource(bufferedImage圖像,int,int,int top,int width,int height){super(width,height); int sourceWidth = image.getWidth(); int sourceHeight = image.getheight();如果(左 +寬度> sourceWidth || top + height> sourceHeight){拋出新的illegalargumentException(“裁切矩形不適合圖像數據。”); } for (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, 0xFFFFFFFF); // = white}}} this.image = new BufferedImage(sourceWidth,sourceHeight,bufferedimage.type_byte_gray); this.image.getGraphics()。繪製圖(Image,0,0,null); this.left =左; this.top = top; } public byte [] getrow(int y,byte [] row){if(y <0 || y> = getheight()){拋出新的illegalargumentException(“請求的行在圖像之外:” + y); } int width = getWidth(); if(row == null || row.length <width){row = new byte [width]; } image.getRaster()。 getDataelements(左,頂部 + y,width,1,行);返回行; } public byte [] getMatrix(){int width = getWidth(); int高度= getheight(); int區域=寬度 *高度;字節[]矩陣= new Byte [區域]; image.getRaster()。 getDataelements(左,頂部,寬度,高度,矩陣);返回矩陣; } public boolean isCropsUpported(){返回true; } public Luminancesource農作物(int,int,int top,int width,int高度){返回新的bufferedimageluminancesource(image,this.left +左,this.top + top + top,寬度,寬度,高度); } public boolean isRotateSupported(){返回true; } public Luminancesource rotatecounterCounterClockWise(){int sourceWidth = image.getWidth(); int sourceHeight = image.getheight(); AffinEtransform變換=新的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();返回新的BufferedImageluminancesource(旋轉圖像,頂部,源寬 - (左 +寬度),Getheight(),寬度); }}}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.BufferedImage;import 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.EncodeHintType;import com.google.zxing.MultiFormatReader;import com.google.zxing.MultiFormatWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.common.HybridBinarizer;import 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 CHARSET = "utf-8"; //保存的二維碼格式私有靜態最終字符串format_name =“ jpg”; //二維碼尺寸私有靜態最終int qrcode_size = 800; //徽標寬度私有靜態最終int logo_width = 80; //徽標高度私有靜態最終int logo_height = 80; / ** * @title:createImage * @description:將二維碼內容創建到image流 * @param content二維碼內容 * @param imgpath徽標圖片地址hashtable <encodeHintType,object> hints = new Hashtable <encodeHintType,object>(); hints.put(encodehinttype.error_correction,errorCorrectionLevel.h); hints.put(encodehinttype.character_set,charset); hints.put(encodehinttype.margin,1); bitmatrix bitmatrix = new MultiStionAtWriter()。 encode(content,barcodeformat.qr_code,qrcode_size,qrcode_size,size,提示); int width = bitmatrix.getWidth(); int height = bitmatrix.getheight(); bufferedImage image = new BufferedImage(寬度,高度,bufferedimage.type_int_rgb); for(int x = 0; x <width; x ++){for(int y = 0; y <heigh; y ++){image.setrgb(x,y,bitmatrix.get(x,x,y)?0xff000000:0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); }} if(logopath == null ||“” .equals(logopath)){return Image; } //插入徽標qrcodeutil.insertimage(Image,logopath,needcompress);返回圖像; } / ** * @title:intertimage * @description:logo插入到二維碼中 * @param source二維碼imag流 * @param imgpath logo地址 * @param needcompress是否壓縮大小新文件(logopath);如果(!file.exists()){system.err.println(“” + logopath +“ 該文件不存在!”);返回; } image src = imageio.read(new file(logopath)); int width = src.getWidth(null); int高度= src.getheight(null); if(needcompress){// logo if(width> logo_width){width = logo_width; } if(height> logo_height){height = logo_height; } image映像= src.getScaledInstance(寬度,高度,image.scale_smooth); bufferedimage tag = new BufferedImage(寬度,高度,bufferedimage.type_int_rgb); Graphics g = tag.getGraphics(); G.DrawImage(Image,0,0,null); //繪製縮小後的圖g.dispose(); src = image; } // logo graphics2d graph = source.creategraphics(); int x =(qrcode_size -width) / 2; int y =(qrcode_size-高度) / 2; graph.drawImage(src,x,y,width,height,null);形狀=新的rountRectangle2d.Float(x,y,width,width,6,6); graph.setstroke(新的基本震動(3F)); graph.draw(shape); graph.dispose(); } / ** * @title:mkdirs * @description:創建文件夾 * @param destpath文件夾地址 * @return void void @throws * @throws * / private static boolean mkdirs(string destpath){file file file file file = new file = new file(destpath); //當文件夾不存在時,mkdirs會自動創建多層目錄,區別於mkdir。 (mkdir如果父目錄不存在則會拋出異常)if(!file.exists()&&!file.isdirectory()){file.mkdirs();返回true; }返回false; } / ** * @title:encode * @description: @param content二維碼內容 * @param imgpath徽標圖片地址bufferedImage image = qrcodeutil.createimage(content,logopath,needcompress); if(mkdirs(destpath)){字符串file = new andural()。 nextInt(99999999) +“ .jpg”; ImageIO.Write(image,format_name,new File(DestPath +“/” + File)); } } /** * @Title: encode * @Description: 生成二維碼 * @param content 二維碼內容 * @param destPath 目標保存地址 * @throws Exception 參數說明 * @return void 返回類型 * @throws */ public static void encode(String content, String destPath) throws Exception { QRCodeUtil.encode(content, null, destPath, false); } / ** * @title:encode * @description:生成二維碼 * @param content二維碼內容 * @param imgpath徽標圖片地址bufferedImage image = qrcodeutil.createimage(content,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 * @throws * / private static string decode(file file file)throses throw exception {bufferedImage image; image = imageio.read(file); if(image == null){return null; } bufferedImageluminancesource source = new BufferedImageluminancesource(image); binarybitmap bitmap = new BinaryBitMap(新的Hybridbinarizer(source)); hashtable <decodeHintType,string> hints = new Hashtable <decodehinttype,string>(); hints.put(decodehinttype.character_set,charset);返回新的MultiformatReader()。解碼(位圖,提示).getText(); } / ** * @title:decode * @description:對二維碼解碼 * @param path文件路徑 * @return * @throws異常參數說明 * @return string返回類型 * @throws * / public static String decode(string path)拋出異常}}}qrcodeservlet.java
軟件包com.webos.servlet; import java.io.io.ioexception; import javax.servlet.servlet.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.serv.serv.servlet.http.httppps.httppservlet; import javax.servlet.servlet.httpp.httppppppppppppservlet importimpt 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#doget(httpservletrequest請求,httpservletResponse *響應) */受保護的void doget(httpservletrequest request,httpservletrequest request,httpservletResponseponse響應)thors servletException thring servletException,ioexception {ioexception {try try {try {try {try { system.currenttimemillis(); qrcodeutil.Encode(text,wendess.getOutputStream()); } catch(異常E){e.printstacktrace(); } } /* * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}}以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網! ,同時也希望多多支持武林網!