java生成二维码、条形码和二维码、条形码的解码
首先,使用java生成二维码和条形码需要ZXing
ZXing是一个开源Java类库用于解析多种格式的条形码和二维码.
官网:http://code.google.com/p/zxing/
MatrixToImageWriter类辅助生成二维码,该类是由Google提供的,我们可以把它复制到自己的项目中使用:
下面是MatrixToImageWriter类的源代码:
-
package learn;
-
-
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 WriteBitMatricToFile {
-
private static final int BLACK = 0xFF000000;
-
private static final int WHITE = 0xFFFFFFFF;
-
-
private static BufferedImage toBufferedImage(BitMatrix bm) {
-
int width = bm.getWidth();
-
int height = bm.getHeight();
-
BufferedImage image = new BufferedImage(width, height,
-
BufferedImage.TYPE_3BYTE_BGR);
-
for ( int i = 0; i < width; i++) {
-
for ( int j = 0; j < height; j++) {
-
image.setRGB(i, j, bm.get(i, j) ? BLACK : WHITE);
-
}
-
}
-
return image;
-
}
-
-
public static void writeBitMatricToFile(BitMatrix bm, String format,
-
File file) {
-
BufferedImage image = toBufferedImage(bm);
-
try {
-
if (!ImageIO.write(image, format, file)) {
-
throw new RuntimeException( "Can not write an image to file" + file);
-
}
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
-
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);
-
}
-
}
-
}
生成二维码和条形码的格式不同,二维码的编码格式是二维码的格式是BarcodeFormat.QR_CODE,条形码的编码格式是条形码的格式是 BarcodeFormat.EAN_13 ,下面是源代码:
-
package learn;
-
-
import java.io.File;
-
import java.util.HashMap;
-
-
import com.google.zxing.BarcodeFormat;
-
import com.google.zxing.EncodeHintType;
-
import com.google.zxing.MultiFormatWriter;
-
import com.google.zxing.common.BitMatrix;
-
-
public class EncodeTest {
-
public static void main(String[] args) throws Exception {
-
int width = 300;
-
int height = 300;
-
// int width = 105;
-
// int height = 50;
-
// 条形码的输入是13位的数字
-
// String text = "6923450657713";
-
// 二维码的输入是字符串
-
String text = "testtesttest生成条形码图片";
-
String format = "png";
-
HashMap<EncodeHintType, String> hints = new HashMap<>();
-
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
-
// 条形码的格式是 BarcodeFormat.EAN_13
-
// 二维码的格式是BarcodeFormat.QR_CODE
-
BitMatrix bm = new MultiFormatWriter().encode(text,
-
BarcodeFormat.QR_CODE, width, height, hints);
-
-
File out = new File( "new.png");
-
// 生成条形码图片
-
// File out = new File("ean3.png");
-
-
WriteBitMatricToFile.writeBitMatricToFile(bm, format, out);
-
}
-
-
}
以下是生成的二维码和条形码:
条形码的输入是6923450657713
解码的过程就是二值化使用Binarizer实现,一维码使用getBlackRow方法,二维码的使用getBlackMatrix方法。Binarizer有两个生成类,GlobalHistogramBinarizer和HybridBinarizer;这两个类对getBlackMatrix方法的实现有不同,但使用时都是一样的没什么分别。
如果对解码过程感兴趣的话,可以阅读:http://blog.csdn.net/mihenyinghua/article/details/17224019
解码时需要zxing-j2se.jar,将jar添加到classpath下。
-
package learn;
-
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.util.HashMap;
-
import java.util.Map;
-
-
import javax.imageio.ImageIO;
-
-
import com.google.zxing.BinaryBitmap;
-
import com.google.zxing.DecodeHintType;
-
import com.google.zxing.LuminanceSource;
-
import com.google.zxing.MultiFormatReader;
-
import com.google.zxing.Result;
-
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
-
import com.google.zxing.common.GlobalHistogramBinarizer;
-
-
public class DecodeTest {
-
public static void main(String[] args) throws Exception {
-
// 这里可以是条形图片码或者二维码图片
-
// 这是二维码图片
-
BufferedImage bi = ImageIO.read( new FileInputStream( new File( "new.png")));
-
// 这是条形码图片
-
// BufferedImage bi = ImageIO.read(new FileInputStream(new
-
// File("ean3.png")));
-
if (bi != null) {
-
Map<DecodeHintType, String> hints = new HashMap<>();
-
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
-
LuminanceSource source = new BufferedImageLuminanceSource(bi);
-
// 这里还可以是 BinaryBitmap bitmap = new BinaryBitmap(new
-
// HybridBinarizer(source));
-
BinaryBitmap bitmap = new BinaryBitmap(
-
new GlobalHistogramBinarizer(source));
-
Result res = new MultiFormatReader().decode(bitmap, hints);
-
System.out.println(res);
-
System.out.println( "decode successfully!");
-
}
-
}
-
}
参考资料:http://blog.csdn.net/mihenyinghua/article/details/17224019