1、Java使用Zxing包制作二维码域名 长度 后缀 删除日期 删除类型 33d.cc 3 cc 2012/9/4 Delete 今天朋友问我一个二维码的东西,说实话我重来也没接触过,于是上网找了一下,和朋友分享一下.用qrcode.jar也能做,但是一看小日本的,于是马上闪过,听所zxing挺好用的,于是就去google下了架包用了这个感觉确实简单,重XX找到写完一个例子只用了,10来分钟. /www.hake.ccZxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码1、二维码的生成1.
2、1 将core.jar 包加入到classpath下。 1.2 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用。java view plaincopyprint?import mon.BitMatrix; import javax.imageio.ImageIO; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.awt.image.BufferedImage;
3、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 i
4、mage = 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 BufferedIm
5、age 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
6、(matrix); if (!ImageIO.write(image, format, stream) throw new IOException(Could not write an image of format + format); import mon.BitMatrix; import javax.imageio.ImageIO; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.awt.image.BufferedImage; public final
7、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 Bu
8、fferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x width; x+) for (int y = 0; y 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;
9、 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
10、, byte row) if (y = getHeight() throw new IllegalArgumentException(Requested row is outside the image: + y); int width = getWidth(); if (row = null | row.length sourceWidth | top + height sourceHeight) throw new IllegalArgumentException(Crop rectangle does not fit within image data.); for (int y = t
11、op; 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
12、 = left; this.top = top; Overridepublic byte getRow(int y, byte row) if (y = getHeight() throw new IllegalArgumentException(Requested row is outside the image: + y); int width = getWidth(); if (row = null | row.length width) row = new bytewidth; image.getRaster().getDataElements(left, top + y, width
13、, 1, row); return row; Overridepublic byte getMatrix() int width = getWidth(); int height = getHeight(); int area = width * height; byte matrix = new bytearea; image.getRaster().getDataElements(left, top, width, height, matrix); return matrix; Overridepublic boolean isCropSupported() return true; Ov
14、erridepublic LuminanceSource crop(int left, int top, int width, int height) return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height); Overridepublic boolean isRotateSupported() return true; Overridepublic 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, BufferedIm
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1