• java实现二维码


    说起二维码,微信好像最先启用,随后各类二维码就开始流行起来了。那什么是二维码呢。

    1.什么是二维码?百度一下即可

     http://baike.baidu.com/view/132241.htm?fr=aladdin

    2.java开发二维码?

      2.1:首先导入:此包

      下载地址:http://pan.baidu.com/s/1jGJwPwU

       

      2.2:java类

      

     1 import java.awt.image.BufferedImage;
     2 
     3 /**
     4  * Encapsulates custom configuration used in methods of {@link MatrixToImageWriter}.
     5  */
     6 public final class MatrixToImageConfig {
     7 
     8   public static final int BLACK = 0xFF000000;
     9   public static final int WHITE = 0xFFFFFFFF;
    10   
    11   private final int onColor;
    12   private final int offColor;
    13 
    14   /**
    15    * Creates a default config with on color {@link #BLACK} and off color {@link #WHITE}, generating normal
    16    * black-on-white barcodes.
    17    */
    18   public MatrixToImageConfig() {
    19     this(BLACK, WHITE);
    20   }
    21 
    22   /**
    23    * @param onColor pixel on color, specified as an ARGB value as an int
    24    * @param offColor pixel off color, specified as an ARGB value as an int
    25    */
    26   public MatrixToImageConfig(int onColor, int offColor) {
    27     this.onColor = onColor;
    28     this.offColor = offColor;
    29   }
    30 
    31   public int getPixelOnColor() {
    32     return onColor;
    33   }
    34 
    35   public int getPixelOffColor() {
    36     return offColor;
    37   }
    38 
    39   int getBufferedImageColorModel() {
    40     // Use faster BINARY if colors match default
    41     return onColor == BLACK && offColor == WHITE ? BufferedImage.TYPE_BYTE_BINARY : BufferedImage.TYPE_INT_RGB;
    42   }
    43 
    44 }
     1 import java.awt.image.BufferedImage;
     2 import java.io.File;
     3 import java.io.IOException;
     4 import java.io.OutputStream;
     5 
     6 import javax.imageio.ImageIO;
     7 
     8 import com.google.zxing.common.BitMatrix;
     9 public class MatrixToImageWriter {
    10      private static final int BLACK = 0xFF000000;
    11        private static final int WHITE = 0xFFFFFFFF;
    12      
    13        private MatrixToImageWriter() {}
    14      
    15        
    16        public static BufferedImage toBufferedImage(BitMatrix matrix) {
    17          int width = matrix.getWidth();
    18          int height = matrix.getHeight();
    19          BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    20          for (int x = 0; x < width; x++) {
    21            for (int y = 0; y < height; y++) {
    22              image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
    23            }
    24          }
    25          return image;
    26        }
    27      
    28        
    29        public static void writeToFile(BitMatrix matrix, String format, File file)
    30            throws IOException {
    31          BufferedImage image = toBufferedImage(matrix);
    32          if (!ImageIO.write(image, format, file)) {
    33            throw new IOException("Could not write an image of format " + format + " to " + file);
    34          }
    35        }
    36      
    37        
    38        public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
    39            throws IOException {
    40          BufferedImage image = toBufferedImage(matrix);
    41          if (!ImageIO.write(image, format, stream)) {
    42            throw new IOException("Could not write an image of format " + format);
    43          }
    44        }
    45 }

    test类:

     1 import java.io.File;
     2 import java.util.Hashtable;
     3 
     4 import com.google.zxing.BarcodeFormat;
     5 import com.google.zxing.EncodeHintType;
     6 import com.google.zxing.MultiFormatWriter;
     7 import com.google.zxing.common.BitMatrix;
     8 
     9 public class Test {
    10 
    11     public static void main(String[] args) throws Exception{
    12         String text = "http://www.baidu.com";//二维码的内容
    13         int width = 400;  
    14         int height = 400;  
    15         String format = "png";    
    16         Hashtable hints= new Hashtable();  
    17         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
    18         BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);  
    19         File outputFile = new File("E:/codeImg");  
    20         MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); 
    21         System.out.println("It is ok!");
    22 
    23     }
    24 }

    以上用的是一个谷歌提供的开源框架首先的,呵呵。。。站在巨人的肩膀上使用....

    效果图:

      

     

  • 相关阅读:
    Java实现单链表的各种操作
    LintCode #452 删除链表中的元素
    前两天做项目遇到了sqlserver最大连接数 Max Pool Size 的问题
    Redis的应用场景
    说一说MVC的Authentication过滤(四)
    MVC中的下载文件及上传
    Python之高级特性
    说一说MVC的MenuCard(五)
    说一说MVC的CSRF(三)
    说一说MVC的控制器(二)
  • 原文地址:https://www.cnblogs.com/huzi007/p/4087745.html
Copyright © 2020-2023  润新知