日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Image中的transformation理解【swt.snippet】

系統(tǒng) 1790 0
    /*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Use transformation matrices to reflect, rotate and shear images
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet207 {	
	public static void main(String[] args) {
		final Display display = new Display();
		
		final Image image = new Image(display, 110, 60);
		GC gc = new GC(image);
		Font font = new Font(display, "Times", 30, SWT.BOLD);
		gc.setFont(font);
		gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
		gc.fillRectangle(0, 0, 110, 60);
		gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
		gc.drawText("HOV", 10, 10, true);
		gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
		gc.fillRectangle(0, 0, 10, 10);
		font.dispose();
		gc.dispose();
		
		final Rectangle rect = image.getBounds();
		Shell shell = new Shell(display);
		shell.setText("Matrix Tranformations");
		shell.setLayout(new FillLayout());
		final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
		canvas.addPaintListener(new PaintListener () {
			public void paintControl(PaintEvent e) {
				GC gc = e.gc;
				gc.setAdvanced(true);
				if (!gc.getAdvanced()){
					gc.drawText("Advanced graphics not supported", 30, 30, true);
					return;
				}
				
				// Original image
				int x = 30, y = 30;
				gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
//				gc.drawLine(20, 20, 580, 580);
//				gc.drawImage(image, x, y); 
				
				Transform transform = new Transform(display);
				
//				transform.setElements(1, 0, 0, 1, 0 ,0);
				transform.scale(1, 1);
//				transform.translate(0.5f, 0.5f);
//				transform.invert();
				gc.setTransform(transform);
				gc.drawImage(image, x, y); 
				
				x += rect.width + 30;
				
				// Note that the tranform is applied to the whole GC therefore
				// the coordinates need to be adjusted too.
				
				// Reflect around the y axis.
				transform.setElements(-1, 0, 0, 1, 0 ,0);
				gc.setTransform(transform);
				
				//(-30-110-30-110,30)
				gc.drawImage(image, -1*x-rect.width, y);
				
				x = 30; y += rect.height + 30;
				
				// Reflect around the x axis. 
				transform.setElements(1, 0, 0, -1, 0, 0);
				gc.setTransform(transform);
				//(30,-30-60-30-60)
				gc.drawImage(image, x, -1*y-rect.height);
				
				x += rect.width + 30;
				
				// Reflect around the x and y axes	
				transform.setElements(-1, 0, 0, -1, 0, 0);
				gc.setTransform(transform);
				//(-30-110-30-110,-30-60-30-60)
				gc.drawImage(image, -1*x-rect.width, -1*y-rect.height);
				
				
				x = 30; y += rect.height + 30;
				
				// Shear in the x-direction
				transform.setElements(1, 0, -1, 1, 0, 0);
				gc.setTransform(transform);
				gc.drawImage(image, 300, y);
				
				// Shear in y-direction
				transform.setElements(1, -1, 0, 1, 0, 0);
				gc.setTransform(transform);
				gc.drawImage(image, 170, 475);
				
				// Rotate by 45 degrees	
				float cos45 = (float)Math.cos(Math.PI/4);
				float sin45 = (float)Math.sin(Math.PI/4);
				System.out.println(cos45);
				System.out.println(sin45);
				transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);
				gc.setTransform(transform);
				gc.drawImage(image, 250, 150);
				
				/*
				// Shear in x-y-direction
				transform.setElements(1, 1, 0, 1, 0, 0);
				gc.setTransform(transform);
				gc.drawImage(image, 170, 195);
				
				
				// Shear in x-y-direction
				transform.setElements(1, 1, 0, 1, 0, 0);
				gc.setTransform(transform);
				gc.drawImage(image, 280, -85);
				
				// Shear in y-direction
				transform.setElements(1, -1, 0, 1, 0, 0);
//				transform.translate(20, 20);
				gc.setTransform(transform);
				gc.drawImage(image, 280, 755);
				
//				gc.setTransform(transform);
//				gc.drawText("20,20", 20, 20, true);
				
				transform = new Transform(display);
				transform.rotate(30);
				gc.setTransform(transform);
				gc.drawImage(image, 350, 0);
				*/
				
				transform.dispose();
			}
		});
		
		shell.setSize(600, 600);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		image.dispose();
		display.dispose();
	}
}

  

1,Instances of this class represent transformation matrices for points expressed as (x, y) pairs of floating point numbers.
org.eclipse.swt.graphics(package)

??????? 例子:org.eclipse.swt.snippets.Snippet207
??????? 實(shí)現(xiàn):Use transformation matrices to reflect, rotate and shear images

Transform其實(shí)就是實(shí)現(xiàn)了坐標(biāo)的轉(zhuǎn)換,其實(shí)并沒(méi)有改變圖像本身。具體怎么改的我只是猜測(cè),并在猜測(cè)后用程序去驗(yàn)證我的猜測(cè)。
Transform用途有:(這些都是針對(duì)于圖像的)
??????? 1,放大,縮小。
??????? 2,旋轉(zhuǎn)一定得角度。
??????? 3,根據(jù)矩形運(yùn)算完成圖像變換,X對(duì)稱,Y對(duì)稱,Y=X對(duì)稱等等。
注意點(diǎn):
??????? 1,Transform的變換是基于坐標(biāo)系的變換,所以對(duì)所有點(diǎn)有效。這里要特別指出的是圖像的起始坐標(biāo),也應(yīng)跟著變換了。(簡(jiǎn)單想,不是太好理解,要認(rèn)真分析Snippet207就可以看出)
??????? 2,
如何使用:
??????? 1,放大,縮小——scale(3,3)放大為原來(lái)的三倍。 scale(0.5f,0.5f)為1/2. 這里與ImageData的scaledTo不同,ImageData只是針對(duì)于圖像變換,這是坐標(biāo)系變大。
??????? 2,旋轉(zhuǎn)一定得角度——rotate(45) 旋轉(zhuǎn)45度,這里的參數(shù)直接是度數(shù),而Math.cos()里面使用的弧度。坐標(biāo)也轉(zhuǎn)45度。
??????? 3,矩形運(yùn)算——最復(fù)雜也最靈活。setElements(float m11, float m12, float m21, float m22, float dx, float dy)
??????????????? transform.setElements(1, 0, 0, 1, 0 ,0)——原圖不變。
??????????????? transform.setElements(-1, 0, 0, 1, 0 ,0)——得到與原圖Y對(duì)稱的圖。
??????????????? transform.setElements(1, 0, 0, -1, 0 ,0)——得到與原圖X對(duì)稱的圖。
??????????????? transform.setElements(-1, 0, 0, -1, 0 ,0)——得到與原圖Y=x對(duì)稱的圖。
??????????????? transform.setElements(1, 0, -1, 1, 0 ,0)——X軸不變,Y軸向左轉(zhuǎn)45度 的圖。
??????????????? transform.setElements(1, -1, 0, 1, 0 ,0)——Y軸不變,X軸向上轉(zhuǎn)45度 的圖。
?????????
Image中的transformation理解【swt.snippet】
?
??????? 上圖就是Snippet207的效果圖:
??????????????? ①,原圖。
??????????????? ②,Y對(duì)稱。
??????????????? ③,X對(duì)稱。
??????????????? ④,Y=X對(duì)稱。
??????????????? ⑤,X軸不變,Y軸向左轉(zhuǎn)45度。
??????????????? ⑥,Y軸不變,X軸向上轉(zhuǎn)45度。
??????????????? ⑦,X軸Y軸一起順時(shí)針轉(zhuǎn)45度,也可以看著是圖像轉(zhuǎn)。所以,transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);和transform.rotate(30);等價(jià)。

矩陣運(yùn)算的原理:
??????? 首先,看API文檔上說(shuō)了。
??????????????? m11 - the first element of the first row of the matrix
??????????????? m12 - the second element of the first row of the matrix
??????????????? m21 - the first element of the second row of the matrix
??????????????? m22 - the second element of the second row of the matrix
??????????????? dx - the third element of the first row of the matrix
??????????????? dy - the third element of the second row of the matrix
??????? 也就是說(shuō)表示的矩形為,這里先不說(shuō)dx和dy,馬上最后說(shuō)。
??????? 所以這里的坐標(biāo)變換就成了。
??????????????? (1, 0, 0, 1, 0 ,0)——(X,Y)——原圖不變。
??????????????? (-1, 0, 0, 1, 0 ,0)——(-X,Y)——得到與原圖Y對(duì)稱的圖。
??????????????? (1, 0, 0, -1, 0 ,0)——(X,-Y)——得到與原圖X對(duì)稱的圖。
??????????????? (-1, 0, 0, -1, 0 ,0)——(-X,-Y)——得到與原圖Y=x對(duì)稱的圖。
??????????????? (1, 0, -1, 1, 0 ,0)——(X-Y,Y)——X軸不變,Y軸向左轉(zhuǎn)45度 的圖。
??????????????? (1, -1, 0, 1, 0 ,0)——(X,-X+Y)——Y軸不變,X軸向上轉(zhuǎn)45度 的圖。


Image中的transformation理解【swt.snippet】
?

上圖展示了三種對(duì)稱圖。
對(duì)于其他如“(1, 0, -1, 1, 0 ,0)——(X-Y,Y)——X軸不變,Y軸向左轉(zhuǎn)45度 的圖”和“(1, -1, 0, 1, 0 ,0)——(X,-X+Y)——Y軸不變,X軸向上轉(zhuǎn)45度 的圖”我是采用點(diǎn)帶入計(jì)算然后總結(jié)出坐標(biāo)系的變換的。如下圖:

?


Image中的transformation理解【swt.snippet】
?

Image中的transformation理解【swt.snippet】
?

這就是“(1, 0, -1, 1, 0 ,0)——(X-Y,Y)——X軸不變,Y軸向左轉(zhuǎn)45度 的圖”的變換。我取了四個(gè)點(diǎn)【(0,0),(0,Y),(X,0),(X,Y)】這樣就可很明顯地看出等價(jià)的坐標(biāo)系的變換。當(dāng)然“(1, -1, 0, 1, 0 ,0)——(X,-X+Y)——Y軸不變,X軸向上轉(zhuǎn)45度 的圖”也是同理了。所以,在遇到這個(gè)矩陣變換看不懂知道用這種方法就可以看出來(lái)。
最后說(shuō)一下這個(gè)dx和dy,其實(shí)看了上面的坐標(biāo)變換就可以很好想象dx和dy了。dx和dy就是實(shí)現(xiàn)坐標(biāo)系的平移了。加入dx和dy,轉(zhuǎn)換的公式就變成了:

再提一下還有個(gè)函數(shù)translate(float offsetX, float offsetY),這個(gè)的offsetX和offsetY就相當(dāng)于dx和dy,這樣就好理解了。


關(guān)于Transform基本上就講完了,這里多說(shuō)一下應(yīng)用,矩形變化可以應(yīng)用于任何的坐標(biāo)系的變換。
這里補(bǔ)充一下為什么,(X,Y)旋轉(zhuǎn)后,坐標(biāo)變?yōu)榱四兀宽槺銖?fù)習(xí)一下數(shù)學(xué)知識(shí)。
角坐標(biāo)系和直角坐標(biāo)系對(duì)應(yīng)
所以:
當(dāng)然Y也同理了。

?

?

?

Image中的transformation理解【swt.snippet】


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 新龙县| 荔波县| 宝清县| 新宁县| 平山县| 临泉县| 晋中市| 宁陕县| 襄垣县| 论坛| 田阳县| 于都县| 松溪县| 榆树市| 新龙县| 突泉县| 沂南县| 广平县| 新化县| 揭东县| 邢台县| 磴口县| 龙南县| 罗定市| 米脂县| 榆树市| 建平县| 新巴尔虎右旗| 普格县| 天峻县| 太原市| 钦州市| 古田县| 苏尼特右旗| 五华县| 辽宁省| 庆城县| 安丘市| 玉树县| 通城县| 黔东|