熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java利用Zxing生成二維碼

2022-06-13   來源: Java核心技術 

  Java利用Zxing生成二維碼

  Zxing是Google提供的關於條碼(一維碼二維碼)的解析工具提供了二維碼的生成與解析的方法現在我簡單介紹一下使用Java利用Zxing生成與解析二維碼

  二維碼的生成

   將Zxingcorejar 包加入到classpath下

   二維碼的生成需要借助MatrixToImageWriter類該類是由Google提供的可以將該類拷貝到源碼中這裡我將該類的源碼貼上可以直接使用

  import monBitMatrix;

  import javaximageioImageIO;

  import javaioFile;

  import javaioOutputStream;

  import javaioIOException;

  import javaawtimageBufferedImage;

  public final class MatrixToImageWriter {

  private static final int BLACK = xFF;

  private static final int WHITE = xFFFFFFFF;

  private MatrixToImageWriter() {}

  public static BufferedImage toBufferedImage(BitMatrix matrix) {

  int width = matrixgetWidth();

  int height = matrixgetHeight();

  BufferedImage image = new BufferedImage(width height BufferedImageTYPE_INT_RGB);

  for (int x = ; x < width; x++) {

  for (int y = ; y < height; y++) {

  imagesetRGB(x y matrixget(x y) ? BLACK : WHITE);

  }

  }

  return image;

  }

  public static void writeToFile(BitMatrix matrix String format File file)

  throws IOException {

  BufferedImage image = toBufferedImage(matrix);

  if (!ImageIOwrite(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 (!ImageIOwrite(image format stream)) {

  throw new IOException(Could not write an image of format + format);

  }

  }

  }

   編寫生成二維碼的實現代碼

  try {

  String content = /jtmjx;

  String path = C:/Users/Administrator/Desktop/testImage;

  MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

  Map hints = new HashMap();

  hintsput(EncodeHintTypeCHARACTER_SET UTF);

  BitMatrix bitMatrix = multiFormatWriterencode(content BarcodeFormatQR_CODE hints);

  File file = new File(path餐巾紙jpg);

  MatrixToImageWriterwriteToFile(bitMatrix jpg file);

  } catch (Exception e) {

  eprintStackTrace();

  }

  現在運行後即可生成一張二維碼圖片是不是很簡單啊? 接下來我們看看如何解析二維碼

  二維碼的解析

   將Zxingcorejar 包加入到classpath下

   和生成一樣我們需要一個輔助類( BufferedImageLuminanceSource)同樣該類Google也提供了這裡我同樣將該類的源碼貼出來可以直接拷貝使用個省去查找的麻煩

  BufferedImageLuminanceSource

  import comgooglezxingLuminanceSource;

  import javaawtGraphicsD;

  import javaawtgeomAffineTransform;

  import javaawtimageBufferedImage;

  public final class BufferedImageLuminanceSource extends LuminanceSource {

  private final BufferedImage image;

  private final int left;

  private final int top;

  public BufferedImageLuminanceSource(BufferedImage image) {

  this(image imagegetWidth() imagegetHeight());

  }

  public BufferedImageLuminanceSource(BufferedImage image int left int top int width int height) {

  super(width height);

  int sourceWidth = imagegetWidth();

  int sourceHeight = imagegetHeight();

  if (left + width > sourceWidth || top + height > sourceHeight) {

  throw new IllegalArgumentException(Crop rectangle does not fit within image data);

  }

  for (int y = top; y < top + height; y++) {

  for (int x = left; x < left + width; x++) {

  if ((imagegetRGB(x y) & xFF) == ) {

  imagesetRGB(x y xFFFFFFFF); // = white

  }

  }

  }

  thisimage = new BufferedImage(sourceWidth sourceHeight BufferedImageTYPE_BYTE_GRAY);

  thisimagegetGraphics()drawImage(image null);

  thisleft = left;

  thistop = top;

  }

  @Override

  public byte[] getRow(int y byte[] row) {

  if (y < || y >= getHeight()) {

  throw new IllegalArgumentException(Requested row is outside the image: + y);

  }

  int width = getWidth();

  if (row == null || rowlength < width) {

  row = new byte[width];

  }

  imagegetRaster()getDataElements(left top + y width row);

  return row;

  }

  @Override

  public byte[] getMatrix() {

  int width = getWidth();

  int height = getHeight();

  int area = width * height;

  byte[] matrix = new byte[area];

  imagegetRaster()getDataElements(left top width height matrix);

  return matrix;

  }

  @Override

  public boolean isCropSupported() {

  return true;

  }

  @Override

  public LuminanceSource crop(int left int top int width int height) {

  return new BufferedImageLuminanceSource(image thisleft + left thistop + top width height);

  }

  @Override

  public boolean isRotateSupported() {

  return true;

  }

  @Override

  public LuminanceSource rotateCounterClockwise() {

  int sourceWidth = imagegetWidth();

  int sourceHeight = imagegetHeight();

  AffineTransform transform = new AffineTransform( sourceWidth);

  BufferedImage rotatedImage = new BufferedImage(sourceHeight sourceWidth BufferedImageTYPE_BYTE_GRAY);

  GraphicsD g = rotatedImagecreateGraphics();

  gdrawImage(image transform null);

  gdispose();

  int width = getWidth();

  return new BufferedImageLuminanceSource(rotatedImage top sourceWidth (left + width) getHeight() width);

  }

  }

   編寫解析二維碼的實現代碼

  try {

  MultiFormatReader formatReader = new MultiFormatReader();

  String filePath = C:/Users/Administrator/Desktop/testImage/testjpg;

  File file = new File(filePath);

  BufferedImage image = ImageIOread(file);;

  LuminanceSource source = new BufferedImageLuminanceSource(image);

  Binarizer  binarizer = new HybridBinarizer(source);

  BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

  Map hints = new HashMap();

  hintsput(EncodeHintTypeCHARACTER_SET UTF);

  Result result = formatReaderdecode(binaryBitmaphints);

  Systemoutprintln(result = + resulttoString());

  Systemoutprintln(resultFormat = + resultgetBarcodeFormat());

  Systemoutprintln(resultText = + resultgetText());

  } catch (Exception e) {

  eprintStackTrace();

  }

  現在運行後可以看到控制台打印出了二維碼的內容


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25967.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.