[Java] Java利用Zxing生成二维码 →→→→→进入此内容的聊天室

来自 , 2019-05-25, 写在 Java, 查看 103 次.
URL http://www.code666.cn/view/1d54c76f
  1.    
  2.  
  3.  
  4. import com.google.zxing.common.BitMatrix;
  5.  
  6.  import javax.imageio.ImageIO;
  7.  import java.io.File;
  8.  import java.io.OutputStream;
  9.  import java.io.IOException;
  10.  import java.awt.image.BufferedImage;
  11.  
  12.  
  13.  public final class MatrixToImageWriter {
  14.  
  15.    private static final int BLACK = 0xFF000000;
  16.    private static final int WHITE = 0xFFFFFFFF;
  17.  
  18.    private MatrixToImageWriter() {}
  19.  
  20.    
  21.    public 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.    
  34.    public static void writeToFile(BitMatrix matrix, String format, File file)
  35.        throws IOException {
  36.      BufferedImage image = toBufferedImage(matrix);
  37.      if (!ImageIO.write(image, format, file)) {
  38.        throw new IOException("Could not write an image of format " + format + " to " + file);
  39.      }
  40.    }
  41.  
  42.    
  43.    public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
  44.        throws IOException {
  45.      BufferedImage image = toBufferedImage(matrix);
  46.      if (!ImageIO.write(image, format, stream)) {
  47.        throw new IOException("Could not write an image of format " + format);
  48.      }
  49.    }
  50.  
  51.  }
  52.  
  53.  
  54. //java/7297

回复 "Java利用Zxing生成二维码"

这儿你可以回复上面这条便签

captcha