imageprocessor.java

来自「一个免费wap站」· Java 代码 · 共 120 行

JAVA
120
字号
package com.eline.wap.catalog.util;

import java.io.File;

import javax.servlet.jsp.PageContext;

import com.blue.imageio.ImageGenerator;
import com.blue.imageio.Size;
import com.eline.wap.catalog.exceptions.CatalogException;
import com.eline.wap.common.util.AppKeys;
import com.eline.wap.common.util.AppSettings;
import com.eline.wap.resource.model.Picture;

public class ImageProcessor {

	protected PageContext pageContext = null;
	protected Picture picture = null;

	/**
	 * Default constructor
	 * @param ctx
	 * @param info
	 */
	public ImageProcessor(PageContext ctx, Picture info) {
		pageContext = ctx;
		picture = info;
	}

	/**
	 * 
	 * @return
	 * @throws CatalogException
	 */
	public String getPreviewImageURL() throws CatalogException {
		return getPreviewImageURL(new Size(50, 50));
	}

	/**
	 * 
	 * @param size
	 * @return
	 * @throws CatalogException
	 */
	public String getPreviewImageURL(Size size) throws CatalogException {
		String[] isArray = picture.getOriginFile().split("/");
		if (isArray.length != 3)	// ;yyyy-MM-dd;hhmmssffff.ext
			throw new CatalogException("Invalid origin image file info.");
		String[] fnArray = isArray[2].split("\\.");
		if (fnArray.length != 2)	// hhmmssffff;ext
			throw new CatalogException("Invalid origin image file name.");

		String imageURL = AppSettings.getInstance().getProperty(AppKeys.UPLOAD_ROOT)
				+ "/images/pictures/"
				+ isArray[1] + "/" + fnArray[0] + "_" + size.getWidth() + "x" + size.getHeight() + ".gif";

		String physicalPath = this.pageContext.getServletContext().getRealPath(imageURL);
		
		if (!(new File(physicalPath).exists()))
			return generateImageURL(size, ImageGenerator.MIME_GIF);

		return AppSettings.getInstance().getProperty(AppKeys.APP_ROOT) + imageURL;
	}

	/**
	 * 
	 * @param size
	 * @param mimeType
	 * @return
	 * @throws CatalogException
	 */
	private String generateImageURL(Size size, String mimeType) throws CatalogException {
		String originImageURL = AppSettings.getInstance().getProperty(
				AppKeys.UPLOAD_ROOT)
				+ "/images/pictures" + this.picture.getOriginFile();
		int i = originImageURL.lastIndexOf(".");
		if (i < 1)
			throw new CatalogException("Invalid origin file info.");

		String newImageURL = originImageURL.substring(0, i) + "_" + size.getWidth() + "x" + size.getHeight() + ".";
		
		newImageURL += mimeType.equals(ImageGenerator.MIME_PNG) ? "png"
				: mimeType.equals(ImageGenerator.MIME_GIF) ? "gif"
						: "jpg";

		String physicalPath = this.pageContext.getServletContext().getRealPath(originImageURL);
		String physicalPath2 = this.pageContext.getServletContext().getRealPath(newImageURL);

		try {
			ImageGenerator.generateImage(physicalPath, physicalPath2, size, mimeType);
		} catch (Exception e) {
			throw new CatalogException(e.getMessage());
		}

		return AppSettings.getInstance().getProperty(AppKeys.APP_ROOT) + newImageURL;
	}
	
	public String getImageURL(Size size, String mime) throws CatalogException {
		String[] isArray = picture.getOriginFile().split("/");
		if (isArray.length != 3)	// ;yyyy-MM-dd;hhmmssffff.ext
			throw new CatalogException("Invalid origin image file info.");
		String[] fnArray = isArray[2].split("\\.");
		if (fnArray.length != 2)	// hhmmssffff;ext
			throw new CatalogException("Invalid origin image file name.");
		
		String extension = mime.equals(ImageGenerator.MIME_PNG) ? "png"
				: mime.equals(ImageGenerator.MIME_GIF) ? "gif" : "jpg";

		String imageURL = AppSettings.getInstance().getProperty(AppKeys.UPLOAD_ROOT)
				+ "/images/pictures/"
				+ isArray[1] + "/" + fnArray[0] + "_" + size.getWidth() + "x" + size.getHeight() + "." + extension;
		
		String physicalPath = this.pageContext.getServletContext().getRealPath(imageURL);
		
		if (!(new File(physicalPath).exists()))
			return generateImageURL(size, mime);

		return AppSettings.getInstance().getProperty(AppKeys.APP_ROOT) + imageURL;
	}
}

⌨️ 快捷键说明

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