楼主做报表,用的ireport,需要用到条形码,由于条码下方的字体用到OCR-B 10 BT这个字体,
外部写一个条码工具集成到ireport.
barcode4j/zxing 下方的条码字体需要自己画上去
但是Font加载OCR-B 10 BT.ttf字体后,测试了下输出效果,结果图片的字体无法正常显示。
public static String create(String str, String filePath, int width, int height) throws Exception{
String fileName = System.currentTimeMillis() + ".jpg";
String path = filePath + "/" + fileName;
File file = new File(path);
BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
String ttf = "C:/Users/Administrator/Desktop/OCR-B 10 BT/OCR-B 10 BT.ttf";
// Font font = new Font("OCR-B 10 Pitch BT", Font.BOLD, 25);//OCR-B 10 Pitch BT
Font font = Font.createFont(Font.TRUETYPE_FONT, new File(ttf));
font = font.deriveFont(Font.BOLD, 12); // 粗体比Font.PLAIN清晰
// FileInputStream aixing = new FileInputStream(new File("C:/Users/Administrator/Desktop/tt0646z_/tt0646z_.ttf"));
// Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
// Font font = dynamicFont.deriveFont(25);
g2.setFont(font);
g2.setPaint(Color.RED);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(str, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.drawString(str, (int) x, (int) baseY);
try {
ImageIO.write(bi, "png", file);
} catch (IOException e) {
e.printStackTrace();
}finally {
// fb.close();
// fi.close();
}
return file.getPath();
}