图片处理程序_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 图片处理程序

图片处理程序

 2013/11/6 19:10:52  风-起-了  程序员俱乐部  我要评论(0)
  • 摘要:大量的图片如何统一处理成固定宽度、或者固定长宽的图片,java中的java.awt包下面的一些类就可以实现图片处理的功能,下面就是自己写的一个简单的实例下面的类是单张图片的处理,里面的参数含义可以参照参数命名:/***@Description:压缩或放大图像处理*/packagecn.lulei.image.deal;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics2D;importjava.awt.Image
  • 标签:程序 图片

? ? 大量的图片如何统一处理成固定宽度、或者固定长宽的图片,java中的java.awt包下面的一些类就可以实现图片处理的功能,下面就是自己写的一个简单的实例

?

下面的类是单张图片的处理,里面的参数含义可以参照参数命名:

class="java" name="code"> /**  
 *@Description: 压缩或放大图像处理
 */ 
package cn.lulei.image.deal;  

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
  
public class DealImage {

	private File file = null;
	private String inputDir;
	private String outputDir;
	private String inputFileName;
	private String outputFileName;
	private int outputWidth;
	private int outputHeight;
	private boolean proportion;//是否等比压缩
	
	public DealImage() {
		this.inputDir = "";
		this.outputDir = "";
		this.inputFileName = "";
		this.outputFileName = "";
		this.outputHeight = 200;
		this.outputWidth = 200;
		this.proportion = true;
	}
	
	public void setInputDir(String inputDir){
		this.inputDir = inputDir;
	}
	public void setOutputDir(String outputDir){
		this.outputDir = outputDir;
	}
	public void setInputFileName(String inputFileName){
		this.inputFileName = inputFileName;
	}
	public void setOutputFileName(String outputFileName){
		this.outputFileName = outputFileName;
	}
	public void setWidthAngHeight(int outputWidth, int outputHeight){
		this.outputWidth = outputWidth;
		this.outputHeight = outputHeight;
	}
	
	/**
	 * @param isVip
	 * @Date: 2013-8-7  
	 * @Author: lulei  
	 * @Description:处理图片
	 */
	private void dealImage(boolean isVip) {
		try {
			this.file = new File(this.inputDir + this.inputFileName);
			if (!this.file.exists()){
				return;
			}
			Image img = ImageIO.read(this.file);
			if (img.getWidth(null) == -1) {
				return;
			}
			int[] widthAndHeight = this.getNewWidthAndHeight(img);
			BufferedImage bufferedImage = new BufferedImage(widthAndHeight[0], widthAndHeight[1], BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(img.getScaledInstance(widthAndHeight[0], widthAndHeight[1], Image.SCALE_SMOOTH), 0, 0, null);
			if (!isVip) {
				g.setFont(new Font("楷体", Font.ITALIC, 14));
				g.setColor(Color.getHSBColor(1.0f, 0.3f, 1.0f));
				g.drawString("想要添加的水印", widthAndHeight[0]/3, widthAndHeight[1]/2);
			}
			File fileOut = new File(this.outputDir);
			if (!fileOut.exists()) {
				fileOut.mkdirs();
			}
			FileOutputStream fileOutputStream = new FileOutputStream(this.outputDir + this.outputFileName);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fileOutputStream);
			encoder.encode(bufferedImage);
			g.dispose();
			fileOutputStream.close();
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println("There're some errors when handling picture which path is :" + this.inputDir + this.inputFileName);
		}
	}
	
	/**
	 * @param inputDir
	 * @param inputFileName
	 * @param outputDir
	 * @param outputFileName
	 * @param isVip
	 * @Date: 2013-8-7  
	 * @Author: lulei  
	 * @Description:处理图片
	 */
	public void dealImage(String inputDir, String inputFileName, String outputDir, String outputFileName, boolean isVip) {
		this.dealImage(inputDir, inputFileName, outputDir, outputFileName, this.outputWidth, this.outputHeight, this.proportion, isVip);
	}
	
	/**
	 * @param inputDir
	 * @param inputFileName
	 * @param outputDir
	 * @param outputFileName
	 * @param outputWidth
	 * @param outputHeight
	 * @param isVip
	 * @Date: 2013-8-7  
	 * @Author: lulei  
	 * @Description:处理图片
	 */
	public void dealImage(String inputDir, String inputFileName, String outputDir, String outputFileName, int outputWidth, int outputHeight, boolean isVip) {
		this.dealImage(inputDir, inputFileName, outputDir, outputFileName, outputWidth, outputHeight, this.proportion, isVip);
	}
	
	/**
	 * @param inputDir
	 * @param inputFileName
	 * @param outputDir
	 * @param outputFileName
	 * @param outputWidth
	 * @param outputHeight
	 * @param proportion
	 * @param isVip
	 * @Date: 2013-8-7  
	 * @Author: lulei  
	 * @Description:处理图片
	 */
	public void dealImage(String inputDir, String inputFileName, String outputDir, String outputFileName, int outputWidth, int outputHeight, boolean proportion, boolean isVip) {
		this.inputDir = inputDir;
		this.inputFileName = inputFileName;
		this.outputDir = outputDir;
		this.outputFileName = outputFileName;
		this.outputWidth = outputWidth;
		this.outputHeight = outputHeight;
		this.proportion = proportion;
		this.dealImage(isVip);
	}
	
	/**
	 * @param img
	 * @return
	 * @Date: 2013-8-7  
	 * @Author: lulei  
	 * @Description:返回压缩后的图片宽和高
	 */
	private int[] getNewWidthAndHeight(Image img){
		int[] reInt = new int[2];
		if (this.proportion) {
			double rateW = ((double)img.getWidth(null)) / (double)this.outputWidth + 0.1;
			double rateH = ((double)img.getHeight(null)) / (double)this.outputHeight + 0.1;
			double rate = rateW > rateH ? rateW :rateH;
			reInt[0] = (int)(((double)img.getWidth(null)) / rate);
			reInt[1] = (int)(((double)img.getHeight(null)) / rate);
		} else {
			reInt[0] = this.outputWidth;
			reInt[1] = this.outputHeight;
		}
		return reInt;
	}
}

?

?

下面的类是对图片文件或者图片文件夹做统一的处理:

 /**  
 *@Description:   批处理文件夹下的所有图片  
 */ 
package cn.lulei.image.deal;  

import java.io.File;

import cn.lulei.image.deal.DealImage;
  
  
public class DealImageFile {
	
	//图片后缀名
	private static String[] IMG_TYPE = {".jpg",".bmp",".jpeg",".png",".pcx",".tiff",".gif",".tga",".exif"
			,".fpx",".svg",".psd","cdr",".pcd",".dxf",".ufo",".eps",".hdri",".ai",".raw"}; 
	private static int outputHeight = 200;
	private static int outputWidth = 200;
	private static int count = 0;
	private static boolean proportion = true;
	
	public String dealImageFile(String inputDir, String outputDir) {
		return this.dealImageFile(inputDir, outputDir, outputWidth, outputHeight, proportion);
	}
	
	public String dealImageFile(String inputDir, String outputDir, int outputWidth, int outputHeight) {
		return this.dealImageFile(inputDir, outputDir, outputWidth, outputHeight, proportion);
	}
	/**
	 * @param inputDir
	 * @param outputDir
	 * @param outputWidth
	 * @param outputHeight
	 * @param proportion
	 * @return
	 * @Date: 2013-8-7  
	 * @Author: lulei  
	 * @Description:批处理文件夹下的文件
	 */
	public String dealImageFile(String inputDir, String outputDir, int outputWidth, int outputHeight, boolean proportion) {
		File fileDir = new File(inputDir);
		File[] files = fileDir.listFiles();
		DealImage dealImage = new DealImage();
		for (File file : files) {
			String imgPath = file.toString().toLowerCase();
			String fileName = imgPath.substring(inputDir.length());
			if (this.isImage(imgPath)) {
				dealImage.dealImage(inputDir, fileName, outputDir, fileName, outputWidth, outputHeight, proportion, true);
				count++; 
			} else if (file.isDirectory()) {
				if (!imgPath.equals(outputDir.toLowerCase())) {
					//这里是递归
					this.dealImageFile(imgPath, outputDir + "/" + fileName, outputWidth, outputHeight, proportion);
				}
			}
		}
		return "Handle " + count + " pictures! ";
	}
	
	private boolean isImage(String imgPath) {
		for (String str : IMG_TYPE) {
			if (imgPath.endsWith(str)) {
				return true;
			}
		}
		return false;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub  
		
	}

}

?

上面的这些代码自己做了简单的测试,可以直接处理大部分的图像文件

发表评论
用户名: 匿名