Reputation: 11424
I am using Java port of ImageMagick called JMagick .I need to be able to create a new image and write an arbitrary text chunk into it.The docs are very poor and what I managed to get so far is to write text into the image which comes from IO.Also , in all the examples I have found it seems like the very first operation ,before writing new image data , is always loading of an existing image into ImageInfo instance.How do I create an image from scratch with JMagick and then write a text into it?
Here is what I do now :
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);
}
It doesn't work , the JVM crashes with access violation as it probably expects for the input image from IO.
Upvotes: 3
Views: 3036
Reputation: 2989
Here is a Clojure example to create a blue image using JMagick:
(import '[magick MagickImage ImageInfo ColorspaceType])
(def image (MagickImage.))
(.constituteImage image 256 256 "RGB" (byte-array (take (* 3 256 256) (cycle [0 0 255]))))
(def info (ImageInfo.))
(.setSize info "256x256")
(.setDepth info 8)
(.setColorspace info ColorspaceType/RGBColorspace)
(.setFileName image "test.jpg")
(.writeImage image info)
Upvotes: 0
Reputation: 99
I understand that it may be too late for the answer.Here I wrote this simple code to create a scenario of what you asked.!
private void createEmptyImage() throws MagickException{
ImageInfo newImageiInfo=new ImageInfo();
newImageiInfo.setFileName("src\\main\\resources\\test.jpg");
newImageiInfo.setSize("512x512");
newImageiInfo.setUnits(ResolutionType.PixelsPerInchResolution);
newImageiInfo.setColorspace(ColorspaceType.RGBColorspace);
newImageiInfo.setBorderColor(PixelPacket.queryColorDatabase("red"));
newImageiInfo.setDepth(8);
MagickImage addTextImage = new MagickImage();
addTextImage.allocateImage(newImageiInfo);
addTextImage.setYResolution(480);
addTextImage.setXResolution(640);
addTextImage.writeImage(newImageiInfo);
DrawInfo aInfo = new DrawInfo(newImageiInfo);
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("+50+50");
addTextImage.annotateImage(aInfo);
addTextImage.setFileName("src\\main\\resources\\test-result.jpg");
addTextImage.writeImage(newImageiInfo);
}
Upvotes: 3