JAVA生产缩略图
来源:互联网 发布:linux如何停掉mysql 编辑:程序博客网 时间:2024/04/30 12:43
- <pre name="code" class="java">package com.hunuo.common;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import javax.imageio.ImageIO;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- public class ImageUtil {
- /**
- * 生成缩略图
- * @param srcImageFile 源图片文件的File实例 File file = new File("文件名");
- * @param dstImageFileName 待生成的缩略图片完整路径(生成的格式为:image/jpeg);
- * @throws Exception
- */
- public static void makeSmallImage(File srcImageFile,String dstImageFileName) throws Exception {
- FileOutputStream fileOutputStream = null;
- JPEGImageEncoder encoder = null;
- BufferedImage tagImage = null;
- Image srcImage = null;
- try{
- srcImage = ImageIO.read(srcImageFile);
- int srcWidth = srcImage.getWidth(null);//原图片宽度
- int srcHeight = srcImage.getHeight(null);//原图片高度
- int dstMaxSize = 120;//目标缩略图的最大宽度/高度,宽度与高度将按比例缩写
- int dstWidth = srcWidth;//缩略图宽度
- int dstHeight = srcHeight;//缩略图高度
- float scale = 0;
- //计算缩略图的宽和高
- if(srcWidth>dstMaxSize){
- dstWidth = dstMaxSize;
- scale = (float)srcWidth/(float)dstMaxSize;
- dstHeight = Math.round((float)srcHeight/scale);
- }
- srcHeight = dstHeight;
- if(srcHeight>dstMaxSize){
- dstHeight = dstMaxSize;
- scale = (float)srcHeight/(float)dstMaxSize;
- dstWidth = Math.round((float)dstWidth/scale);
- }
- //生成缩略图
- tagImage = new BufferedImage(dstWidth,dstHeight,BufferedImage.TYPE_INT_RGB);
- tagImage.getGraphics().drawImage(srcImage,0,0,dstWidth,dstHeight,null);
- fileOutputStream = new FileOutputStream(dstImageFileName);
- encoder = JPEGCodec.createJPEGEncoder(fileOutputStream);
- encoder.encode(tagImage);
- fileOutputStream.close();
- fileOutputStream = null;
- }finally{
- if(fileOutputStream!=null){
- try{
- fileOutputStream.close();
- }catch(Exception e){
- }
- fileOutputStream = null;
- }
- encoder = null;
- tagImage = null;
- srcImage = null;
- System.gc();
- }
- }
- }
- 这里可能遇到问题
- 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就可以编译通过。
- </pre><br>
- <br>
- <pre></pre>
- <pre></pre>
-
0 0
- JAVA生产缩略图
- JAVA生产缩略图
- PHP生产缩略图
- 好用的PHP生产缩略图类
- thinkphp3.23 文件上传并生产缩略图
- Java缩略图
- JAVA缩略图
- java 缩略图
- java缩略图
- 文件(图片生产缩略图)的上传与下载
- 文件(图片生产缩略图)的上传与下载
- JSP使用上传文件,并生产高清缩略图示例
- JAVA生成JPG缩略图
- JAVA生成JPG缩略图
- JAVA生成JPG缩略图
- JAVA生成JPG缩略图
- JAVA生成JPG缩略图
- JAVA生成JPG缩略图
- Linux-SED
- OLTP与OLAP的介绍
- 多线程抓取数据
- [LeetCode]100. Same Tree(相同树)
- Linux--RH134---unit 1 自动安装配置虚拟机
- JAVA生产缩略图
- POJ2385 Apple Catching(DP/递推)
- Leetcode-Trie
- Spring注入失败的原因
- linux运维-dhcp服务
- 全页式网站 练习代码暂存
- [Linux基础]Linux基础知识入门及常见命令.
- echarts动态加载数据
- Jmeter分析请求的响应结果