0

我想绘制包含栅格和矢量数据的缩放对象。目前,我正在绘制一个缩放的 Graphics2D 对象

protected void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    //Get transformation, apply scale and shifts
    at = g2d.getTransform();
    at.translate(x, y);
    at.scale(zoom, zoom);      

    //Transform Graphics2D object  
    g2d.setTransform(at);

    //Draw buffered image into the transformed object
    g2d.drawImage(img, 0, 0, this);

    //Draw line into transformed Graphics2D object
    Line2D.Double line = new Line2D.Double();
    line.x1 = (int)p1.getX();
    line.y1 = (int)p1.getY();
    line.x2 = (int)p2.getX();
    line.y2 = (int)p2.getY();

    g2d.draw(line);

    g2d.dispose();
}

不幸的是,这种方法有一些缺点(影响笔画宽度,缩放图像的质量较差)。

相反,我想绘制缩放的对象。对于矢量数据,方法很简单:

protected void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    //Update affine transformation
    at = AffineTransform.getTranslateInstance(x, y);
    at = AffineTransform.getScaleInstance(zoom, zoom);

    //Transform line and draw
    Line2D.Double line = new Line2D.Double();
    line.x1 = (int)p1.getX();
    line.y1 = (int)p1.getY();
    line.x2 = (int)p2.getX();
    line.y2 = (int)p2.getY();

    g2d.draw(at.createTransformedShape((Shape)line));

    //Transform buffered image and draw ???
    g2d.draw(at.createTransformedShape((Shape)img));  //Error

    g2d.dispose();
}

但是,如何在不重新缩放 Graphics2D 对象的情况下将转换应用于缓冲图像?这种方法不起作用

g2d.draw(at.createTransformedShape((Shape)img)); 

因为光栅图像不能理解为矢量形状......

谢谢你的帮助。

4

1 回答 1

1

一种可能的解决方案可能表示栅格的仿射变换:

//Update affine transformation
at = AffineTransform.getTranslateInstance(x, y);
at = AffineTransform.getScaleInstance(zoom, zoom);

at.translate(x, y);
at.scale(zoom, zoom);      

//Create affine transformation
AffineTransformOp op = new AffineTransformOp(at,  AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

//Apply transformation
BufferedImage img2 = op.filter(img, null);

//Draw image
g2d.drawImage(img2, 0, 0, this);

不幸的是,这种方法不适用于缩放操作;它的计算成本更高。对于栅格数据(JPG,5000 x 7000 pix),一个简单的双线性插值

AffineTransformOp op = new AffineTransformOp(at,  AffineTransformOp.TYPE_BILINEAR);

大约需要 1.5 秒...相反,使用缩放 Graphics2D 对象明显更快(< 0.1 秒),图像可以实时移动和缩放。

在我看来,将对象与栅格数据一起重新缩放比重新缩放 Graphics2D 对象要慢......

于 2016-12-04T14:20:29.260 回答