我正在尝试转换通过barcode4j 生成的条形码图像,但无法这样做。当我使用 FileOutputStream 在本地路径中生成图像时,它按预期工作。但是当使用 ByteArrayOutputStream 将其转换为 base64 字符串时,我什么也得不到。我的代码有问题吗?
public void testNothing() throws FileNotFoundException, UnsupportedEncodingException{
Code39Bean bean = new Code39Bean();
int resolution = 150;
bean.setModuleWidth(UnitConv.in2mm(1.0f / resolution)); //makes the narrow bar
bean.setWideFactor(3);
bean.doQuietZone(false);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
BitmapCanvasProvider canvas = new BitmapCanvasProvider(
out, "image/x-png", resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
bean.generateBarcode(canvas, "1234");
System.out.println("Generating Base64");
// Base64Encoder encode= new Base64Encoder();
String imgString = new String(Base64Encoder.encode(out.toByteArray()));
System.out.println("String Generated :"+ imgString);
try {
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
canvas.finish();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出
Generating Base64
String Generated :