我正在使用ImageMagick的Java版本JMagick。我需要能够创建一个新的图像,并将任意文本块写入其中。文档非常简陋,到目前为止,我所能得到的只是从IO中写入图像的文本。此外,在我找到的所有示例中,似乎在编写新图像数据之前,第一步操作总是将现有图像加载到ImageInfo实例中。如何使用JMagick从头开始创建图像,然后将文本写入其中?
这是我现在所做的:
这是我现在所做的:
try {
ImageInfo info = new ImageInfo();
info.setSize("512x512");
info.setUnits(ResolutionType.PixelsPerInchResolution);
info.setColorspace(ColorspaceType.RGBColorspace);
info.setBorderColor(PixelPacket.queryColorDatabase("red"));
info.setDepth(8);
BufferedImage img = new BufferedImage(512,512,BufferedImage.TYPE_4BYTE_ABGR);
byte[] imageBytes = ((DataBufferByte) img.getData().getDataBuffer()).getData();
MagickImage mimage = new MagickImage(info,imageBytes);
DrawInfo aInfo = new DrawInfo(info);
aInfo.setFill(PixelPacket.queryColorDatabase("green"));
aInfo.setUnderColor(PixelPacket.queryColorDatabase("yellow"));
aInfo.setOpacity(0);
aInfo.setPointsize(36);
aInfo.setFont("Arial");
aInfo.setTextAntialias(true);
aInfo.setText("JMagick Tutorial");
aInfo.setGeometry("+40+40");
mimage.annotateImage(aInfo);
mimage.setFileName("text.jpg");
mimage.writeImage(info);
} catch (MagickException ex) {
Logger.getLogger(LWJGL_IDOMOO_SIMPLE_TEST.class.getName()).log(Level.SEVERE, null, ex);
}
它不起作用,JVM崩溃并出现访问冲突,可能是因为它期望从IO中获取输入图像。