google zxing作为二维码生成工具

[java] view plain copy
  1. import com.google.zxing.BarcodeFormat;  
[java] view plain copy
  1. import com.google.zxing.EncodeHintType;  
  2. import com.google.zxing.MultiFormatWriter;  
  3. import com.google.zxing.common.BitMatrix;  
  4. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  
  5. import org.apache.commons.logging.Log;  
  6. import org.apache.commons.logging.LogFactory;  
  7.   
  8. import javax.imageio.ImageIO;  
  9. import java.awt.image.BufferedImage;  
  10. import java.io.File;  
  11. import java.io.IOException;  
  12. import java.util.Hashtable;  
  13. import java.util.Map;  
  14.   
  15. public class ZxingUtils {  
  16.     private static Log log = LogFactory.getLog(ZxingUtils.class);  
  17.   
  18.     private static final int BLACK = 0xFF000000;  
  19.     private static final int WHITE = 0xFFFFFFFF;  
  20.   
  21.     private static BufferedImage toBufferedImage(BitMatrix matrix) {  
  22.         int width = matrix.getWidth();  
  23.         int height = matrix.getHeight();  
  24.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  25.         for (int x = 0; x < width; x++) {  
  26.             for (int y = 0; y < height; y++) {  
  27.                 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);  
  28.             }  
  29.         }  
  30.         return image;  
  31.     }  
  32.   
  33.     private static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {  
  34.         BufferedImage image = toBufferedImage(matrix);  
  35.         if (!ImageIO.write(image, format, file)) {  
  36.             throw new IOException("Could not write an image of format " + format + " to " + file);  
  37.         }  
  38.     }  
  39.   
  40.     /** 将内容contents生成长宽均为width的图片,图片路径由imgPath指定 
  41.      */  
  42.     public static File getQRCodeImge(String contents, int width, String imgPath) {  
  43.         return getQRCodeImge(contents, width, width, imgPath);  
  44.     }  
  45.   
  46.     /** 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定 
  47.      */  
  48.     public static File getQRCodeImge(String contents, int width, int height, String imgPath) {  
  49.         try {  
  50.             Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();  
  51.             hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);  
  52.             hints.put(EncodeHintType.CHARACTER_SET, "UTF8");  
  53.   
  54.             BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);  
  55.   
  56.             File imageFile = new File(imgPath);  
  57.             writeToFile(bitMatrix, "png", imageFile);  
  58.   
  59.             return imageFile;  
  60.   
  61.         } catch (Exception e) {  
  62.             log.error("create QR code error!", e);  
  63.             return null;  
  64.         }  
  65.     }  
  66. }  

猜你喜欢

转载自blog.csdn.net/weixin_39816740/article/details/80512716
今日推荐