/**
*
*/
package com.fkhwl.fkhserver.core.utils; import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; /**
* @ClassName: ThumbnailTools
* @Description: 缩略图生成工具
* @author
* @date 2014年9月25日 下午5:18:33
*/
public class ThumbnailTools { private int fileSize;
private String inPath; // 输入图路径
private String outPath; // 输出图路径
private int width = 100; // 默认输出图片宽
private int height = 100; // 默认输出图片高
private String inFileName; // 输入图文件名
private String outFileName; // 输出图文件名
private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)
private String prefix = "thumbnail_"; public int getWidth() {
return width;
} public void setWidth(int width) {
this.width = width;
} public int getHeight() {
return height;
} public void setHeight(int height) {
this.height = height;
} public ThumbnailTools setSize(int size){
this.width = size;
this.height = size;
return this;
} public ThumbnailTools setSize(int width, int height){
this.width = width;
this.height = height;
return this;
} public String getInPath() {
return inPath;
} public void setInPath(String inPath) {
this.inPath = inPath;
} public String getOutPath() {
return outPath;
} public void setOutPath(String outPath) {
this.outPath = outPath;
} public boolean isProportion() {
return proportion;
} public void setProportion(boolean proportion) {
this.proportion = proportion;
} public ThumbnailTools(String path){
this.inPath = path;
this.outPath = path;
} public ThumbnailTools(String inPath, String outPath){
this.inPath = inPath;
this.outPath = outPath;
} /**
* 生成缩略图
* @param fileName 文件名
* @return boolean
*/
public boolean generate(String fileName) throws Exception{
this.generate(fileName, null);
return Boolean.TRUE;
} /**
* 生成缩略图
* @param fileName 文件名
* @param outFileName 输出文件名
* @return boolean
*/
public boolean generate(String fileName, String outFileName) throws Exception{
File file = new File(inPath+fileName);
this.inFileName = fileName;
this.outFileName = null == outFileName ? prefix+inFileName : outFileName;
this.execute(new FileInputStream(file));
return Boolean.TRUE;
} public boolean generate(InputStream inputStream, String outPath) throws Exception{
this.execute(inputStream); return Boolean.TRUE;
} private void execute(InputStream inputStream) throws Exception {
this.fileSize = inputStream.available();
BufferedImage oldImage = ImageIO.read(inputStream);
if (oldImage.getWidth() == -1) {
System.out.println("Input image cant't read or format error.");
return;
} int newWidth;
int newHeight;
// 是否是等比缩放
if (this.proportion == true) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) oldImage.getWidth()) / (double) width;
double rate2 = ((double) oldImage.getHeight()) / (double) height;
// 根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) oldImage.getWidth()) / rate);
newHeight = (int) (((double) oldImage.getHeight()) / rate);
} else {
newWidth = width; // 输出的图片宽度
newHeight = height; // 输出的图片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB); // Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
tag.getGraphics().drawImage(oldImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
String newFilePath = outPath+outFileName;
File newFile = new File(newFilePath);
FileOutputStream out = new FileOutputStream(newFile);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
inputStream.close();
out.close(); System.out.println(newFilePath+" ok, size: "+(fileSize/1024)+"kb to "+(newFile.length()/1024)+"kb");
} public static void main(String[] args) {
try {
new ThumbnailTools("D:/").setSize(300, 200).generate("1.jpg", "2.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}

大家在运行的时候或许会报错,因为该程序需要java两个jar包的支持

在Eclipse中处理图片,需要引入两个包:

import com.sun.image.codec.jpeg.JPEGCodec;


import com.sun.image.codec.jpeg.JPEGImageEncoder;


报错:


Access
restriction: The type JPEGImageEncoder is not accessible due to
restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar

此时解决办法:
Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated
and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

java生成生成图片缩略图的更多相关文章

  1. JAVA生成图片缩略图、JAVA截取图片局部内容

    package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...

  2. java图片裁剪和java生成缩略图

    一.缩略图 在浏览相冊的时候.可能须要生成相应的缩略图. 直接上代码: public class ImageUtil { private Logger log = LoggerFactory.getL ...

  3. PHP一般情况下生成的缩略图都比较不理想

    PHP用GD库生成高质量的缩略图片,PHP一般情况下生成的缩略图都比较不理想.今天试用PHP,GD库来生成缩略图.虽然并不100%完美.可是也应该可以满足缩略图的要求了.<?php $FILEN ...

  4. PHP原生写的生成图片缩略图类

    PHP原生写的生成图片缩略图类,本文以京东商品图片为例,分别生成三种不同尺寸的图片.调用方法很简单只要传参数高度和宽度,及新图片的名称. 引入缩略图类 include_once 'ImageResiz ...

  5. java 生成二维码、可带LOGO、可去白边

      1.准备工作 所需jar包: JDK 1.6: commons-codec-1.11.jar core-2.2.jar javase-2.2.jar JDK 1.7: commons-codec- ...

  6. java 生成二维码后叠加LOGO并转换成base64

      1.代码 见文末推荐 2.测试 测试1:生成base64码 public static void main(String[] args) throws Exception { String dat ...

  7. springboot搭建项目,实现Java生成随机图片验证码。

    这篇文章主要介绍了如何通过Java如何生成验证码并验证.验证码的作用我想必大家都知道,话不多说开始实施! 首先创建一个springboot项目以下是项目结构,内有utli工具类.存放生成图片验证码方法 ...

  8. c#.net 生成清晰缩略图

    1 public void imgsize() 2 { 3 //本例中假定了两个变量: 4 5 String src = "c:/myImages/a.jpg"; //源图像文件的 ...

  9. Java生成和操作Excel文件(转载)

    Java生成和操作Excel文件   JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该A ...

随机推荐

  1. (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  2. Swift 为你的webView定制标题

    有些情况下,应用中会使用webView来加载大段的文字,而且还是带各种标签的. 不能全部过滤掉,那样的话,内容就会失去原本想表达的格式. 可是,如果webView中并没有将内容的标题或其他杂项包含进那 ...

  3. 《CSS3秘籍》(第三版)-读书笔记(3)

    第9章 装饰网站导航 1.  选择要定义样式的链接 大部分浏览器都支持4种基本的链接状态:未访问的链接.已访问的链接.访问者的鼠标正悬停在上方的链接,以及正被单击的链接.CSS提供了与这些状态对应的4 ...

  4. POJ3694 Network

    题目大意:已知连通图G有N个点m条无向边,有Q次操作,每次操作为增加一条边,问每次操作后图上有几个桥. 如果添加一条边进行Tarjin搜索一次时间复杂度为m*m*q很大,会超时.真的超时,我试过.看了 ...

  5. 在updatepanel使用colorbox无效

    今天在给一个使用colorbox的页面加了一个updatepanel后,colorbox无效了,原因是以前的页面初始化colorbox是用 $(document).ready(function(){ ...

  6. sublime text3 编译less

    1,  下载 插件   链接:http://pan.baidu.com/s/1bNbFJK 密码:m3zt 2,解压后把lessc文件夹放到sublime text3 的\Data\Packages ...

  7. EhReport ,CReport改进版本,再次改进 ,V1.31

    取消了xlgrid依赖,带齐了第三方包. 安装更加方便. For D7 下载源码

  8. CGRectXXX笔记

    CoreGraphics中有关CGRect相关函数笔记 1.CGRectInset //该结构体的应用是以原rect为中心,再参考dx,dy,进行缩放或者放大. CGRect rect = CGRec ...

  9. su - root 报su: incorrect password的错误

    检查/bin/下面的文件的组属 和 sh 文件 的权限 是否有问题 例如:-rwxr-xr-x. 1 weblogic dba 34904 Jul 15  2011 /bin/su1   修改这个文件 ...

  10. Android -- The Manifest File

    Before the Android system can start an app component, the system must know that the component exists ...