java生成生成图片缩略图
/**
*
*/
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生成生成图片缩略图的更多相关文章
- JAVA生成图片缩略图、JAVA截取图片局部内容
package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...
- java图片裁剪和java生成缩略图
一.缩略图 在浏览相冊的时候.可能须要生成相应的缩略图. 直接上代码: public class ImageUtil { private Logger log = LoggerFactory.getL ...
- PHP一般情况下生成的缩略图都比较不理想
PHP用GD库生成高质量的缩略图片,PHP一般情况下生成的缩略图都比较不理想.今天试用PHP,GD库来生成缩略图.虽然并不100%完美.可是也应该可以满足缩略图的要求了.<?php $FILEN ...
- PHP原生写的生成图片缩略图类
PHP原生写的生成图片缩略图类,本文以京东商品图片为例,分别生成三种不同尺寸的图片.调用方法很简单只要传参数高度和宽度,及新图片的名称. 引入缩略图类 include_once 'ImageResiz ...
- 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- ...
- java 生成二维码后叠加LOGO并转换成base64
1.代码 见文末推荐 2.测试 测试1:生成base64码 public static void main(String[] args) throws Exception { String dat ...
- springboot搭建项目,实现Java生成随机图片验证码。
这篇文章主要介绍了如何通过Java如何生成验证码并验证.验证码的作用我想必大家都知道,话不多说开始实施! 首先创建一个springboot项目以下是项目结构,内有utli工具类.存放生成图片验证码方法 ...
- c#.net 生成清晰缩略图
1 public void imgsize() 2 { 3 //本例中假定了两个变量: 4 5 String src = "c:/myImages/a.jpg"; //源图像文件的 ...
- Java生成和操作Excel文件(转载)
Java生成和操作Excel文件 JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该A ...
随机推荐
- MCS-51系列特殊功能寄存器(摘录)
MCS-51系列特殊功能寄存器(80H~FFH) 1. P0 (80H) P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0 2.SP 栈指针(81H) 3.DPTR 数据 ...
- php 通过变量 来调用函数
<?php function fun() { echo 'fun'; } $a = 'fun'; $a(); ?> 复制代码 上面的$a变量就是fun()函数,调用$a()和调用fun() ...
- Nginx-->进阶-->原理-->Nginx+php+fastcgi的原理与关系
一.用户对动态PHP网页访问过程 用户浏览器发起对网页的访问:http://192.168.1.103/index.php 用户和nginx服务器进行三次握手进行TCP连接(忽略包括nginx访问控制 ...
- fiddler的使用
查看fiddler监听的端口: tools=>fiddler options=>connections=>fiddler listens on port 8888 如果想抓curl发 ...
- Reapter控件的特殊使用:使用EVAL调取asp:Repeater里面绑定的值来进行判断 根据从数据库获取的数据进行判断 ,进而显示成想要的内容
1.这个判断的过程你可以写在后台,如先在后台写一个public类型的方法:public bool CheckAduit(string code){ //根据你传入的code来判断,并返回true或者f ...
- 1.3 Content Provider
ContentProvider向我们提供了我们在应用程序之间共享数据的一种机制,分为系统的和自定义的,系统的也就是例如联系人,图片等数据. 使用方式:一个应用实现ContentProvider来提供内 ...
- UISwitch(开关控件)、UISegmentedControl(分段控件)
一.UISwitch 1.初始化 UISwitch *s1 = [[UISwitch alloc]initWithFrame:CGRectMake(50, 170, 100, 200)]; 2.设 ...
- Zabbix3.0 自动电话报障
第一种:Pagerduty 网站:www.pagerduty.com 优点:老牌服务商,稳定 缺点:贵,英文,网站要FQ 价格参考(34美元每月才25个电话,*29每月是包年才有的价格) 安装方式: ...
- 使用jsPlumb制作流程图设计器
jsPlumb是一个比较强大的绘图组件,它提供了一种方法,主要用于连接网页上的元素.在现代浏览器中,它使用SVG或者Canvas技术,而对于IE8以下(含IE8)的古董浏览器,则使用VML技术. 项目 ...
- Yii中设置时间分区
在wamp环境下,运行一个Php yii的项目 出现问题: Use of undefined constant PRC - assumed 'PRC' 检测我的环境 PHP5.3 检测Php.ini中 ...