Java Graphics2D drawString 是 "blacking out" 源图像
Java Graphics2D drawString is "blacking out" the source image
Java 8 和 Mac OS(High Sierra)在这里。我有以下 class TextOverlayer
从文件系统读取图像并需要将一些红色文本覆盖到图像上,然后将该 "overlayed" 图像另存为文件中的不同文件系统:
public class TextOverlayer implements ImageObserver {
public static void main(String[] args) throws IOException {
// Instantiate a TextOverlayer and read a source/input image from disk
TextOverlayer textOverlayer = new TextOverlayer();
BufferedImage bufferedImage = ImageIO.read(Paths.get("/User/myuser/pix/sourceImage.jpg").toFile());
// Lay some text over the image at specific coordinates
BufferedImage drawn = textOverlayer.drawText(bufferedImage, "Some text");
// Write the overlayed image to disk
File outputfile = new File("/User/myuser/pix/targetImage.jpg");
ImageIO.write(drawn, "jpg", outputfile);
}
private BufferedImage drawText(BufferedImage old, String text) {
int w = old.getWidth() / 3;
int h = old.getHeight() / 3;
BufferedImage img = new BufferedImage(
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, w, h, this);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(text) - 5;
int y = fm.getHeight();
g2d.drawString(text, x, y);
g2d.dispose();
return img;
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
return false;
}
}
运行时,不会抛出任何错误,并且 targetImage.jpg
已成功写入磁盘,除了它只是一个小黑框的图像。我原以为 targetImage.jpg
与 sourceImage.jpg
完全相同,只是在所需坐标(图像内)处添加了一些额外的文本。
有什么地方出错了吗?
您不需要 ImageObserver,并且您可能无法使用包含的编写器将带有 alpha 通道的图像写入 JPEG 文件。
这个有效:
public class TextOverlayer {
public static void main(String[] args) throws IOException {
// Instantiate a TextOverlayer and read a source/input image from disk
TextOverlayer textOverlayer = new TextOverlayer();
BufferedImage bufferedImage = ImageIO.read(Paths.get("/User/myuser/pix/sourceImage.jpg").toFile());
// Lay some text over the image at specific coordinates
BufferedImage drawn = textOverlayer.drawText(bufferedImage, "Some text");
// Write the overlayed image to disk
File outputfile = new File("/User/myuser/pix/targetImage.jpg");
boolean result = ImageIO.write(drawn, "jpg", outputfile);
if (!result) {
System.out.println("FAILED");
}
}
private BufferedImage drawText(BufferedImage old, String text) {
int w = old.getWidth();
int h = old.getHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, w, h, null);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(text) - 5;
int y = fm.getHeight();
g2d.drawString(text, x, y);
g2d.dispose();
return img;
}
}
Java 8 和 Mac OS(High Sierra)在这里。我有以下 class TextOverlayer
从文件系统读取图像并需要将一些红色文本覆盖到图像上,然后将该 "overlayed" 图像另存为文件中的不同文件系统:
public class TextOverlayer implements ImageObserver {
public static void main(String[] args) throws IOException {
// Instantiate a TextOverlayer and read a source/input image from disk
TextOverlayer textOverlayer = new TextOverlayer();
BufferedImage bufferedImage = ImageIO.read(Paths.get("/User/myuser/pix/sourceImage.jpg").toFile());
// Lay some text over the image at specific coordinates
BufferedImage drawn = textOverlayer.drawText(bufferedImage, "Some text");
// Write the overlayed image to disk
File outputfile = new File("/User/myuser/pix/targetImage.jpg");
ImageIO.write(drawn, "jpg", outputfile);
}
private BufferedImage drawText(BufferedImage old, String text) {
int w = old.getWidth() / 3;
int h = old.getHeight() / 3;
BufferedImage img = new BufferedImage(
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, w, h, this);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(text) - 5;
int y = fm.getHeight();
g2d.drawString(text, x, y);
g2d.dispose();
return img;
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
return false;
}
}
运行时,不会抛出任何错误,并且 targetImage.jpg
已成功写入磁盘,除了它只是一个小黑框的图像。我原以为 targetImage.jpg
与 sourceImage.jpg
完全相同,只是在所需坐标(图像内)处添加了一些额外的文本。
有什么地方出错了吗?
您不需要 ImageObserver,并且您可能无法使用包含的编写器将带有 alpha 通道的图像写入 JPEG 文件。
这个有效:
public class TextOverlayer {
public static void main(String[] args) throws IOException {
// Instantiate a TextOverlayer and read a source/input image from disk
TextOverlayer textOverlayer = new TextOverlayer();
BufferedImage bufferedImage = ImageIO.read(Paths.get("/User/myuser/pix/sourceImage.jpg").toFile());
// Lay some text over the image at specific coordinates
BufferedImage drawn = textOverlayer.drawText(bufferedImage, "Some text");
// Write the overlayed image to disk
File outputfile = new File("/User/myuser/pix/targetImage.jpg");
boolean result = ImageIO.write(drawn, "jpg", outputfile);
if (!result) {
System.out.println("FAILED");
}
}
private BufferedImage drawText(BufferedImage old, String text) {
int w = old.getWidth();
int h = old.getHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, w, h, null);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(text) - 5;
int y = fm.getHeight();
g2d.drawString(text, x, y);
g2d.dispose();
return img;
}
}