Java二维码工具栏-带中间logo


麻城创捷信息技术有限公司,专注于App,微信,网站,企业信息系统开发,专业承接软件项目开发。

麻城创捷官网http://www.foundjet.com


本文转自

http://blog.csdn.net/mmm333zzz/article/details/17259513

 

  1. package com.util.cccm;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.Graphics;  
  5. import java.awt.Graphics2D;  
  6. import java.awt.Image;  
  7. import java.awt.Shape;  
  8. import java.awt.geom.RoundRectangle2D;  
  9. import java.awt.image.BufferedImage;  
  10. import java.io.File;  
  11. import java.io.OutputStream;  
  12. import java.util.Hashtable;  
  13. import java.util.Random;  
  14.   
  15. import javax.imageio.ImageIO;  
  16.   
  17. import com.google.zxing.BarcodeFormat;  
  18. import com.google.zxing.BinaryBitmap;  
  19. import com.google.zxing.DecodeHintType;  
  20. import com.google.zxing.EncodeHintType;  
  21. import com.google.zxing.MultiFormatReader;  
  22. import com.google.zxing.MultiFormatWriter;  
  23. import com.google.zxing.Result;  
  24. import com.google.zxing.common.BitMatrix;  
  25. import com.google.zxing.common.HybridBinarizer;  
  26. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  
  27.   
  28. /** 
  29.  * 二维码工具类 
  30.  *  
  31.  */  
  32. public class QRCodeUtil {  
  33.   
  34.     private static final String CHARSET = "utf-8";  
  35.     private static final String FORMAT_NAME = "JPG";  
  36.     // 二维码尺寸  
  37.     private static final int QRCODE_SIZE = 300;  
  38.     // LOGO宽度  
  39.     private static final int WIDTH = 60;  
  40.     // LOGO高度  
  41.     private static final int HEIGHT = 60;  
  42.   
  43.     private static BufferedImage createImage(String content, String imgPath,  
  44.             boolean needCompress) throws Exception {  
  45.         Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();  
  46.         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);  
  47.         hints.put(EncodeHintType.CHARACTER_SET, CHARSET);  
  48.         hints.put(EncodeHintType.MARGIN, 1);  
  49.         BitMatrix bitMatrix = new MultiFormatWriter().encode(content,  
  50.                 BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);  
  51.         int width = bitMatrix.getWidth();  
  52.         int height = bitMatrix.getHeight();  
  53.         BufferedImage image = new BufferedImage(width, height,  
  54.                 BufferedImage.TYPE_INT_RGB);  
  55.         for (int x = 0; x < width; x++) {  
  56.             for (int y = 0; y < height; y++) {  
  57.                 image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000  
  58.                         : 0xFFFFFFFF);  
  59.             }  
  60.         }  
  61.         if (imgPath == null || "".equals(imgPath)) {  
  62.             return image;  
  63.         }  
  64.         // 插入图片  
  65.         QRCodeUtil.insertImage(image, imgPath, needCompress);  
  66.         return image;  
  67.     }  
  68.   
  69.     /** 
  70.      * 插入LOGO 
  71.      *  
  72.      * @param source 
  73.      *            二维码图片 
  74.      * @param imgPath 
  75.      *            LOGO图片地址 
  76.      * @param needCompress 
  77.      *            是否压缩 
  78.      * @throws Exception 
  79.      */  
  80.     private static void insertImage(BufferedImage source, String imgPath,  
  81.             boolean needCompress) throws Exception {  
  82.         File file = new File(imgPath);  
  83.         if (!file.exists()) {  
  84.             System.err.println(""+imgPath+"   该文件不存在!");  
  85.             return;  
  86.         }  
  87.         Image src = ImageIO.read(new File(imgPath));  
  88.         int width = src.getWidth(null);  
  89.         int height = src.getHeight(null);  
  90.         if (needCompress) { // 压缩LOGO  
  91.             if (width > WIDTH) {  
  92.                 width = WIDTH;  
  93.             }  
  94.             if (height > HEIGHT) {  
  95.                 height = HEIGHT;  
  96.             }  
  97.             Image image = src.getScaledInstance(width, height,  
  98.                     Image.SCALE_SMOOTH);  
  99.             BufferedImage tag = new BufferedImage(width, height,  
  100.                     BufferedImage.TYPE_INT_RGB);  
  101.             Graphics g = tag.getGraphics();  
  102.             g.drawImage(image, 00null); // 绘制缩小后的图  
  103.             g.dispose();  
  104.             src = image;  
  105.         }  
  106.         // 插入LOGO  
  107.         Graphics2D graph = source.createGraphics();  
  108.         int x = (QRCODE_SIZE - width) / 2;  
  109.         int y = (QRCODE_SIZE - height) / 2;  
  110.         graph.drawImage(src, x, y, width, height, null);  
  111.         Shape shape = new RoundRectangle2D.Float(x, y, width, width, 66);  
  112.         graph.setStroke(new BasicStroke(3f));  
  113.         graph.draw(shape);  
  114.         graph.dispose();  
  115.     }  
  116.   
  117.     /** 
  118.      * 生成二维码(内嵌LOGO) 
  119.      *  
  120.      * @param content 
  121.      *            内容 
  122.      * @param imgPath 
  123.      *            LOGO地址 
  124.      * @param destPath 
  125.      *            存放目录 
  126.      * @param needCompress 
  127.      *            是否压缩LOGO 
  128.      * @throws Exception 
  129.      */  
  130.     public static void encode(String content, String imgPath, String destPath,  
  131.             boolean needCompress) throws Exception {  
  132.         BufferedImage image = QRCodeUtil.createImage(content, imgPath,  
  133.                 needCompress);  
  134.         mkdirs(destPath);  
  135.         String file = new Random().nextInt(99999999)+".jpg";  
  136.         ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));  
  137.     }  
  138.   
  139.     /** 
  140.      * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) 
  141.      * @author lanyuan 
  142.      * Email: mmm333zzz520@163.com 
  143.      * @date 2013-12-11 上午10:16:36 
  144.      * @param destPath 存放目录 
  145.      */  
  146.     public static void mkdirs(String destPath) {  
  147.         File file =new File(destPath);      
  148.         //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)  
  149.         if (!file.exists() && !file.isDirectory()) {  
  150.             file.mkdirs();  
  151.         }  
  152.     }  
  153.   
  154.     /** 
  155.      * 生成二维码(内嵌LOGO) 
  156.      *  
  157.      * @param content 
  158.      *            内容 
  159.      * @param imgPath 
  160.      *            LOGO地址 
  161.      * @param destPath 
  162.      *            存储地址 
  163.      * @throws Exception 
  164.      */  
  165.     public static void encode(String content, String imgPath, String destPath)  
  166.             throws Exception {  
  167.         QRCodeUtil.encode(content, imgPath, destPath, false);  
  168.     }  
  169.   
  170.     /** 
  171.      * 生成二维码 
  172.      *  
  173.      * @param content 
  174.      *            内容 
  175.      * @param destPath 
  176.      *            存储地址 
  177.      * @param needCompress 
  178.      *            是否压缩LOGO 
  179.      * @throws Exception 
  180.      */  
  181.     public static void encode(String content, String destPath,  
  182.             boolean needCompress) throws Exception {  
  183.         QRCodeUtil.encode(content, null, destPath, needCompress);  
  184.     }  
  185.   
  186.     /** 
  187.      * 生成二维码 
  188.      *  
  189.      * @param content 
  190.      *            内容 
  191.      * @param destPath 
  192.      *            存储地址 
  193.      * @throws Exception 
  194.      */  
  195.     public static void encode(String content, String destPath) throws Exception {  
  196.         QRCodeUtil.encode(content, null, destPath, false);  
  197.     }  
  198.   
  199.     /** 
  200.      * 生成二维码(内嵌LOGO) 
  201.      *  
  202.      * @param content 
  203.      *            内容 
  204.      * @param imgPath 
  205.      *            LOGO地址 
  206.      * @param output 
  207.      *            输出流 
  208.      * @param needCompress 
  209.      *            是否压缩LOGO 
  210.      * @throws Exception 
  211.      */  
  212.     public static void encode(String content, String imgPath,  
  213.             OutputStream output, boolean needCompress) throws Exception {  
  214.         BufferedImage image = QRCodeUtil.createImage(content, imgPath,  
  215.                 needCompress);  
  216.         ImageIO.write(image, FORMAT_NAME, output);  
  217.     }  
  218.   
  219.     /** 
  220.      * 生成二维码 
  221.      *  
  222.      * @param content 
  223.      *            内容 
  224.      * @param output 
  225.      *            输出流 
  226.      * @throws Exception 
  227.      */  
  228.     public static void encode(String content, OutputStream output)  
  229.             throws Exception {  
  230.         QRCodeUtil.encode(content, null, output, false);  
  231.     }  
  232.   
  233.     /** 
  234.      * 解析二维码 
  235.      *  
  236.      * @param file 
  237.      *            二维码图片 
  238.      * @return 
  239.      * @throws Exception 
  240.      */  
  241.     public static String decode(File file) throws Exception {  
  242.         BufferedImage image;  
  243.         image = ImageIO.read(file);  
  244.         if (image == null) {  
  245.             return null;  
  246.         }  
  247.         BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(  
  248.                 image);  
  249.         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
  250.         Result result;  
  251.         Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();  
  252.         hints.put(DecodeHintType.CHARACTER_SET, CHARSET);  
  253.         result = new MultiFormatReader().decode(bitmap, hints);  
  254.         String resultStr = result.getText();  
  255.         return resultStr;  
  256.     }  
  257.   
  258.     /** 
  259.      * 解析二维码 
  260.      *  
  261.      * @param path 
  262.      *            二维码图片地址 
  263.      * @return 
  264.      * @throws Exception 
  265.      */  
  266.     public static String decode(String path) throws Exception {  
  267.         return QRCodeUtil.decode(new File(path));  
  268.     }  
  269.   
  270.     public static void main(String[] args) throws Exception {  
  271.         String text = "薯 灯可分列式本上楞珂要瓜熟蒂落!000000000000000";  
  272.         QRCodeUtil.encode(text, "c:/df.jsp""c:/a/"true);  
  273.     }  
  274. }  

 

 

 

 

  1.   
  1. package com.util.cccm;  
  2.   
  3. import java.awt.Graphics2D;  
  4. import java.awt.geom.AffineTransform;  
  5. import java.awt.image.BufferedImage;  
  6.   
  7. import com.google.zxing.LuminanceSource;  
  8.   
  9. public class BufferedImageLuminanceSource extends LuminanceSource {  
  10.     private final BufferedImage image;  
  11.     private final int left;  
  12.     private final int top;  
  13.   
  14.     public BufferedImageLuminanceSource(BufferedImage image) {  
  15.         this(image, 00, image.getWidth(), image.getHeight());  
  16.     }  
  17.   
  18.     public BufferedImageLuminanceSource(BufferedImage image, int left,  
  19.             int top, int width, int height) {  
  20.         super(width, height);  
  21.   
  22.         int sourceWidth = image.getWidth();  
  23.         int sourceHeight = image.getHeight();  
  24.         if (left + width > sourceWidth || top + height > sourceHeight) {  
  25.             throw new IllegalArgumentException(  
  26.                     "Crop rectangle does not fit within image data.");  
  27.         }  
  28.   
  29.         for (int y = top; y < top + height; y++) {  
  30.             for (int x = left; x < left + width; x++) {  
  31.                 if ((image.getRGB(x, y) & 0xFF000000) == 0) {  
  32.                     image.setRGB(x, y, 0xFFFFFFFF); // = white  
  33.                 }  
  34.             }  
  35.         }  
  36.   
  37.         this.image = new BufferedImage(sourceWidth, sourceHeight,  
  38.                 BufferedImage.TYPE_BYTE_GRAY);  
  39.         this.image.getGraphics().drawImage(image, 00null);  
  40.         this.left = left;  
  41.         this.top = top;  
  42.     }  
  43.   
  44.       
  45.     public byte[] getRow(int y, byte[] row) {  
  46.         if (y < 0 || y >= getHeight()) {  
  47.             throw new IllegalArgumentException(  
  48.                     "Requested row is outside the image: " + y);  
  49.         }  
  50.         int width = getWidth();  
  51.         if (row == null || row.length < width) {  
  52.             row = new byte[width];  
  53.         }  
  54.         image.getRaster().getDataElements(left, top + y, width, 1, row);  
  55.         return row;  
  56.     }  
  57.   
  58.       
  59.     public byte[] getMatrix() {  
  60.         int width = getWidth();  
  61.         int height = getHeight();  
  62.         int area = width * height;  
  63.         byte[] matrix = new byte[area];  
  64.         image.getRaster().getDataElements(left, top, width, height, matrix);  
  65.         return matrix;  
  66.     }  
  67.   
  68.       
  69.     public boolean isCropSupported() {  
  70.         return true;  
  71.     }  
  72.   
  73.       
  74.     public LuminanceSource crop(int left, int top, int width, int height) {  
  75.         return new BufferedImageLuminanceSource(image, this.left + left,  
  76.                 this.top + top, width, height);  
  77.     }  
  78.   
  79.       
  80.     public boolean isRotateSupported() {  
  81.         return true;  
  82.     }  
  83.   
  84.       
  85.     public LuminanceSource rotateCounterClockwise() {  
  86.         int sourceWidth = image.getWidth();  
  87.         int sourceHeight = image.getHeight();  
  88.         AffineTransform transform = new AffineTransform(0.0, -1.01.0,  
  89.                 0.00.0, sourceWidth);  
  90.         BufferedImage rotatedImage = new BufferedImage(sourceHeight,  
  91.                 sourceWidth, BufferedImage.TYPE_BYTE_GRAY);  
  92.         Graphics2D g = rotatedImage.createGraphics();  
  93.         g.drawImage(image, transform, null);  
  94.         g.dispose();  
  95.         int width = getWidth();  
  96.         return new BufferedImageLuminanceSource(rotatedImage, top,  
  97.                 sourceWidth - (left + width), getHeight(), width);  
  98.     }  
  99. }  


 

 

 

 

 

 源码和包下载:http://download.csdn.net/detail/mmm333zzz/6695793

 



麻城创捷信息技术有限公司,专注于App,微信,网站,企业信息系统开发,专业承接软件项目开发。

麻城创捷官网 http://www.foundjet.com



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值