您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。