/**
*
*/
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. ftl文件格式化jsp形式显示

    通过myeclipse设置ftl的编辑器为jsp的编辑器,达到效果. Myeclipse->windows->preferences 1\ 2\ 3\ 完成后即可.

  2. poj 2584 T-Shirt Gumbo (二分匹配)

    T-Shirt Gumbo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2571   Accepted: 1202 Des ...

  3. iOS 如何给Xcode7项目添加“.pch”文件

    1.首先打开你的项目(演示使用一个空的项目),按照以下步骤即可 找到“Supporting Files”文件夹,右键即可看到下图,选择“New File...” 2.选择"iOS" ...

  4. Android Studio 初使用

    Android Studio 更改Eclipse快捷键 Android Studio 更改编码 Android Studio 导包

  5. nginx解析php请求为404

    刚搭建了lnmp环境,发现如果讲我的程序放在某个文件夹的时候,然后nginx进行代理的时候竟然会是404: nginx配置如下: # pass the PHP scripts to FastCGI s ...

  6. PostGreSQL 分页

    select * from users limit 10 offset 20; limit A offset B    其中A是页容量 B是偏移量 即跳过前20条 查询每页10条

  7. 深入浅出Symfony2 - 如何提高网站响应速度 [转]

    简介 Symfony2是一个基于PHP语言的Web开发框架,有着开发速度快.性能高等特点.但Symfony2的学习曲线也比较陡峭,没有经验的初学者往往需要一些练习才能掌握其特性.相对其他框架,Symf ...

  8. easyui combobox onSelect事件

    easyui combobox 没有onchange事件,只有onSelect事件 1 $(function () { $('#Select6').combobox({ onSelect: funct ...

  9. (Unity)Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进展混淆,避免被反编译

    Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进行混淆,避免被反编译. 1.打开VS,博主所用版本是Visual Studio 2013. 2.新建一个VC项目 ...

  10. 2004FBI树

    题目描述 Description 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树[1],它的结点类型 ...