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
패키지 com.webos.util; import java.awt.graphics2d; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; import com.google.zxing.luminances and public class bufferedimageluminances {luminances {개인 버퍼링 된 버퍼링 된 이미지 이미지; 개인 최종 INT 왼쪽; 개인 최종 INT 탑; public bufferedimageluminancesource (bufferedImage image) {this (image, 0, 0, image.getWidth (), image.getheight ()); } public bufferedimageluminancesource (bufferedImage 이미지, int 왼쪽, int 상단, int 너비, int 높이) {슈퍼 (너비, 높이); int sourceWidth = image.getWidth (); int sourceHeight = image.getheight (); if (왼쪽 + 너비> sourcewidth || top + height> sourceHeight) {새로운 불법 불법 행위 ( "작물 사각형은 이미지 데이터에 맞지 않습니다."); } for (int y = top; y <top+height; y ++) {for (int x = left; x <왼쪽+너비; x ++) {if ((image.getrgb (x, y) & 0xff000000) == 0) {image.setrgb (x, y, 0xffffff); // = white}}} this.image = new bufferedImage (sourceWidth, sourceHeight, bufferedImage.type_byte_gray); this.image.getGraphics (). DrawImage (image, 0, 0, null); this.left = 왼쪽; this.top = 상단; } public byte [] getrow (int y, byte [] row) {if (y <0 || y> = getheight ()) {throw new new OrgalArgumentException ( "요청 된 행은 이미지 외부에 있습니다 :" + 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 height = getheight (); int 면적 = 너비 * 높이; 바이트 [] 매트릭스 = 새로운 바이트 [영역]; image.getRaster (). getDataelements (왼쪽, 상단, 너비, 높이, 행렬); 리턴 매트릭스; } public boolean iscropsupported () {return true; } public luminancesource crop (int 왼쪽, int 상단, int 너비, int 높이) {return new bufferedimageluminancesource (image, this.left + left, this.top + 상단, 너비, 높이); } public boolean isrotatesUpported () {return true; } public luminancesource rotateCounterClockise () {int sourceWidth = image.getWidth (); int sourceHeight = image.getheight (); 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 (이미지, 변환, null); g.dispose (); int width = getWidth (); 새로운 BufferedImageluminanceSource (RotatedImage, Top, SourceWidth- (왼쪽 + 너비), 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.buperedimage; 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.binarytybitmap; import com.zxogle.decodehodehintpe; 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 月 日 3:03:24*/public class qrcodeutil {// 设置二维码编码格式 设置二维码编码格式 设置二维码编码格式 设置二维码编码格式 设置二维码编码格式 设置二维码编码格式 设置二维码编码格式 下午 下午 3:03:24*/public class qrcodeutil {// // 保存的二维码格式 개인 정적 최종 문자열 format_name = "jpg"; // 二维码尺寸 개인 정적 최종 최종 int qrcode_size = 800; // 로고 宽度 개인 정적 최종 최종 int logo_width = 80; // 로고 高度 개인 정적 최종 int logo_height = 80; / ** * @title : createImage * @description : 将二维码内容创建到 indage 流 * @param content 二维码内容 * @param imgpath 로고 图片地址 * @param needcompress 是否压缩 logo 图片大小 * @return * @throws 예외 参数说明 * @return bufferedimage 返回类型 * @throws */ private static buffered 컨텐츠 (string, boolean) hashtable <encodehinttype, object> hints = new hashtable <encodehinttype, object> (); 힌트. hints.put (encodehinttype.character_set, charset); hints.put (encodehinttype.margin, 1); bitmatrix bitmatrix = new multiformatwriter (). encode (content, barcodeformat.qr_code, qrcode_size, qrcode_size, hints); int width = bitmatrix.getWidth (); int height = bitmatrix.getheight (); BufferedImage image = 새로운 BufferedImage (너비, 높이, BufferedImage.type_int_rgb); for (int x = 0; x <width; x ++) {for (int y = 0; y <height; y ++) {image.setrgb (x, y, bitmatrix.get (x, y)? 0xfffffffff); }} if (logopath == null || "".Equals (logopath)) {return image; } // 로고 로고 qrcodeutil.insertimage (이미지, 로지 패스, Needcompress); 리턴 이미지; } / ** * @title : insertImage * @description : 将 로고 插入到二维码中 * @param 소스 二维码 이미지 流 * @param imgpath 로고 地址 * @param needcompress 是否压缩大小 * @throws 예외 参数说明 * @return void 返回类型 * @throws * / private static void insertimage (bubsered grows, boolean newopath, boolean newopath retrowe) 파일 (logopath); if (! file.exists ()) {system.err.println ( "" + logopath + "该文件不存在!"); 반품; } image src = imageio.read (새 파일 (logopath)); int width = src.getWidth (null); int height = src.getheight (null); if (needcompress) {// 压缩 로고 if (width> logo_width) {width = logo_width; } if (height> logo_height) {height = logo_height; } image image = src.getScaledInstance (너비, 높이, image.scale_smooth); bufferedImage tag = 새로운 bufferedImage (너비, 높이, bufferedImage.type_int_rgb); 그래픽 g = tag.getGraphics (); g.DrawImage (image, 0, 0, null); // 绘制缩小后的图 g.dispose (); SRC = 이미지; } // 插入 로고 그래픽 2d 그래프 = source.creategraphics (); int x = (qrcode_size -width) / 2; int y = (qrcode_size- 높이) / 2; Graph.DrawImage (src, x, y, 너비, 높이, null); 모양 구성 = New 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 = 새 파일 (destpath); // 当文件夹不存在时 当文件夹不存在时, mkdirs 会自动创建多层目录 会自动创建多层目录 会自动创建多层目录, 区别于 mkdir. (mkdir 如果父目录不存在则会抛出异常) if (! file.exists () &&! file.isdirectory ()) {file.mkdirs (); 진실을 반환하십시오. } false를 반환합니다. } / ** * @title : encode * @description : 生成二维码 * @param content 二维码内容 * @param imgpath 로고 图片地址 * @param destpath 目标保存地址 * @param needcompress 是否压缩 * @throws 예외 参数说明 * @Throws * / private static void encode (문자열 예외, String throws) BufferedImage image = qrcodeutil.createimage (내용, 로지 패스, Needcompress); if (mkdirs (destPath)) {문자열 파일 = 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 (문자열 콘텐츠, String DestPath) {qrcodeUtil.encode (내용, destPation, 거짓). } / ** * @title : encode * @description : 生成二维码 * @param content 二维码内容 * @param imgpath 로고 图片地址 * @param output 输出流 * @param needcompress 是否压缩 * @thrrows 예외 参数说明 * @Throws * / public static void encode (문자열 logopath, string leancpress, boolean retpution, boolean rowper) BufferedImage image = qrcodeutil.createimage (내용, 로지 패스, Needcompress); imageio.write (image, format_name, output); } / ** * @title : encode * @description : 生成二维码 * @param content 二维码内容 * @param output 输出流 * @throws 예외 参数说明 * @return void 返回类型 * @throws * / public static void encode (문자열 내용, 출력 스트림 출력) 예외 {qrcodeUtil.encode (내용, 출력, 거짓); } / ** * @title : decode * @description : 对二维码解码 * @param 파일 文件对象 * @return 解码后的二维码内容字符串 * @throws 예외 参数说明 * @return string 返回类型 * @throws * / private static string decode (파일 파일) 예외 {bufferedimage image; image = imageio.read (파일); if (image == null) {return null; } bufferedimageluminancesource source = new bufferedimageluminancesource (image); BinaryBitMap 비트 맵 = New BinaryBitMap (New HybridBinarizer (Source)); hashtable <decodehinttype, string> hints = new Hashtable <decodehinttype, String> (); 힌트. 새 multiformatreader (). decode (bitmap, hints) .getText ()를 반환합니다. } / ** * @title : decode * @description : 对二维码解码 * @param path 文件路径 * @return * @throws Exception 参数说明 * @return String 返回类型 * @throws * / public static string decode (String Path) 예외 {return qrcodeutil.decode (new File (path)); }}qrcodeservlet.java
패키지 com.webos.servlet; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.httpervletrequest; import; javax.servlet.http.http.httpervletresponse; import com.webos.util.qrcodeutil;/ * * 서블릿 구현 클래스 qrcodeservlet */@webservlet ( "/qrcodeservlet") public class qrcodeservlet 확장 httpservlet {private static final longionionioid = 1l; / * * @httpservlet#httpservlet () */ public qrcodeservlet () {super (); }/ * * @see httpservlet#doget (httpservletrequest request, httpservletresponse * response) */protected void doget (httpservletrequest request, httpservletresponse 응답) servletexception, ioexception {string text= "http://www.c.com? System.CurrentTimeMillis (); qrcodeUtil.encode (text, response.getOutputStream ()); } catch (예외 e) {e.printstacktrace (); }} / * * @ @httpservlet#dopost (httpservletRequest 요청, httpservletResponse * response) * / protected void dopost (httpservletrequest 요청, httpservletrepsonse 응답) servletexception, ioexception, ioxception {// auto-genderated method (요청, 응답); }}以上就是本文的全部内容 以上就是本文的全部内容, 希望本文的内容对大家的学习或者工作能带来一定的帮助, 同时也希望多多支持武林网!