}
}
return image;
}
/**
-
将字符串编成一维条码的矩阵
-
@param str
-
@param width
-
@param height
-
@return
*/
public static BitMatrix toBarCodeMatrix(String str, Integer width,
Integer height) {
if (width == null || width < 200) {
width = 200;
}
if (height == null || height < 50) {
height = 50;
}
try {
// 文字编码
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
BitMatrix bitMatrix = new MultiFormatWriter().encode(str,
BarcodeFormat.CODE_128, width, height, hints);
return bitMatrix;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
- 根据矩阵、图片格式,生成文件。
*/
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage 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(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format "
- format);
}
}
/**
-
解码,需要javase包。
-
@param file
-
@return
*/
/*public static String decode(File file) {
BufferedImage image;
try {
if (file == null || file.exists() == false) {
throw new Exception(" File not found:" + file.getPath());
}
image = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(image);