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

📄 imageeffect.java

📁 j2me 图象缩小和放大。自动适应屏幕大小
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			// e.printStackTrace();
			ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
			return image;
		} catch (Exception e) {
			e.printStackTrace();
			return image;
		} finally {
			raw = null;
		}
	}

	/***************************************************************************
	 * 
	 * 旋转90度的方法
	 */
	public static Image turn90(Image img) {

		// int[] rgbOutput = null;
		int[] rgbInput = null;
		int width = 0, height = 0;
		int[][] tempArr = null;
		int[][] tempArr1 = null;
		try {

			width = img.getWidth();
			height = img.getHeight();
			rgbInput = new int[width * height];
			// rgbOutput = new int[width * height];
			img.getRGB(rgbInput, 0, width, 0, 0, width, height);
			int i, j, k;
			k = 0;
			tempArr = new int[height][width];
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++)
					tempArr[i][j] = rgbInput[k++];

			rgbInput = null;// 显式地设置为空值,告诉系统可以垃圾回收
			k = 0;
			tempArr1 = new int[width][height];
			for (i = 0; i < height; i++) {
				for (j = 0; j < width; j++) {
					tempArr1[j][i] = tempArr[i][j];
				}
			}
			tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
			// 不调用这个方法,不是顺时针90度
			Image result = verticalMirror(tempArr1, height, width);
			// k = 0;
			// for (i = 0; i < width; i++) {
			// for (j = 0; j < height; j++) {
			// rgbOutput[k] = tempArr1[i][j];
			// k++;
			// }
			// }
			// tempArr1 = null;// 显式地设置为空值,告诉系统可以垃圾回收
			// // 不调用这个方法,不是顺时针90度
			// Image result = verticalMirror(rgbOutput, height, width);
			if (result == null)
				// 水平镜像的时候出问题,内存不够了,返回原来的图,不做修改
				return img;
			else
				return result;
			// return Image.createRGBImage(rgbOutput, height, width, true);

		} catch (OutOfMemoryError e) {
			// e.printStackTrace();
			ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
			return img;
		} finally {
		}

	}

	/***************************************************************************
	 * 
	 * 旋转180度的方法
	 */
	public static Image turn180(Image img) {

		int[] rgbOutput = null;
		int[] rgbInput = null;
		int width = 0, height = 0;
		int[][] tempArr = null;
		try {

			width = img.getWidth();
			height = img.getHeight();
			rgbInput = new int[width * height];
			rgbOutput = new int[width * height];
			img.getRGB(rgbInput, 0, width, 0, 0, width, height);
			int i, j, k;
			k = 0;
			tempArr = new int[height][width];
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++)
					tempArr[i][j] = rgbInput[k++];
			rgbInput = null;// 显式地设置为空值,告诉系统可以垃圾回收
			k = 0;
			for (i = height - 1; i >= 0; i--) {
				for (j = width - 1; j >= 0; j--) {
					rgbOutput[k] = tempArr[i][j];
					k++;
				}
			}
			tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
			// return img;
			return Image.createRGBImage(rgbOutput, width, height, true);

		} catch (OutOfMemoryError e) {
			// e.printStackTrace();
			ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
			return img;
		} finally {
			rgbOutput = null;

		}

	}

	/***************************************************************************
	 * 
	 * 垂直镜像的私有方法,被turn90调用
	 */
	private static Image verticalMirror(int[][] tempArr, int width, int height) {

		int[] rgbOutput = null;
		int[][] tempArr1 = null;
		try {
			rgbOutput = new int[width * height];
			int i, j, k;
			k = 0;
			tempArr1 = new int[height][width];
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++) {
					tempArr1[i][width - 1 - j] = tempArr[i][j];
				}
			tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
			k = 0;
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++) {
					rgbOutput[k] = tempArr1[i][j];
					k++;

				}
			tempArr1 = null;// 显式地设置为空值,告诉系统可以垃圾回收
			// return img;
			return Image.createRGBImage(rgbOutput, width, height, true);
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
			return null;
		} finally {
			rgbOutput = null;

		}

	}

	/***************************************************************************
	 * 
	 * 垂直镜像的私有方法2
	 */
	private static Image verticalMirror(int[] rgbInput, int width, int height) {

		int[] rgbOutput = null;
		int[][] tempArr = null;
		int[][] tempArr1 = null;
		try {
			rgbOutput = new int[width * height];
			int i, j, k;
			k = 0;
			tempArr = new int[height][width];
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++)
					tempArr[i][j] = rgbInput[k++];
			rgbInput = null;// 显式地设置为空值,告诉系统可以垃圾回收
			tempArr1 = new int[height][width];
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++) {
					tempArr1[i][width - 1 - j] = tempArr[i][j];
				}
			tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
			k = 0;
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++) {
					rgbOutput[k] = tempArr1[i][j];
					k++;

				}
			tempArr1 = null;// 显式地设置为空值,告诉系统可以垃圾回收
			// return img;
			return Image.createRGBImage(rgbOutput, width, height, true);
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
			return null;
		} finally {
			rgbOutput = null;

		}

	}

	/***************************************************************************
	 * 
	 * 垂直镜像的方法
	 */
	public static Image verticalMirror(Image img) {

		int[] rgbOutput = null;
		int[] rgbInput = null;
		int width = 0, height = 0;
		int[][] tempArr = null;
		try {
			width = img.getWidth();
			height = img.getHeight();
			rgbInput = new int[width * height];
			rgbOutput = new int[width * height];
			img.getRGB(rgbInput, 0, width, 0, 0, width, height);
			int i, j, k;
			k = 0;
			tempArr = new int[height][width];

			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++)
					tempArr[i][j] = rgbInput[k++];
			rgbInput = null;// 显式地设置为空值,告诉系统可以垃圾回收
			int[][] tempArr1 = new int[height][width];
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++) {
					tempArr1[i][width - 1 - j] = tempArr[i][j];
				}
			tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
			k = 0;
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++) {
					rgbOutput[k] = tempArr1[i][j];
					k++;

				}
			tempArr1 = null;// 显式地设置为空值,告诉系统可以垃圾回收
			// return img;
			return Image.createRGBImage(rgbOutput, width, height, true);
		} catch (OutOfMemoryError e) {
			// e.printStackTrace();
			ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
			return img;
		} finally {
			rgbOutput = null;
		}

	}

	/***************************************************************************
	 * 
	 * 水平镜像的方法
	 */
	public static Image horizontalMirror(Image img) {

		int[] rgbOutput = null;
		int[] rgbInput = null;
		int width = 0, height = 0;
		int[][] tempArr = null;
		int[][] tempArr1 = null;
		try {
			width = img.getWidth();
			height = img.getHeight();
			rgbInput = new int[width * height];
			rgbOutput = new int[width * height];
			img.getRGB(rgbInput, 0, width, 0, 0, width, height);
			int i, j, k;
			k = 0;
			tempArr = new int[height][width];

			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++)
					tempArr[i][j] = rgbInput[k++];
			rgbInput = null;// 显式地设置为空值,告诉系统可以垃圾回收
			tempArr1 = new int[height][width];
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++) {
					tempArr1[height - 1 - i][j] = tempArr[i][j];
				}
			tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
			k = 0;
			for (i = 0; i < height; i++)
				for (j = 0; j < width; j++) {
					rgbOutput[k] = tempArr1[i][j];
					k++;

				}
			tempArr1 = null;// 显式地设置为空值,告诉系统可以垃圾回收
			return Image.createRGBImage(rgbOutput, width, height, true);
		} catch (OutOfMemoryError e) {
			// e.printStackTrace();
			ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
			return img;
		} finally {
			rgbOutput = null;
		}

	}
	
	

	/*
	 * 创建拇指图标的方法
	 */
	public static Image createThumbnail(Image image) {
		int sourceWidth = image.getWidth();
		int sourceHeight = image.getHeight();

		int thumbWidth = 64;
		int thumbHeight = -1;

		if (thumbHeight == -1)
			thumbHeight = thumbWidth * sourceHeight / sourceWidth;

		Image thumb = Image.createImage(thumbWidth, thumbHeight);
		Graphics g = thumb.getGraphics();

		for (int y = 0; y < thumbHeight; y++) {
			for (int x = 0; x < thumbWidth; x++) {
				g.setClip(x, y, 1, 1);
				int dx = x * sourceWidth / thumbWidth;
				int dy = y * sourceHeight / thumbHeight;
				g
						.drawImage(image, x - dx, y - dy, Graphics.LEFT
								| Graphics.TOP);
			}
		}

		Image immutableThumb = Image.createImage(thumb);

		return immutableThumb;
	}

	/***************************************************************************
	 * 放大图片的方法
	 */
	public static Image zoomInImage(Image img) {

		int[] rgbOutput = null;
		int width = 0, height = 0;
		int[] rgbInput = null;
		try {

			width = img.getWidth();
			height = img.getHeight();
			rgbInput = new int[width * height];
			rgbOutput = new int[(width << 1) * (height << 1)];
			img.getRGB(rgbInput, 0, width, 0, 0, width, height);
			int i, j, k;
			k = 0;
			for (i = 0; i < (height << 1); i += 2) {
				for (j = 0; j < (width << 1); j += 2) {
					if (j < 3) {
						// System.out.println(i * (width << 1) + j);
						// System.out.println((i + 1) * (width << 1) + j);
						// System.out.println(i * (width << 1) + j + 1);
						// System.out.println((i + 1) * (width << 1) + j + 1);
					}
					rgbOutput[i * (width << 1) + j] = rgbInput[k];
					rgbOutput[(i + 1) * (width << 1) + j] = rgbInput[k];
					rgbOutput[i * (width << 1) + j + 1] = rgbInput[k];
					rgbOutput[(i + 1) * (width << 1) + j + 1] = rgbInput[k];
					k++;
				}
			}
			return Image.createRGBImage(rgbOutput, width << 1, height << 1,
					true);
		} catch (OutOfMemoryError e) {
			// e.printStackTrace();
			ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
			return img;
		} finally {
			rgbOutput = null;
			rgbInput = null;
		}

	}

	/***************************************************************************
	 * 缩小图片的方法
	 */
	public static Image zoomOutImage(Image img) {

		int[] rgbOutput = null;
		int width = 0, height = 0;
		int[] rgbInput = null;
		try {
			width = img.getWidth();
			height = img.getHeight();
			// System.out.println(width >> 1);
			// System.out.println(height >> 1);
			rgbInput = new int[width * height];
			rgbOutput = new int[(width >> 1) * (height >> 1)];
			img.getRGB(rgbInput, 0, width, 0, 0, width, height);
			int i, j, k, k1;
			k = 0;
			k1 = 0;
			// System.out.println(height);
			// System.out.println(width);
			for (i = 0; i < height; i++) {
				for (j = 0; j < width; j += 2) {
					if (i % 2 == 0) {
						if (k1 > width * height - 1
								|| k > (width >> 1) * (height >> 1) - 1) {
							// System.out.println("k=" + k);
							// System.out.println("k1=" + k1);
							break;
						}
						rgbOutput[k] = rgbInput[k1];
						k++;
						k1 += 2;
						// System.out.println(k);
						// System.out.println(k1);
					} else {
						k1 += 2;

					}
				}
			}

			return Image.createRGBImage(rgbOutput, width >> 1, height >> 1,
					true);
		} catch (OutOfMemoryError e) {
			// e.printStackTrace();
			ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
			return img;
		} finally {
			rgbOutput = null;
			rgbInput = null;
		}

	}

}

⌨️ 快捷键说明

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