二维码--Google二维码使用(java版)

二维码--Google二维码使用(java版)


下载jar


http://mvnrepository.com/artifact/com.google.zxing/core/2.2



package com.haier.util.qr;


 


import java.awt.image.BufferedImage;


import java.io.File;


import java.io.IOException;


import java.io.OutputStream;


import javax.imageio.ImageIO;


import com.google.zxing.common.BitMatrix;


 


public class MatrixToImageWriter {


         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);


                  }


         }


 


}


 


 


 


 


package com.haier.util.qr;


 


import java.io.File;


import java.io.OutputStream;


import java.util.Hashtable;


import com.google.zxing.BarcodeFormat;


import com.google.zxing.EncodeHintType;


import com.google.zxing.MultiFormatWriter;


import com.google.zxing.common.BitMatrix;


import com.test.MatrixToImageWriter;


 


public class QRUtil {


 


         public static void create(String text, OutputStream stream, Integer size)


                           throws Exception {


                  int width = 100 + (10 * size);


                  int height = 100 + (10 * size);


                  // 二维码的图片格式


                  String format = "jpg";


                  Hashtable hints = new Hashtable();


                  // 内容所使用编码


                  hints.put(EncodeHintType.CHARACTER_SET, "utf-8");


                  BitMatrix bitMatrix = new MultiFormatWriter().encode(text,


                                    BarcodeFormat.QR_CODE, width, height, hints);


                  // 1.1去白边


                  int[] rec = bitMatrix.getEnclosingRectangle();


                  int resWidth = rec[2] + 1;


                  int resHeight = rec[3] + 1;


                  BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);


                  resMatrix.clear();


                  for (int i = 0; i < resWidth; i++) {


                           for (int j = 0; j < resHeight; j++) {


                                    if (bitMatrix.get(i + rec[0], j + rec[1])) {


                                             resMatrix.set(i, j);


                                    }


                           }


                  }


                  MatrixToImageWriter.writeToStream(resMatrix, format, stream);


         }


 


         public static void main(String[] args) throws Exception {


                  String text = "http://www.baidu.com/action/page/formSub.html?mac=1111111111111111111111111111111111111111111111111111111";


                  int width = 300;


                  int height = 300;


                  // 二维码的图片格式


                  String format = "jpg";


                  Hashtable hints = new Hashtable();


                  // 内容所使用编码


                  hints.put(EncodeHintType.CHARACTER_SET, "utf-8");


                  BitMatrix bitMatrix = new MultiFormatWriter().encode(text,


                                    BarcodeFormat.QR_CODE, width, height, hints);


                  // 生成二维码


                  File outputFile = new File("d:" + File.separator + "new.jpg");


                  MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);


         }


}


 


 


  • 发表于 2017-11-24 16:02
  • 阅读 ( 1631 )
  • 分类:Java

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
不写代码的码农
HJ社区-肖峰

IT

29 篇文章

作家榜 »

  1. 威猛的小站长 124 文章
  2. Jonny 65 文章
  3. 江南烟雨 36 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 22 文章
  8. Alan 14 文章