imageutils.java

来自「example2 众多JAVA实例源码...学习java基础的好帮手」· Java 代码 · 共 88 行

JAVA
88
字号
package opusmicro.demos.backgroundimage;

import javax.microedition.lcdui.Image;

public class ImageUtils {
	 public final static Image zoomImage(Image src, int desW, int desH) {
		 Image desImg = null;
		 int srcW = src.getWidth(); // source image width
		 int srcH = src.getHeight(); // source image height
		 int[] srcBuf = new int[srcW * srcH]; // source image pixel
		 src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
		 // compute interpolation table
		 int[] tabY = new int[desH];
		 int[] tabX = new int[desW];
		 int sb = 0;
		 int db = 0;
		 int tems = 0;
		 int temd = 0;
		 int distance = srcH > desH ? srcH : desH;
		 for ( int i = 0 ; i <= distance ; i++) { /* vertical direction */
			 tabY[db] = sb;
			 tems += srcH;
			 temd += desH;
			 if ( tems > distance) {
				 tems -= distance;
				 sb++;
			 }
			 if ( temd > distance) {
				 temd -= distance;
				 db++;
			 }
		 }
		 sb = 0;
		 db = 0;
		 tems = 0;
		 temd = 0;
		 distance = srcW > desW ? srcW : desW;
		 for ( int i = 0 ; i <= distance ; i++) { /* horizontal direction */
			 tabX[db] = (short) sb;
			 tems += srcW;
			 temd += desW;
			 if ( tems > distance) {
				 tems -= distance;
				 sb++;
			 }
			 if ( temd > distance) {
				 temd -= distance;
				 db++;
			 }
		 }
		 // set transparence
//		 int a= 100;//set the transparence of pixel 100
//		 for(int i=0;i<srcBuf.length;i++){
//			srcBuf[i]=(a<<24) | (srcBuf[i] & 0x00FFFFFF);// modify the highest 2 value
//			}
		 
		 // formation enlarge and shorten buffer pixel
		 int[] desBuf = new int[desW * desH];
		 int dx = 0;
		 int dy = 0;
		 int sy = 0;
		 int oldy = -1;
		 for ( int i = 0 ; i < desH ; i++) {
			 if ( oldy == tabY[i]) {
				 System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
			 }
			 else {
				 dx = 0;
				 for ( int j = 0 ; j < desW ; j++) {
					 desBuf[dy + dx] = srcBuf[sy + tabX[j]];
					 dx++;
				 }
				 sy += (tabY[i] - oldy) * srcW;
			 }
			 oldy = tabY[i];
			 dy += desW;
		 }
		 
//		 int a = 100;
//		 for(int i=0;i<desBuf.length;i++){
//			 desBuf[i]=(a<<24) |(desBuf[i] & 0x00FFFFFF);
//		 }
		 
		 desImg = Image.createRGBImage(desBuf, desW, desH, true);
		 return desImg;
	 }
}

⌨️ 快捷键说明

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