生成缩略图

有些地方上传图片要显示出缩略图,所以就有了这个


第一种方法:

package gov.charity.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.gif4j.GifEncoder;


public class Thumbnail {
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		Thumbnail thu = new Thumbnail();
	    File file = new File("C:/welcome.gif"); //大图文件
	    thu.thumbnail(file);
	}
	
	public void thumbnail(File file) throws IOException {
		OutputStream os = new FileOutputStream("C:/1.gif");//生成的缩略图  
	    byte[] imageThumbnail;
	    BufferedImage img = ImageIO.read(file);  
        int h = img.getHeight();  
        int w = img.getWidth();  
           
        int nw = 500;  //width
        int nh = (nw * h) / w;  //height 
        nh = 400;  
        nw = (nh * w) / h;  
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedImage dest = new BufferedImage(nw, nh,BufferedImage.TYPE_4BYTE_ABGR);  
        dest.getGraphics().drawImage(img,0,0,nw, nh,null);  
        GifEncoder.encode(dest, out);
        //ImageIO.write(dest, "gif", out);  
        imageThumbnail = out.toByteArray();  
        os.write(imageThumbnail);
	}

}


其中使用了GifEncoder这也类,对应的jar包就是gif4j.jar,这是个开源的包,做了修改,去掉了恶心的公司logo,现在生成的缩略图没有问题了,连gif的动画也能缩略,强啊

下面是jar包的下载地址


http://download.csdn.net/detail/louxiaomo/5596803











第二种方法(这种方法很方便):

Thumbnailator 是一个为Java界面更流畅的缩略图生成库。从API提供现有的图像文件和图像对象的缩略图中简化了缩略过程,两三行代码就能够从现有图片生成缩略图,且允许微调缩略图生成,同时保持了需要写入到最低限度的代码量。同时还支持根据一个目录批量生成缩略图。 

http://code.google.com/p/thumbnailator/ 

版本:thumbnailator-0.4.2.jar 

原图如下:
 
 

1、指定大小进行缩放 
Java代码   收藏代码
  1. //size(宽度, 高度)  
  2.   
  3. /*   
  4.  * 若图片横比200小,高比300小,不变   
  5.  * 若图片横比200小,高比300大,高缩小到300,图片比例不变   
  6.  * 若图片横比200大,高比300小,横缩小到200,图片比例不变   
  7.  * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300   
  8.  */   
  9. Thumbnails.of("images/a380_1280x1024.jpg")   
  10.         .size(200300)  
  11.         .toFile("c:/a380_200x300.jpg");  
  12.   
  13. Thumbnails.of("images/a380_1280x1024.jpg")   
  14.         .size(25602048)   
  15.         .toFile("c:/a380_2560x2048.jpg");  


2、按照比例进行缩放 
Java代码   收藏代码
  1. //scale(比例)  
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .scale(0.25f)  
  4.         .toFile("c:/a380_25%.jpg");  
  5.   
  6. Thumbnails.of("images/a380_1280x1024.jpg")   
  7.         .scale(1.10f)  
  8.         .toFile("c:/a380_110%.jpg");  


3、不按照比例,指定大小进行缩放 
Java代码   收藏代码
  1. //keepAspectRatio(false) 默认是按照比例缩放的  
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .size(200200)   
  4.         .keepAspectRatio(false)   
  5.         .toFile("c:/a380_200x200.jpg");  


4、旋转 
Java代码   收藏代码
  1. //rotate(角度),正数:顺时针 负数:逆时针  
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .size(12801024)  
  4.         .rotate(90)   
  5.         .toFile("c:/a380_rotate+90.jpg");   
  6.   
  7. Thumbnails.of("images/a380_1280x1024.jpg")   
  8.         .size(12801024)  
  9.         .rotate(-90)   
  10.         .toFile("c:/a380_rotate-90.jpg");   

 

5、水印 
Java代码   收藏代码
  1. //watermark(位置,水印图,透明度)  
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .size(12801024)  
  4.         .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)   
  5.         .outputQuality(0.8f)   
  6.         .toFile("c:/a380_watermark_bottom_right.jpg");  
  7.   
  8. Thumbnails.of("images/a380_1280x1024.jpg")   
  9.         .size(12801024)  
  10.         .watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)   
  11.         .outputQuality(0.8f)   
  12.         .toFile("c:/a380_watermark_center.jpg");  

 

6、裁剪 
Java代码   收藏代码
  1. //sourceRegion()  
  2.   
  3. //图片中心400*400的区域  
  4. Thumbnails.of("images/a380_1280x1024.jpg")  
  5.         .sourceRegion(Positions.CENTER, 400,400)  
  6.         .size(200200)  
  7.         .keepAspectRatio(false)   
  8.         .toFile("c:/a380_region_center.jpg");  
  9.   
  10. //图片右下400*400的区域  
  11. Thumbnails.of("images/a380_1280x1024.jpg")  
  12.         .sourceRegion(Positions.BOTTOM_RIGHT, 400,400)  
  13.         .size(200200)  
  14.         .keepAspectRatio(false)   
  15.         .toFile("c:/a380_region_bootom_right.jpg");  
  16.   
  17. //指定坐标  
  18. Thumbnails.of("images/a380_1280x1024.jpg")  
  19.         .sourceRegion(600500400400)  
  20.         .size(200200)  
  21.         .keepAspectRatio(false)   
  22.         .toFile("c:/a380_region_coord.jpg");  

 

7、转化图像格式 
Java代码   收藏代码
  1. //outputFormat(图像格式)  
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .size(12801024)  
  4.         .outputFormat("png")   
  5.         .toFile("c:/a380_1280x1024.png");   
  6.   
  7. Thumbnails.of("images/a380_1280x1024.jpg")   
  8.         .size(12801024)  
  9.         .outputFormat("gif")   
  10.         .toFile("c:/a380_1280x1024.gif");   


8、输出到OutputStream 
Java代码   收藏代码
  1. //toOutputStream(流对象)  
  2. OutputStream os = new FileOutputStream("c:/a380_1280x1024_OutputStream.png");  
  3. Thumbnails.of("images/a380_1280x1024.jpg")   
  4.         .size(12801024)  
  5.         .toOutputStream(os);  


9、输出到BufferedImage 
Java代码   收藏代码
  1. //asBufferedImage() 返回BufferedImage  
  2. BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .size(12801024)  
  4.         .asBufferedImage();  
  5. ImageIO.write(thumbnail, "jpg"new File("c:/a380_1280x1024_BufferedImage.jpg"));   



ThumbnailatorTest.rar 包的下载地址

http://download.csdn.net/detail/louxiaomo/5596991

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值