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

📄 qimage.java

📁 j2me 高级UI
💻 JAVA
字号:
/**
 * 作者:cat 戚永城
 * 时间:2008-3-1
 * QQ:415898635
 * E-Mail:	415898635@qq.com
 * 			qyc_12345@163.com
 * 
 * */
package org.qui.image;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class QImage {

	/**
	 * 画渐变的图块
	 * */
	public static final int Horizontal = 0;

	public static final int Vertical = 0;

	public static void drawGradual(Graphics graphics, int src_r, int src_g, int src_b,
			int dst_r, int dst_g, int dst_b, int perPix, int x, int y,
			int width, int height, int style) {
		// TODO 自动生成方法存根
		int r = 0, g = 0, b = 0;
		int length = 0;
		if (style == Horizontal) {
			length = height / perPix;
		} else {
			length = width / perPix;
		}
		for (int i = 0; i < length; i++) {
			r = src_g + (dst_g - src_g) * i / ((length-1)/perPix );
			g = src_b + (dst_b - src_b) * i / ((length-1)/perPix );
			b = src_b + (dst_b - src_b) * i / ((length-1)/perPix );
			graphics.setColor(r, g, b);
			if (style == Horizontal) {
				graphics.drawLine(x, y+i, x+width, y+i);
			} else {
				graphics.drawLine(x+i, y, x+i, y+height);
			}
		}
	}
	
	/**
	 * 1.0方法
	 * 由于该算法中使用了Image.createImage(w,h) 来创建图像,这个函数会创建一个w*h像素的全白可变图像,所以透明图片放大缩小后,
	 * 背景不再透明,而是白色了,这是j2me本身的缺憾和算法没有关系。 对于这个问题,有一个解决办法,就是程序的图片不采用Image来保存,
	 * 而是采用short[]数组保存,画图的时候用drawPixels()来画图。
	 */
	public static final Image transImage(Image srcImage, int newW, int newH) {
		int srcW = srcImage.getWidth();
		int srcH = srcImage.getHeight();
		// 先做水平方向上的伸缩变换
		Image tmp = Image.createImage(newW, srcH);
		Graphics g = tmp.getGraphics();

		for (int x = 0; x < newW; x++) {
			g.setClip(x, 0, 1, srcH);
			// 按比例放缩
			g.drawImage(srcImage, x - x * srcW / newW, 0, Graphics.LEFT
					| Graphics.TOP);

		}

		// 再做垂直方向上的伸缩变换
		Image dst = Image.createImage(newW, newH);
		g = dst.getGraphics();

		for (int y = 0; y < newH; y++) {
			g.setClip(0, y, newW, 1);
			// 按比例放缩
			g.drawImage(tmp, 0, y - y * srcH / newH, Graphics.LEFT
					| Graphics.TOP);
		}

		return dst;
	}
	
	/**
	 * 2.0方法 用生成RGBImage解决
	 * */
	public static Image zoomImage(Image src, int desW, int desH) {
		Image desImg = null;
		int srcW = src.getWidth(); // 原始图像宽
		int srcH = src.getHeight(); // 原始图像高
		int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存

		src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);

		// 计算插值表
		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++) { /* 垂直方向 */
			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++) { /* 水平方向 */
			tabX[db] = (short) sb;
			tems += srcW;
			temd += desW;
			if (tems > distance) {
				tems -= distance;
				sb++;
			}
			if (temd > distance) {
				temd -= distance;
				db++;
			}
		}

		// 生成放大缩小后图形像素buf
		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;
		}

		// 生成图片		
		desImg = Image.createRGBImage(desBuf, desW, desH, true);
		return desImg;
	}
	//按比例缩放图片
	public static Image scaleImage(Image src, int scales1, int scales2) {
		return zoomImage(src, src.getWidth() * scales1 / scales2, src
				.getHeight()
				* scales1 / scales2);
	}
	
	// 获得半透明图片,透明度从0到10共分为11个等级
	public static final Image alfImage(Image src_img, int alf) {
		if (src_img == null) {
			System.out.println("alfImage");
			return null;
		}
		if (alf < 0)
			alf = 0;
		else if (alf > 10)
			alf = 10;
		int imgW = src_img.getWidth();
		int imgH = src_img.getHeight();
		int[] RGBData = new int[imgW * imgH];
		src_img.getRGB(RGBData, 0, imgW, 0, 0, imgW, imgH);
		int tmp = ((alf * 255 / 10) << 24) | 0x00ffffff;
		for (int i = 0; i < RGBData.length; i++)
			RGBData[i] &= tmp;
		Image dst_Img = Image.createRGBImage(RGBData, imgW, imgH, true);
		return dst_Img;
	}

	// 得到灰度图
	public static final Image grayImage(Image img) {
		if (img == null) {
			System.out.println("grayAlfImage");
			return null;
		}
		int imgW = img.getWidth();
		int imgH = img.getHeight();
		int[] imgRGBData = new int[imgW * imgH];
		img.getRGB(imgRGBData, 0, imgW, 0, 0, imgW, imgH);
		int ALF = 0;
		int R = 0;
		int G = 0;
		int B = 0;
		int GRAY = 0;
		for (int i = 0; i < imgRGBData.length; i++) {
			ALF = (imgRGBData[i] >> 24) & 0xFF;
			R = (imgRGBData[i] >> 16) & 0xFF;
			G = (imgRGBData[i] >> 8) & 0xFF;
			B = imgRGBData[i] & 0xFF;
			GRAY = (R * 77 + G * 151 + B * 28 + 128) >> 8;
			imgRGBData[i] = (ALF << 24) | (GRAY << 16) | (GRAY << 8) | GRAY;
		}
		return Image.createRGBImage(imgRGBData, imgW, imgH, true);
	}

}

⌨️ 快捷键说明

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