使用JAI时别名

时间:2015-01-07 11:31:39

标签: java jai aliasing

亲爱的Stackoverflow社区,

所以目前我正在使用Java Advanced Imaging API来实现一个窗格,在这个窗格中你有一个可以自由移动的角落,并可以根据需要扩展整个内容。我小心翼翼,结果图片不会比原来的图片大得多或小得多。现在有些人使用我们的软件抱怨,结果图像中有一些别名。我尝试了一些抗锯齿措施(对整个结果进行低通滤波,设置更好的插值器和图形渲染提示),但我似乎无法摆脱这个问题。任何想法?

我的代码:

@Override
public void paint(Graphics g) {
    super.paint(g);

    ParameterBlock params = new ParameterBlock();
    params.addSource(previewImage); // source is the input image
    int w = previewImage.getWidth(); // Set to the original width of the
                                     // image
    int h = previewImage.getHeight(); // Set to the original height of image

    Point tl = new Point((int) cornersTrans[0].getX(), (int) cornersTrans[0].getY()); // The
                                                                                        // new
                                                                                        // top
                                                                                        // left
                                                                                        // corner
    Point tr = new Point((int) cornersTrans[1].getX(), (int) cornersTrans[1].getY()); // The
                                                                                        // new
                                                                                        // top
                                                                                        // right
                                                                                        // corner
    Point bl = new Point((int) cornersTrans[2].getX(), (int) cornersTrans[2].getY()); // The
                                                                                        // new
                                                                                        // bottom
                                                                                        // left
                                                                                        // corner
    Point br = new Point((int) cornersTrans[3].getX(), (int) cornersTrans[3].getY()); // The
                                                                                        // new
                                                                                        // bottom
                                                                                        // right
                                                                                        // corner
    try {
        params.add(new WarpPerspective(PerspectiveTransform.getQuadToQuad(0, 0, 0, h, w, h, w, 0, tl.x, tl.y, bl.x, bl.y, br.x, br.y, tr.x, tr.y).createInverse()));
    } catch (NoninvertibleTransformException e) {
        e.printStackTrace();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }

    if (!isInMovement)
        params.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC));

    PlanarImage image = smoothConvolve( JAI.create("warp", params) );

    int y0 = (int) (cornersTrans[0].getY() < cornersTrans[1].getY() ? cornersTrans[0].getY() : cornersTrans[1].getY());
    int x0 = (int) (cornersTrans[0].getX() < cornersTrans[2].getX() ? cornersTrans[0].getX() : cornersTrans[2].getX());
    int x1 = (int) (cornersTrans[3].getX() > cornersTrans[1].getX() ? cornersTrans[3].getX() : cornersTrans[1].getX());
    int y1 = (int) (cornersTrans[3].getY() > cornersTrans[2].getY() ? cornersTrans[3].getY() : cornersTrans[2].getY());

    Graphics2D g2d = (Graphics2D) g;
 //     g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f));
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.drawImage((Image) image.getAsBufferedImage(), x0, y0, x1, y1, 0, 0, image.getWidth(), image.getHeight(), null);

    if (enableEditing) {
        int width = (int) (cornersTrans[3].getX() - cornersTrans[0].getX());
        int height = (int) (cornersTrans[3].getY() - cornersTrans[0].getY());

        g.drawImage(moveIcon, (int) cornersTrans[0].getX() + (width / 2) - (MOVE_ICON_WIDTH / 2), (int) (cornersTrans[0].getY() + (height / 2) - (MOVE_ICON_WIDTH / 2)),
                (int) (cornersTrans[0].getX() + (width / 2) + (MOVE_ICON_WIDTH / 2)), (int) (cornersTrans[0].getY() + (height / 2) + (MOVE_ICON_WIDTH / 2)), 0, 0, moveIcon.getWidth(),
                moveIcon.getHeight(), null);

        if (mode == MODE_DISTORT) {
            for (IntPoint p : cornersTrans)
                drawCircle(g, new Point(p.x, p.y));
        } else {
            for (int i = 0; i < 4; i++)
                drawScalingIcon(g2d, i);
        }
    }
}

private PlanarImage smoothConvolve(PlanarImage input){
     int kernelSize = 2;
        // Create an array with enough positions for the kernel.
        float[] kernelMatrix = new float[kernelSize*kernelSize];
        // Fill the array with the data for the kernel. The sum of all array
        // positions will be 1.0.
        for(int k=0;k<kernelMatrix.length;k++)
          kernelMatrix[k] = 1.0f/(kernelSize*kernelSize);
        // Create the kernel using the array.
        KernelJAI kernel = new KernelJAI(kernelSize,kernelSize,kernelMatrix);
        // Run the convolve operator, creating the output image.
        return JAI.create("convolve", input, kernel);       
}

我附上了一个示例,说明了结果图像在画布1中的外观。有谁知道如何解决这个问题?

enter image description here

0 个答案:

没有答案