Java 将文本放置到图像上未显示
Java placing text onto image not showing up
大家好,我是 Java 世界的新手,所以这可能很容易解决,但我无法找出问题所在。
我想在我正在创建的图像上放置一些文本。问题是一旦图像被保存并且我看着它没有文字。
String tiffImg = "";
String imgPath = "";
BufferedImage image = null;
@SuppressWarnings("rawtypes")
protected boolean saveTiff(String filename, BufferedImage image) {
File tiffFile = new File(filename);
ImageOutputStream ios = null;
ImageWriter writer = null;
try {
Iterator it = ImageIO.getImageWritersByFormatName("TIF");
if (it.hasNext()) {
writer = (ImageWriter)it.next();
} else {
return false;
}
ios = ImageIO.createImageOutputStream(tiffFile);
imgPath = tiffFile.getAbsolutePath();
writer.setOutput(ios);
TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.ENGLISH);
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("PackBits");
IIOImage iioImage = new IIOImage(image, null, null);
writer.write(null, iioImage, writeParam);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static BufferedImage decodeToImage(String imageString) {
BufferedImage image = null;
byte[] imageByte;
try {
Base64.Decoder decoder = Base64.getDecoder();
imageByte = decoder.decode(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
public BufferedImage placeDataOnImage(BufferedImage image, String transNum) {
try {
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.drawString(transNum, 150, 1350);
g.drawString(transNum, 200, 2805);
g.drawString(transNum, 200, 4453);
g.drawString("DFDFFFF", 225, 590);
g.drawString("rrrr", 225, 675);
g.drawString("555-555-5555", 225, 718);
g.drawString("Jocelyn", 767, 597);
g.drawString("1-877-324-6427, ext. #", 767, 665);
g.drawString("1-888-550-6427 (Toll Free)", 767, 701);
g.drawString("11/28/18", 276, 800);
g.drawString("11/28/18", 718, 799);
g.drawString("2", 270, 837);
g.drawString("sdfsdfsdfsdfsdf", 176, 1880);
g.drawString("11/15/2018", 176, 1980);
g.dispose();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
public String exec(ITestExecutionServices tes, String[] args) {
boolean savedImg = false;
String currentTransID = args[0];
image = decodeToImage(tiffImg);
image = placeDataOnImage(image, currentTransID);
savedImg = saveTiff(currentTransID + ".tiff", image);
if (savedImg) {
tes.getTestLogManager().reportVerificationPoint("TIFF created with Transaction ID# " + currentTransID, VerdictEvent.VERDICT_PASS);
} else {
tes.getTestLogManager().reportVerificationPoint("TIFF could not be created", VerdictEvent.VERDICT_ERROR);
}
return null;
}
有人看到我哪里做错了吗?
Ugg...刚刚想到了什么。我没有指定文本的颜色,因此我认为它默认为白色 - 这就是我看不到它的全部原因。
我补充了:
g.setColor(Color.BLACK);
它神奇地出现在图像上了!去图....
大家好,我是 Java 世界的新手,所以这可能很容易解决,但我无法找出问题所在。
我想在我正在创建的图像上放置一些文本。问题是一旦图像被保存并且我看着它没有文字。
String tiffImg = "";
String imgPath = "";
BufferedImage image = null;
@SuppressWarnings("rawtypes")
protected boolean saveTiff(String filename, BufferedImage image) {
File tiffFile = new File(filename);
ImageOutputStream ios = null;
ImageWriter writer = null;
try {
Iterator it = ImageIO.getImageWritersByFormatName("TIF");
if (it.hasNext()) {
writer = (ImageWriter)it.next();
} else {
return false;
}
ios = ImageIO.createImageOutputStream(tiffFile);
imgPath = tiffFile.getAbsolutePath();
writer.setOutput(ios);
TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.ENGLISH);
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("PackBits");
IIOImage iioImage = new IIOImage(image, null, null);
writer.write(null, iioImage, writeParam);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static BufferedImage decodeToImage(String imageString) {
BufferedImage image = null;
byte[] imageByte;
try {
Base64.Decoder decoder = Base64.getDecoder();
imageByte = decoder.decode(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
public BufferedImage placeDataOnImage(BufferedImage image, String transNum) {
try {
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.drawString(transNum, 150, 1350);
g.drawString(transNum, 200, 2805);
g.drawString(transNum, 200, 4453);
g.drawString("DFDFFFF", 225, 590);
g.drawString("rrrr", 225, 675);
g.drawString("555-555-5555", 225, 718);
g.drawString("Jocelyn", 767, 597);
g.drawString("1-877-324-6427, ext. #", 767, 665);
g.drawString("1-888-550-6427 (Toll Free)", 767, 701);
g.drawString("11/28/18", 276, 800);
g.drawString("11/28/18", 718, 799);
g.drawString("2", 270, 837);
g.drawString("sdfsdfsdfsdfsdf", 176, 1880);
g.drawString("11/15/2018", 176, 1980);
g.dispose();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
public String exec(ITestExecutionServices tes, String[] args) {
boolean savedImg = false;
String currentTransID = args[0];
image = decodeToImage(tiffImg);
image = placeDataOnImage(image, currentTransID);
savedImg = saveTiff(currentTransID + ".tiff", image);
if (savedImg) {
tes.getTestLogManager().reportVerificationPoint("TIFF created with Transaction ID# " + currentTransID, VerdictEvent.VERDICT_PASS);
} else {
tes.getTestLogManager().reportVerificationPoint("TIFF could not be created", VerdictEvent.VERDICT_ERROR);
}
return null;
}
有人看到我哪里做错了吗?
Ugg...刚刚想到了什么。我没有指定文本的颜色,因此我认为它默认为白色 - 这就是我看不到它的全部原因。
我补充了:
g.setColor(Color.BLACK);
它神奇地出现在图像上了!去图....