AffineTransform图像仿射操作类- (Never Give Up)--(我的知识点滴 ...
AffineTransform类描述了一种二维仿射变换的功能,它是一种二维坐标到二维坐标之间的线性变换,保持二维图形的“平直性”(译注:straightness,即变换后直线还是直线不会打弯,圆弧还是圆弧)和“平行性”(译注:parallelness,其实是指保二维图形间的相对位置关系不变,平行线还是平行线,相交直线的交角不变。大二学过的复变,“保形变换/保角变换”都还记得吧,数学就是王道啊!)。仿射变换可以通过一系列的原子变换的复合来实现,包括:平移(Translation)、缩放(Scale)、翻转(Flip)、旋转(Rotation)和剪切(Shear)。

此类变换可以用一个3×3的矩阵来表示,其{zh1}一行为(0, 0, 1)。该变换矩阵将原坐标(x, y)变换为新坐标(x', y'),这里原坐标和新坐标皆视为最末一行为(1)的三维列向量,原列向量左乘变换矩阵得到新的列向量:

[x'] [m00 m01 m02] [x] [m00*x+m01*y+m02]
[y'] = [m10 m11 m12] [y] = [m10*x+m11*y+m12]
[1 ] [ 0 0 1 ] [1] [ 1 ]


几种典型的仿射变换:


public static AffineTransform getTranslateInstance(double tx, double ty)

平移变换,将每一点移动到(x+tx, y+ty),变换矩阵为:
[ 1 0 tx ]
[ 0 1 ty ]
[ 0 0 1 ]
(译注:平移变换是一种“刚体变换”,rigid-body transformation,中学学过的物理,都知道啥叫“刚体”吧,就是不会产生形变的理想物体,平移当然不会改变二维图形的形状。同理,下面的“旋转变换”也是刚体变换,而“缩放”、“错切”都是会改变图形形状的。)


public static AffineTransform getScaleInstance(double sx, double sy)

缩放变换,将每一点的横坐标放大(缩小)至sx倍,纵坐标放大(缩小)至sy倍,变换矩阵为:
[ sx 0 0 ]
[ 0 sy 0 ]
[ 0 0 1 ]



public static AffineTransform getShearInstance(double shx, double shy)

剪切变换,变换矩阵为:
[ 1 shx 0 ]
[ shy 1 0 ]
[ 0 0 1 ]
相当于一个横向剪切与一个纵向剪切的复合
[ 1 0 0 ][ 1 shx 0 ]
[ shy 1 0 ][ 0 1 0 ]
[ 0 0 1 ][ 0 0 1 ]
(译注:“剪切变换”又称“错切变换”,指的是类似于四边形不稳定性那种性质,街边小商店那种铁拉门都见过吧?想象一下上面铁条构成的菱形拉动的过程,那就是“错切”的过程。)


public static AffineTransform getRotateInstance(double theta)

旋转变换,目标图形围绕原点顺时针旋转theta弧度,变换矩阵为:
[ cos(theta) -sin(theta) 0 ]
[ sin(theta) cos(theta) 0 ]
[ 0 0 1 ]



public static AffineTransform getRotateInstance(double theta, double x, double y)

旋转变换,目标图形以(x, y)为轴心顺时针旋转theta弧度,变换矩阵为:
[ cos(theta) -sin(theta) x-x*cos+y*sin]
[ sin(theta) cos(theta) y-x*sin-y*cos ]
[ 0 0 1 ]
相当于两次平移变换与一次原点旋转变换的复合:
[1 0 -x][cos(theta) -sin(theta) 0][1 0 x]
[0 1 -y][sin(theta) cos(theta) 0][0 1 y]
[0 0 1 ][ 0 0 1 ][0 0 1]

?

package Assis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class IsAffineTransform extends JComponent {

	private static final long serialVersionUID = 1L;

	public IsAffineTransform() {
		setDoubleBuffered(true);
	}

	public void paintComponent(Graphics g) {
		AffineTransform at;
		int i;
		Graphics2D g2 = (Graphics2D) g;
		// xx锯齿边缘
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		Dimension size = getSize();
		g2.setColor(Color.white);
		g2.fill(new Rectangle2D.Double(0, 0, size.width, size.height));

		at = new AffineTransform();

		Font f1 = new Font("Serif", Font.BOLD, 18);
		g2.setFont(f1);

		Color colorArray[] = new Color[10];
		colorArray[0] = Color.blue;
		colorArray[1] = Color.green;
		colorArray[2] = Color.magenta;
		colorArray[3] = Color.black;
		colorArray[4] = Color.blue;
		colorArray[5] = Color.green;
		colorArray[6] = Color.magenta;
		colorArray[7] = Color.black;
		for (i = 0; i < 8; i++) {
			at.rotate(Math.PI / 4, 180, 200);
			g2.setTransform(at);
			g2.setColor(colorArray[i % 8]);
			g2.drawString("Hello,World!", 200, 200);
		}

	}

	public static void main(String args[]) {
		MyWindowListener l = new MyWindowListener();
		IsAffineTransform c = new IsAffineTransform();

		JFrame fr = new JFrame("旋转");
		fr.addWindowListener(l);
		fr.getContentPane().add(c, BorderLayout.CENTER);
		fr.pack();
		fr.setSize(400, 400);
		fr.setLocation(400, 400);
		fr.setVisible(true);

	}

}

class MyWindowListener extends WindowAdapter {
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
}

?

package Assis;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

/**
 * 使图片水平翻转、垂直翻转、旋转180度
 * 
 * @author Administrator
 */
public class TestPicture {

	public static void main(String[] args) throws IOException {
		BufferedImage sourceImage = ImageIO.read(getURL("/user.png"));
		BufferedImage dstImage = null;
		// AffineTransform transform = new AffineTransform(-1, 0, 0, 1,
		// sourceImage.getWidth(), 0);// 水平翻转
		AffineTransform transform = new AffineTransform(1, 0, 0, -1, 0,
				sourceImage.getHeight());// 垂直翻转
		// AffineTransform transform = new AffineTransform(-1, 0, 0, -1,
		// sourceImage.getWidth(), sourceImage.getHeight());// 旋转180度
		AffineTransformOp op = new AffineTransformOp(transform,
				AffineTransformOp.TYPE_BILINEAR);
		dstImage = op.filter(sourceImage, null);

		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.add("Source Transform", new JLabel(
				new ImageIcon(sourceImage)));
		tabbedPane.add("Affine Transform", new JLabel(new ImageIcon(dstImage)));

		JFrame jframe = new JFrame();
		jframe.setSize(800, 600);
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.getContentPane().add(tabbedPane);
		jframe.setVisible(true);
	}

	/** 获得文件的{jd1}地址 */
	public static final URL getURL(String path) {
		return "".getClass().getResource(path);
	}
}

?

郑重声明:资讯 【AffineTransform图像仿射操作类- (Never Give Up)--(我的知识点滴 ...】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——