欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java利用Zxing生成二维码的简单实例

程序员文章站 2024-03-13 14:28:39
zxing是google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用java利用zxing生成与解析二维码 1、二维...

zxing是google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用java利用zxing生成与解析二维码

1、二维码的生成

1.1 将zxing-core.jar 包加入到classpath下。

1.2 二维码的生成需要借助matrixtoimagewriter类,该类是由google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用。

import com.google.zxing.common.bitmatrix;
 
 import javax.imageio.imageio;
 import java.io.file;
 import java.io.outputstream;
 import java.io.ioexception;
 import java.awt.image.bufferedimage;
 
 
 public final 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);
   }
  }
 
 }

1.3 编写生成二维码的实现代码

try {
       
   string content = "120605181003;http://www.cnblogs.com/jtmjx";
   string path = "c:/users/administrator/desktop/testimage";
   
   multiformatwriter multiformatwriter = new multiformatwriter();
   
   map hints = new hashmap();
   hints.put(encodehinttype.character_set, "utf-8");
   bitmatrix bitmatrix = multiformatwriter.encode(content, barcodeformat.qr_code, 400, 400,hints);
   file file1 = new file(path,"餐巾纸.jpg");
   matrixtoimagewriter.writetofile(bitmatrix, "jpg", file1);
   
 } catch (exception e) {
   e.printstacktrace();
 }

现在运行后即可生成一张二维码图片,是不是很简单啊? 接下来我们看看如何解析二维码

2、二维码的解析

2.1 将zxing-core.jar 包加入到classpath下。  

2.2 和生成一样,我们需要一个辅助类( bufferedimageluminancesource),同样该类google也提供了,这里我同样将该类的源码贴出来,可以直接拷贝使用个,省去查找的麻烦

bufferedimageluminancesource 
 import com.google.zxing.luminancesource;
 
 import java.awt.graphics2d;
 import java.awt.geom.affinetransform;
 import java.awt.image.bufferedimage;
 
 public final class bufferedimageluminancesource extends luminancesource {
 
  private final bufferedimage image;
  private final int left;
  private final int top;
 
  public bufferedimageluminancesource(bufferedimage image) {
   this(image, 0, 0, image.getwidth(), image.getheight());
  }
 
  public bufferedimageluminancesource(bufferedimage image, int left, int top, int width, int height) {
   super(width, height);
 
   int sourcewidth = image.getwidth();
   int sourceheight = image.getheight();
   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 ((image.getrgb(x, y) & 0xff000000) == 0) {
      image.setrgb(x, y, 0xffffffff); // = white
     }
    }
   }
 
   this.image = new bufferedimage(sourcewidth, sourceheight, bufferedimage.type_byte_gray);
   this.image.getgraphics().drawimage(image, 0, 0, null);
   this.left = left;
   this.top = top;
  }
 
  @override
  public byte[] getrow(int y, byte[] row) {
   if (y < 0 || y >= getheight()) {
    throw new illegalargumentexception("requested row is outside the image: " + y);
   }
   int width = getwidth();
   if (row == null || row.length < width) {
    row = new byte[width];
   }
   image.getraster().getdataelements(left, top + y, width, 1, row);
   return row;
  }
 
  @override
  public byte[] getmatrix() {
   int width = getwidth();
   int height = getheight();
   int area = width * height;
   byte[] matrix = new byte[area];
   image.getraster().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, this.left + left, this.top + top, width, height);
  }
 
  @override
  public boolean isrotatesupported() {
   return true;
  }
 
  @override
  public luminancesource rotatecounterclockwise() {
 
    int sourcewidth = image.getwidth();
   int sourceheight = image.getheight();
 
   affinetransform transform = new affinetransform(0.0, -1.0, 1.0, 0.0, 0.0, sourcewidth);
 
   bufferedimage rotatedimage = new bufferedimage(sourceheight, sourcewidth, bufferedimage.type_byte_gray);
 
   graphics2d g = rotatedimage.creategraphics();
   g.drawimage(image, transform, null);
   g.dispose();
 
   int width = getwidth();
   return new bufferedimageluminancesource(rotatedimage, top, sourcewidth - (left + width), getheight(), width);
  }
 
 }

2.3 编写解析二维码的实现代码

try {
             multiformatreader formatreader = new multiformatreader();
       string filepath = "c:/users/administrator/desktop/testimage/test.jpg";
       file file = new file(filepath);
       bufferedimage image = imageio.read(file);;
       luminancesource source = new bufferedimageluminancesource(image);
       binarizer binarizer = new hybridbinarizer(source);
       binarybitmap binarybitmap = new binarybitmap(binarizer);
       map hints = new hashmap();
       hints.put(encodehinttype.character_set, "utf-8");
       result result = formatreader.decode(binarybitmap,hints);
       
             system.out.println("result = "+ result.tostring());
       system.out.println("resultformat = "+ result.getbarcodeformat());
       system.out.println("resulttext = "+ result.gettext());
             
     } catch (exception e) {
       e.printstacktrace();
     }

现在运行后可以看到控制台打印出了二维码的内容。

到此为止,利用zxing生成和解析二维码就讲述演示完毕,主要为自己做备忘,同时方便有需要的人。呵呵!

以上这篇java利用zxing生成二维码的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。