Java利用Zxing生成二維碼
Zxing是Google提供的關於條碼(一維碼
import mon
import javax
import java
import java
import java
import java
public final class MatrixToImageWriter {
private static final int BLACK =
private static final int WHITE =
private MatrixToImageWriter() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix
int height = matrix
BufferedImage image = new BufferedImage(width
for (int x =
for (int y =
image
}
}
return image;
}
public static void writeToFile(BitMatrix matrix
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO
throw new IOException(
}
}
public static void writeToStream(BitMatrix matrix
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO
throw new IOException(
}
}
}
try {
String content =
String path =
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints
BitMatrix bitMatrix = multiFormatWriter
File file
MatrixToImageWriter
} catch (Exception e) {
e
}
現在運行後即可生成一張二維碼圖片
BufferedImageLuminanceSource
import com
import java
import java
import java
public final class BufferedImageLuminanceSource extends LuminanceSource {
private final BufferedImage image;
private final int left;
private final int top;
public BufferedImageLuminanceSource(BufferedImage image) {
this(image
}
public BufferedImageLuminanceSource(BufferedImage image
super(width
int sourceWidth = image
int sourceHeight = image
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException(
}
for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image
image
}
}
}
this
this
this
this
}
@Override
public byte[] getRow(int y
if (y <
throw new IllegalArgumentException(
}
int width = getWidth();
if (row == null || row
row = new byte[width];
}
image
return row;
}
@Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image
return matrix;
}
@Override
public boolean isCropSupported() {
return true;
}
@Override
public LuminanceSource crop(int left
return new BufferedImageLuminanceSource(image
}
@Override
public boolean isRotateSupported() {
return true;
}
@Override
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image
int sourceHeight = image
AffineTransform transform = new AffineTransform(
BufferedImage rotatedImage = new BufferedImage(sourceHeight
Graphics
g
g
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage
}
}
try {
MultiFormatReader formatReader = new MultiFormatReader();
String filePath =
File file = new File(filePath);
BufferedImage image = ImageIO
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map hints = new HashMap();
hints
Result result = formatReader
System
System
System
} catch (Exception e) {
e
}
現在運行後可以看到控制台打印出了二維碼的內容
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25967.html