⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 morphingimage.java

📁 可以实现两张图片渐变过程的MORPHING
💻 JAVA
字号:
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.util.TimerTask;
import javax.swing.ImageIcon;
 
class MorphingImage {
	  	
	  	protected double t=0;
	  	protected int frames = 0;
	  	protected int frameNum = 1;
	  	protected Image sourceImage;
	  	protected Image targetImage;
	  	protected Image currentImage;
	  	protected int [] sourcePixels = null;
	  	protected int [] destPixels = null;
	  	protected int imageWidth=0, imageHeight=0;
	  	
	  	public MorphingImage() {
	  		frames=50;
	  	}
	  	
	  	public void setFrames(int f) {
	  		if( f > 0 ) {
	  			frames = f;
	  		}
	  	}
	  	public void setFrameNum(int frameNum) {
	  		if( frameNum>=0 && frameNum <=this.frames ) {
	  			this.frameNum = frameNum;
	  		}
	  	}
	  	
	  	public Image getCurrentImage() {
	  		return currentImage;
	  	}
	  	public Image getSourceImage() {
	  		return sourceImage;
	  	}
	  	public Image getTargetImage() {
	  		return targetImage;
	  	}
	  	
	  
	  	public void setSourceImage( Image i ) throws Exception {
	  		sourceImage = i;
	  		int h,w;
	  		if( sourceImage != null ) {
	  			h = sourceImage.getHeight(null);
	  			w = sourceImage.getWidth(null);
	  			if( targetImage != null ) {
	  				if( h!=imageHeight || w!=imageWidth) {
	  					sourceImage = null;
	  					throw new Exception();
	  				}
	  			}
	  			imageHeight = h;
	  			imageWidth  = w;
	  			sourcePixels = new int[imageHeight*imageWidth];
	  			PixelGrabber pixelGrabber = 
	  				new PixelGrabber(sourceImage,0,0,imageWidth,imageHeight,
	  						sourcePixels,0,imageWidth);
	  			try {
	  			    pixelGrabber.grabPixels();
	  			} catch (InterruptedException e) {
	  			    System.err.println("interrupted waiting for pixels!");
	  			    return;
	  			}
	  			if ((pixelGrabber.getStatus() & ImageObserver.ABORT) != 0) {
	  			    System.err.println("image fetch aborted or errored");
	  			    return;
	  			}
	  		}
	  	}
	  
	  	public void setTargetImage( Image i ) throws Exception{
	  		targetImage = i;
	  		int h,w;
	  		if( targetImage != null ) {
	  			h = targetImage.getHeight(null);
	  			w = targetImage.getWidth(null);
	  			if( sourceImage != null ) {
	  				if( h!=imageHeight || w!=imageWidth) {
	  					targetImage = null;
	  					throw new Exception();
	  				}
	  			}
	  			imageHeight = h;
	  			imageWidth  = w;
	  			destPixels = new int[imageHeight*imageWidth];
	  			PixelGrabber pixelGrabber = 
	  				new PixelGrabber(targetImage,0,0,imageWidth,imageHeight,
	  						destPixels,0,imageWidth);
	  			try {
	  				pixelGrabber.grabPixels();
	  			} catch (InterruptedException e) {
	  				System.err.println("interrupted waiting for pixels!");
	  				return;
	  			}
	  			if ((pixelGrabber.getStatus() & ImageObserver.ABORT) != 0) {
	  				System.err.println("image fetch aborted or errored");
	  				return;
	  			}	
	  		}
	  	}
	  	
	  	
	  	
	  	
	  	public void morphing() {
	  		if( sourcePixels != null && destPixels != null ) {
	  			if( frameNum <= frames ) {
	  				t = (1.0/frames)*frameNum;
	  				frameNum++;
	  			}else {
	  				frameNum = 0;
	  				t = 0;
	  			}
	  			int [] currentPixels=new int[imageWidth*imageHeight];
	  			for( int w=0; w<imageWidth; w++ ) {
	  				for( int h=0; h<imageHeight; h++ ) {
	  					currentPixels[h*imageWidth+w] =
	  					translation(sourcePixels[h*imageWidth+w],
	  						destPixels[h*imageWidth+w]);
	  				}
	  			}
	  			currentImage = Toolkit.getDefaultToolkit().createImage
	  	        (new MemoryImageSource(imageWidth, imageHeight,
	  	        		currentPixels, 0, imageWidth));
	  		}
	  	}
	  	
	  	private int translation( int source, int dest ) {
	  		int source_alpha, dest_alpha, current_alpha; 
	  		int source_red,   dest_red,   current_red;
	  		int source_green, dest_green, current_green; 
	  		int source_blue,  dest_blue,  current_blue;
	  		source_alpha = (source >> 24) & 0xff;
	  		source_red   = (source >> 16) & 0xff;
	  		source_green = (source >>  8) & 0xff;
	  		source_blue  = (source      ) & 0xff;
	  		dest_alpha   = (dest >> 24) & 0xff;
	  		dest_red     = (dest >> 16) & 0xff;
	  		dest_green   = (dest >>  8) & 0xff;
	  		dest_blue    = (dest      ) & 0xff;	
	  		current_alpha=(int)(source_alpha*(1-t)+dest_alpha*t)<<24;
	  		current_red  =(int)(source_red*(1-t)+dest_red*t)    <<16;
	  		current_green=(int)(source_green*(1-t)+dest_green*t)<<8;
	  		current_blue =(int)(source_blue*(1-t)+dest_blue*t);
	  		return (current_alpha+current_red+current_green+current_blue);
	  	}
	  	
	  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -