package com.cyberway.Encoder.util;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.p_w_picpathio.ImageIO;
import javax.p_w_picpathio.stream.ImageOutputStream;
import java.awt.p_w_picpath.BufferedImage;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.jcraft.jsch.jce.Random;
/**
*
* @author lijinshan
* @createTime 2015年10月26日 下午4:10:30
*/
public class EncoderHandler{
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private EncoderHandler() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage p_w_picpath = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
p_w_picpath.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return p_w_picpath;
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage p_w_picpath = toBufferedImage(matrix);
if (!ImageIO.write(p_w_picpath, format, file)) {
throw new IOException("Could not write an p_w_picpath of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage p_w_picpath = toBufferedImage(matrix);
if (!ImageIO.write(p_w_picpath, format, stream)) {
throw new IOException("Could not write an p_w_picpath of format " + format);
}
}
public static void main(String[] args) throws WriterException, IOException {
String text = "单号:2030011150614143706938 收件人:小李";
int width = 300;
int height = 300;
//二维码的图片格式
String format = "jpg";
Hashtable hints = new Hashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
//生成二维码
File outputFile = new File("f:"+File.separator+"lijinshan.jpg");
EncoderHandler.writeToFile(bitMatrix, format, outputFile);
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。