Create image from scratch with JMagick

Posted by Michael IV on Stack Overflow See other posts from Stack Overflow or by Michael IV
Published on 2013-02-10T16:03:03Z Indexed on 2014/06/13 15:25 UTC
Read the original article Hit count: 382

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about imagemagick