码迷,mamicode.com
首页 > 编程语言 > 详细

java 二维码 工具类

时间:2017-09-26 13:36:39      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:div   buffered   turn   buffere   matrix   set   http   str   技术分享   

   private static final int BLACK = 0xFF000000;
   private static final int WHITE = 0xFFFFFFFF;
  
   private MatrixToImageWriter() {}
  
    
   public static BufferedImage toBufferedImage(BitMatrix matrix) {
     int width = matrix.getWidth();
     int height = matrix.getHeight();
     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     for (int x = 0; x < width; x++) {
       for (int y = 0; y < height; y++) {
         image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
       }
     }
     return image;
   }
  
    
   public static void writeToFile(BitMatrix matrix, String format, File file)
       throws IOException {
     BufferedImage image = toBufferedImage(matrix);
     if (!ImageIO.write(image, format, file)) {
       throw new IOException("Could not write an image of format " + format + " to " + file);
     }
   }
  
    
   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
       throws IOException {
     BufferedImage image = toBufferedImage(matrix);
     if (!ImageIO.write(image, format, stream)) {
       throw new IOException("Could not write an image of format " + format);
     }
   }

技术分享

java 二维码 工具类

标签:div   buffered   turn   buffere   matrix   set   http   str   技术分享   

原文地址:http://www.cnblogs.com/dragonetyu/p/7596269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!