jmagick文字绕圈稍微偏了

时间:2013-01-04 17:54:35

标签: java imagemagick jmagick

我对ImageMagick和java相对较新,并且在一个项目中使用ImageMagick 6.3.9 Q16和jmagick 6.3.9 Q16在窗口上显示围绕圆心0度圆的外部的文本。我们正在从PHP MagickWand移植现有的图像magick代码,但是由于以下差异,我认为java版本中每个字母在圆弧上的位置有点偏离。

在MagickWand中,它通过这一行代码放置在弧上,该代码使用浮动x,y坐标值和浮动角度值(更高精度)来注释绘图棒(相当于jmagick中的DrawInfo)并且工作得很漂亮:

MagickAnnotateImage($magick_wand, $drawing_wand, $origin_x + $x, $origin_y - $y, $angle, $character);

在jmagick中,annotateImage方法只接受一个参数,即DrawInfo,所以我最终得到了我认为唯一的另一种选择,即compositeImage方法。所以为了做到这一点,我将每个角色绘制为单独的绘图信息,然后将其注释为透明的png图像,然后通过rotateImage方法旋转该图像,然后使用compositeImage将其放置在我的画布图像上,但compositeImage仅处理x& y作为int值(并且不考虑角度)所以我正在舍入我的x& y double值(得到相同数量的小数或更多像php版本只是用来排除那个),我怀疑这是将字符放在圆圈上的主要原因。

执行工作的代码如下所示,Article是字体文件的本地路径(例如:E:\ WCDE_ENT70 \ workspace \ Stores \ WebContent \ AdminArea \ CoordsCenterSection \ fonts \ ARIALN.TTF),nameNumStr是字符串要在圆上渲染(例如:SAMUELSON),fsize是字体的磅值(例如:32),colorStr是字体颜色名称(例如:黑色),radVal是半径(例如:120),poix是x原点开始坐标(例如:150),poiy是y起始坐标(例如:150):

public byte[] getArcedImage(String Article, String nameNumStr, int fsize, String colorStr, int radVal, int poix, int poiy)
{
     try {
            Font f = null;
            try {
                f = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(Article.replaceAll("%20"," ")));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (FontFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String fontName = f.getName();
            // Use awt's font metrics since jmagick doesn't have font metrics built in like php magickwand does
            FontMetrics fm = createFontMetrics(new Font(fontName, Font.PLAIN, fsize));
            int strImgW = fm.stringWidth(nameNumStr);
            int strImgH = fm.getHeight();

            String spacerImg = "E:\\WCDE_ENT70\\workspace\\Stores\\WebContent\\AdminArea\\CoordsCenterSection\\images\\600x600.png";

            //Read in large 600 png first as our main canvas
            ImageInfo bi = new ImageInfo(spacerImg);
            MagickImage bmi = new MagickImage(bi);

            // Make canvas image transparent
            bmi.setMatte(true);
            bmi.setBackgroundColor(PixelPacket.queryColorDatabase("#FFFF8800"));

            //defaults or param vals
            final int radius = radVal;
            final int origin_x = poix;
            final int origin_y = poiy;
            final int center_text_on = 0;
            final int charXGeom = 150;
            final int charYGeom = 150;

            double circumference = 0;
            double percentage = 0;
            double degrees = 0;
            double start = 0;
            double current_degree = 0;
            double angle = 0;
            double angle_adjustment = 0;
            double character_center = 0;

            /**
             * Calculate the circumference of the drawn circle and label the image
             * with it.
             */
            circumference = (2 * Math.PI * radius);

            /**
             * Calculate the percentage of the circumference that the string will
             * consume.
             */
            percentage = strImgW / circumference;

            /**
             * Convert this percentage into something practical - degrees.
             */
            degrees = 360 * percentage;

            /**
             * Because the string is centered, we need to calculate the starting point
             * of the string by subtracting half of the required degrees from the
             * anticipated center mark.
             */
            start = center_text_on - (degrees / 2);

            /**
             * Initialize our traversal starting point.
             */
            current_degree = start;

            // 
                        ImageInfo ci = null;
                        MagickImage cmi = null;

                        double x = 0;
                        double y = 0;
                        int finalStrWidth = 0;
                        int charImgW = 0;
                        int charImgH = 0;

                        for (int i=0; i<nameNumStr.length(); i++)
                        {
                                    /**
                                     * Isolate the appropriate character.
                                     */
                                    String charVal = nameNumStr.substring(i, i+1);

                                    charImgW = fm.stringWidth(charVal);
                                    charImgH = strImgH;

                                    ci = new ImageInfo(spacerImg);
                                    cmi = new MagickImage(ci);

                                    // Create Rectangle for cropping character image canvas to final width and height
                                    Rectangle charRect = new Rectangle(0,0,charImgW,charImgH);

                                    // Crop image to final width and height
                                    cmi = cmi.cropImage(charRect);

                                    // Make image transparent
                                    cmi.setMatte(true);
                                    cmi.setBackgroundColor(PixelPacket.queryColorDatabase("#FFFF8800"));

                                    // Set a draw info for each character
                                    DrawInfo cdi = new DrawInfo(ci);

                                    // Set Opacity
                                    cdi.setOpacity(0);

                                    // Set Gravity
                                    cdi.setGravity(GravityType.CenterGravity);

                                    // Set Fill Color
                                    cdi.setFill(PixelPacket.queryColorDatabase(colorStr));

                                    // Set Font Size
                                    cdi.setPointsize(fsize);

                                    // Set Font
                                    cdi.setFont(Article.replaceAll("%20"," "));

                                    // Set the text
                                    cdi.setText(charVal);

                                    // Make the text smoother
                                    cdi.setTextAntialias(true);

                                    // Annotate the draw info to make the character image
                                    cmi.annotateImage(cdi);

                                    // For debug purposes
                                    finalStrWidth += charImgW;

                                    /**
                                     * Calculate the percentage of the circumference that the character
                                     * will consume.
                                     */
                                    percentage = charImgW / circumference;

                                    /**
                                     * Convert this percentage into something practical - degrees.
                                     */
                                    degrees = 360 * percentage;

                                    /**
                                     * Calculate the x and y axis adjustments to make, based on the origin
                                     * of the circle, so we can place each letter.
                                     */
                                    x = radius * Math.sin(Math.toRadians(current_degree));
                                    y = radius * Math.cos(Math.toRadians(current_degree));

                                    // Rotate the character image to the angle
                                    cmi = cmi.rotateImage(angle);

                                    // Composite character image to main canvas image
                                    bmi.compositeImage(CompositeOperator.HardLightCompositeOp, cmi, (int)Math.round((origin_x+x)), (int)Math.round((origin_y-y)));

                                    // Increment the degrees
                                    current_degree += degrees;

                        }

                        bmi = bmi.trimImage();
                        byte[] pi = bmi.imageToBlob(ci);
                        return pi;

            } catch (MagickException e) {
                        e.printStackTrace();
                        return null;
            }
}

private FontMetrics createFontMetrics(Font font)
{
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
    Graphics g = bi.getGraphics();
    FontMetrics fm = g.getFontMetrics(font);
    g.dispose();
    bi = null;
    return fm;
}

private Rectangle2D createFontRectangle(Font font, String strVal)
{
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
    Graphics g = bi.getGraphics();
    FontMetrics fm = g.getFontMetrics(font);
    Rectangle2D rect = fm.getStringBounds(strVal, g);
    g.dispose();
    bi = null;
    return rect;
}

我已经发现可以使用DrawInfo的setGeometry方法来设置x,y并看到我在jmagick.org的wiki上找到的一个例子,据说它可以用于x,y以外的放置但是找不到任何其他示例或文档显示如何使用它(希望也指定一个角度)。

我不是肯定的,但似乎setGeometry是唯一可能指定角度的方法,因为jmagick的annotateImage实现只接受Draw Info作为它的参数。

有没有人知道使用DrawInfo的setGeometry方法设置x,y和角度的方法?我认为它可以解决我的问题。此外,如果任何人有任何使用jmagick在他们愿意分享的圆圈周围绘制文字的工作示例,我将非常感激。

由于

0 个答案:

没有答案