Java小程序:批量图片处理(缩小和添加Logo)

阅读更多
【虎.无名】喜欢写Blog,经常上传照片,问题是照片通常都太大,相册空间又太小,通常都要做一些压缩处理。虽然windows自带的画图工具也可以完成,但是需要手工一个一个完成,我又是比较懒的人,所以,写了下面一个小工具,把图片按比例缩小x倍,或者添加一个Logo文字,关键是能批量处理,省事多了。如果需要使用,得改改main,代码很简单,我就不写详细使用手册了。

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import javax.imageio.ImageIO;

public class JpgTest {
	public static void jpg_logo(String jpgName, String logoText)	throws Exception {
		File _file = new File(jpgName+".jpg");	//读入文件
		Image src = ImageIO.read(_file);	//构造Image对象
		int w0 = src.getWidth(null);		//得到源图宽
		int h0 = src.getHeight(null); 		//得到源图长
		int w2 = 800;	//=w0/2
		int h2 = 600;	//=h0/2
		int fontSize = 32;
		//缩小一 半为(800,600)
		BufferedImage tag = null;
		tag = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(src, 0, 0,w2, h2, null);	//绘制缩小后的图
		//标注水印
	        Graphics g = tag.getGraphics();
		g.setColor(Color.RED);		//以下设置前景色BLACK
	//	g.setXORMode(Color.RED);
	        g.setFont(new Font("MyFont", Font.BOLD, fontSize));	//PLAIN,BOLD,ITALIC
	//	g.drawString(logoText, 10, 10+fontSize);
	        g.drawString(logoText, w2-fontSize*(logoText.length()+3)/2, h2-10);
	        g.dispose();
	        //保存文件,输出到文件流
	        FileOutputStream out = new FileOutputStream(jpgName+"_800.jpg");	
	        try {
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
			encoder.encode(tag); //近JPEG编码
			//System.out.print(width+"*"+h0); 
		}finally{
			out.close();
		}
	}
	public static void jpg_logo()	throws Exception {
		File _file = new File("test_old.jpg");	//读入文件
		Image src = ImageIO.read(_file);	//构造Image对象
		int w0=src.getWidth(null);		//得到源图宽
		int h0=src.getHeight(null); 	//得到源图长
		//缩小一半
		BufferedImage tag = null;
		tag = new BufferedImage(w0/2,h0/2,BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(src,0,0,w0/2,h0/2,null);	//绘制缩小后的图
		//标注水印
	        Graphics g = tag.getGraphics();
		//g.setColor(Color.BLACK);	 //以下设置前景色
	        g.setXORMode(Color.GREEN);
	        g.setFont(new Font("MyFont", Font.ITALIC, 24));
	        g.drawString("www.Test.com", w0/4, h0/4);
	        g.dispose();
	        //保存文件
	        FileOutputStream out = new FileOutputStream("test_new.jpg");	//输出到文件流
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
		encoder.encode(tag); //近JPEG编码
		//System.out.print(width+"*"+h0); 
		out.close();
	}
	public static void jpg_mini(String fn, int factor)	throws Exception {
		String	f0 = fn+".jpg";
		File	f = new File(f0);	//读入文件
		Image src = ImageIO.read(f);	//构造Image对象
		int w0 = src.getWidth(null);	//得到源图宽
		int h0 = src.getHeight(null); 	//得到源图长
		if (w0<800)	throw new Exception("w0<800");
		//-------------------------
		String	f2 = fn+"_.jpg";
		int w2 = w0 / factor;
		int h2 = h0 / factor;
		BufferedImage tag = new BufferedImage(w2, h2,BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(src, 0, 0, w2, h2, null);	//绘制缩小后的图
		System.out.println(f0+"("+w0+"*"+h0+") \t=> "+f2+"("+w2+"*"+h2+")");
	        //保存文件
	        FileOutputStream out = new FileOutputStream(f2);	//输出到文件流
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
		encoder.encode(tag); //近JPEG编码
		out.close();
	}
	public static void jpg_rename(String fn)	throws Exception {
		File	f0 = new File(fn+".jpg");
			f0.delete();
		File	f2 = new File(fn+"_.jpg");
			f2.renameTo(f0);
	}
	public static String zeroInt(int n, int len) {
		String	s = ""+n;
		for(int i=s.length(); i 

你可能感兴趣的:(Java,Windows,SUN,F#,Blog)