/**
*
*/
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. Ubuntu下配置和使用github

    一.配置github环境 1.环境:Ubuntu14.04 2.申请github账号 3.安装配置git服务器: (1)安装ssh:sudo apt-get install openssh-serve ...

  2. 自动布局之autoresizingMask

    UIViewAutoresizing是一个枚举类型,默认是UIViewAutoresizingNone,也就是不做任何处理. 1 2 3 4 5 6 7 8 9 typedef NS_OPTIONS( ...

  3. shell脚本批量处理字符串

    上周五运营那边给了一份手机号码的excle,要求查询出所有对应于用户编号的用户的信息.这个时候遇到了一个问题就是,需要查询的用户数量很多,不可能一个一个去查,而excle中的格式又不符合sqlquer ...

  4. apache的虚拟目录的配置

    第一步:在httpd.conf底部添加以下代码.表示添加虚拟目录 1 <IfModule dir_module> #direcotory相当于是欢迎页面 DirectoryIndex in ...

  5. Intellij idea 设置svn 父目录文件显示状态颜色

    file-->setting-->version control

  6. EasyUI 后台接受DataGrid传来的参数

    string ad = Context.Request.QueryString["rows"];不行 string aedf = Context.Request.Form[&quo ...

  7. 【半平面交】bzoj1038 [ZJOI2008]瞭望塔

    http://m.blog.csdn.net/blog/qpswwww/44105605 #include<cstdio> #include<cmath> #include&l ...

  8. [f]添加css3动画的方法

    添加css3的一些动画的属性 使用方法: css3(oDiv[0], 'scale', 300)('rotate', 300);css3(oDiv[0], 'animation', '"dd ...

  9. iOS开发零基础--Swift篇:Swift中数据类型

    Swift类型的介绍 Swift中的数据类型也有:整型/浮点型/对象类型/结构体类型等等 先了解整型和浮点型 整型 有符号 Int8 : 有符号8位整型 Int16 : 有符号16位整型 Int32 ...

  10. 能看到U盘占用内存,但看不到文件

    原因:是u盘感染了病毒 .病毒把U盘里的东西加上了隐藏属性和系统属性. 解决办法:1.在“运行”里面输入:cmd,回车:2.在cmd中进入U盘.比如你的U盘是H盘,就输入:h:,回车:3.进入U盘之后 ...