java 对图片的矩阵运算示例

来源:互联网 发布:linux命令存储 编辑:程序博客网 时间:2024/06/05 18:45

之前看过一些java矩阵运算,不过没找到具体的例子。下面就介绍我自己做的一个小demo.
大致效果就是对图片进行平移,缩放;图片的坐标始终保持不变。
譬如:图片移动了  T 米,本来图片左上角在(0,0),现在点击图片左上角,还是在(0,0)位置;
同理,缩放也是同样的效果。
控制台输出的信息就是图片的像素坐标(可以这样理解)

请自己找一张图片测试,图片的大小(我这里图片大小为(500,500),希望能有所帮助;


import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Graphics;import java.awt.Image;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import Jama.Matrix;public class affineTest {private int width = 500, height = 500;private int centralPoint_x = 0, centralPoint_y = 0;private int top_x = 0, top_y = 0;private JFrame jf;public affineTest() {centralPoint_x = width / 2;centralPoint_y = height / 2;init();}public static void main(String[] args) {new affineTest();}private void init() {jf = new JFrame("AffineTest");Container container = jf.getContentPane();jf.setSize(600, 600);jf.setLocation(0, 0);//container.setLayout(new BorderLayout());final Image ii = new ImageIcon("e:/0.jpg").getImage();JPanel jp = new JPanel() {protected void paintComponent(Graphics g) {super.paintComponent(g);g.drawImage(ii, top_x, top_y, width, height, null);}};//jp.setSize(500, 500);//container.add(jp);jf.add(jp);jf.setVisible(true);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);MouseControl mc = new MouseControl();jp.addMouseListener(mc);jp.addMouseMotionListener(mc);// 取消鼠标拖拽图片jp.addMouseWheelListener(mc);}class MouseControl extends MouseAdapter implements MouseMotionListener, MouseWheelListener {private int x, xxx;private int y, yyy;private int move_x, move_y;private int lastW = width, lastH = height;private double scale_x = 1.0, scale_y = 1.0;private int sum_x = 0, sum_y = 0;private double[][] E = {{1, 0, 0},{0, 1, 0},{0, 0, 1}};Matrix Total = new Matrix(E);Matrix RTR_1 = new Matrix(E);Matrix Invers = new Matrix(E);public void mousePressed(MouseEvent e) {xxx = x = e.getX();yyy = y = e.getY();//System.out.println(x+"  "+y);}public void mouseReleased(MouseEvent e) {int xx = e.getX();int yy = e.getY();//System.out.println(xx+"  "+yy);//System.out.println("change x : " + (xx- xxx) + "  y : "+ (yy -  yyy));move_x = xx - xxx;move_y = yy - yyy;sum_x = sum_x + move_x;sum_y = sum_y + move_y;double[][] moved= {{1     , 0     , 0},{0     , 1     , 0},{move_x, move_y, 1}};Total = Total.times(new Matrix(moved));}public void mouseDragged(MouseEvent e) {int dx = e.getX() - x;int dy = e.getY() - y;top_x = top_x + dx;top_y = top_y + dy;centralPoint_x = top_x + width / 2;centralPoint_y = top_y + height / 2;//System.out.println(centralPoint_x+"    "+centralPoint_y);jf.repaint();x = x + dx;y = y + dy;  }public void mouseMoved(MouseEvent e){int xm = e.getX();int ym = e.getY();//System.out.println(xm+"      "+ym);double[][] finXY = {{xm , ym , 1}};//System.out.println(Total.get(2, 0)+"  Total "+Total.get(2, 1));Invers = Total.inverse();//System.out.println(Invers.get(2, 0)+"  invers "+Invers.get(2, 1));Matrix FIN = new Matrix(finXY);FIN = FIN.times(Invers); System.out.println((int)(FIN.get(0, 0) )+"  print mouse position  "+(int)(FIN.get(0, 1) )); }public void mouseWheelMoved(MouseWheelEvent e) {int num = e.getWheelRotation();int wheelScale = num * 2;width = width - wheelScale;height = height - wheelScale;scale_x = (double)width / lastW;scale_y = (double)height / lastH;lastW = width;lastH = height;//System.out.println(scale_x+"  scaleXY  "+scale_y);top_x = centralPoint_x - width / 2;top_y = centralPoint_y - height / 2;jf.repaint();//System.out.println(realW+"  "+realH);double[][] reback = {{1          , 0          , 0},{0          , 1          , 0},{centralPoint_x ,centralPoint_y , 1}};double[][] reback_1 = {{1          ,  0           , 0},{0          ,  1           , 0},{-centralPoint_x , -centralPoint_y , 1}};double[][] scale = {{scale_x, 0      , 0},{0      , scale_y, 0},{0      , 0      , 1}};Matrix Reback = new Matrix(reback);Matrix Reabck_1 = new Matrix(reback_1);Matrix TempS = new Matrix(scale);RTR_1 = Reabck_1.times(TempS);RTR_1 = RTR_1.times(Reback);//System.out.println(RTR_1.get(0, 0)+"  RTR  "+move_y);Total = Total.times(RTR_1);}}}