iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Java怎么实现自动生成缩略图片
  • 217
分享到

Java怎么实现自动生成缩略图片

2023-06-30 08:06:43 217人浏览 八月长安
摘要

这篇文章主要介绍“Java怎么实现自动生成缩略图片”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java怎么实现自动生成缩略图片”文章能帮助大家解决问题。一、自动生成缩略图方法:package&nb

这篇文章主要介绍“Java怎么实现自动生成缩略图片”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java怎么实现自动生成缩略图片”文章能帮助大家解决问题。

一、自动生成缩略图方法:

package writeimg; import java.awt.geom.AffineTransfORM;import java.awt.image.AffineTransformOp;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO; public class JpegTool {         private boolean isInitFlag = false; //         对象是否己经初始化         private String pic_big_pathfilename = null; //定义源图片所在的带路径目录的文件名        private String pic_small_pathfilename = null; // 生成小图片的带存放路径目录的文件名         private int smallpicwidth = 0; //定义生成小图片的宽度和高度,给其一个就可以了         private int smallpicheight = 0;         private int pic_big_width=0;        private int pic_big_height=0;        private double picscale = 0; //定义小图片的相比原图片的比例                  public JpegTool(){                this.isInitFlag = false;         }                  private void resetJpegToolParams(){                 this.picscale = 0;                 this.smallpicwidth = 0;                 this.smallpicheight = 0;                 this.isInitFlag = false;         }                  public void SetScale(double scale) throws JpegToolException        {                 if(scale<=0){                                 throw new JpegToolException(" 缩放比例不能为 0 和负数! ");                 }                 this.resetJpegToolParams();                 this.picscale = scale;                 this.isInitFlag = true;         }                  public void SetSmallWidth(int smallpicwidth) throws JpegToolException         {                 if(smallpicwidth<=0)                {                         throw new JpegToolException(" 缩影图片的宽度不能为 0 和负数! ");                 }                 this.resetJpegToolParams();                 this.smallpicwidth = smallpicwidth;                 this.isInitFlag = true;         }                    public void SetSmallHeight(int smallpicheight) throws JpegToolException {                 if(smallpicheight<=0)                {                    throw new JpegToolException(" 缩影图片的高度不能为 0 和负数! ");                 }                 this.resetJpegToolParams();                 this.smallpicheight = smallpicheight;                 this.isInitFlag = true;         }                         public String getpic_big_pathfilename()        {                return this.pic_big_pathfilename;        }                public String getpic_small_pathfilename()        {                return this.pic_small_pathfilename;        }                public int getsrcw()        {                return this.pic_big_width;        }        public int getsrch()        {                return this.pic_big_height;        }                 public void doFinal(String pic_big_pathfilename,String pic_small_pathfilename) throws JpegToolException {                 if(!this.isInitFlag){                     throw new JpegToolException(" 对象参数没有初始化! ");                 }                 if(pic_big_pathfilename==null || pic_small_pathfilename==null){                     throw new JpegToolException(" 包含文件名的路径为空! ");                 }                 if((!pic_big_pathfilename.toLowerCase().endsWith("jpg")) && (!pic_big_pathfilename.toLowerCase().endsWith("jpeg"))){                     throw new JpegToolException(" 只能处理 JPG/JPEG 文件! ");                 }                 if((!pic_small_pathfilename.toLowerCase().endsWith("jpg")) && !pic_small_pathfilename.toLowerCase().endsWith("jpeg")){                     throw new JpegToolException(" 只能处理 JPG/JPEG 文件! ");                 }                 this.pic_big_pathfilename=pic_big_pathfilename;                this.pic_small_pathfilename=pic_small_pathfilename;                int smallw = 0;                 int smallh = 0;                 // 新建源图片和生成的小图片的文件对象                 File fi = new File(pic_big_pathfilename);                 File fo = new File(pic_small_pathfilename);                 //生成图像变换对象                 AffineTransform transform = new AffineTransform();                 //通过缓冲读入源图片文件                 BufferedImage bsrc = null;                 try {                 bsrc = ImageIO.read(fi);                 }catch (IOException ex) {                     throw new JpegToolException(" 读取源图像文件出错! ");                 }                 this.pic_big_width= bsrc.getWidth();// 原图像的长度                 this.pic_big_height = bsrc.getHeight();// 原图像的宽度                 double scale = (double)pic_big_width/pic_big_height;// 图像的长宽比例                 if(this.smallpicwidth!=0)                {// 根据设定的宽度求出长度                         smallw = this.smallpicwidth;// 新生成的缩略图像的长度                         smallh = (smallw*pic_big_height)/pic_big_width ;// 新生成的缩略图像的宽度                 }                else if(this.smallpicheight!=0)                {// 根据设定的长度求出宽度                         smallh = this.smallpicheight;// 新生成的缩略图像的长度                         smallw = (smallh*pic_big_width)/pic_big_height;// 新生成的缩略图像的宽度                 }                else if(this.picscale!=0)                {// 根据设置的缩小比例设置图像的长和宽                         smallw = (int)((float)pic_big_width*this.picscale);                         smallh = (int)((float)pic_big_height*this.picscale);                 }                else                {                     throw new JpegToolException(" 对象参数初始化不正确! ");                 }                double sx = (double)smallw/pic_big_width;//小/大图像的宽度比例                 double sy = (double)smallh/pic_big_height;//小/大图像的高度比例                 transform.setToScale(sx,sy);// 设置图像转换的比例                 //生成图像转换操作对象                 AffineTransformOp ato = new AffineTransformOp(transform,null);                 //生成缩小图像的缓冲对象                 BufferedImage bsmall = new BufferedImage(smallw,smallh,BufferedImage.TYPE_3BYTE_BGR);                 //生成小图像                 ato.filter(bsrc,bsmall);                 //输出小图像                 try{                        ImageIO.write(bsmall, "jpeg", fo);                 }                catch (IOException ex1)                 {                    throw new JpegToolException(" 写入缩略图像文件出错! ");                 }         }}

二、异常处理类:

package jpegtool;     public class JpegToolException extends Exception {            private String errMsg = "";             public JpegToolException(String errMsg)             {                     this.errMsg = errMsg;             }              public String getMsg(){                 return "JpegToolException:"+this.errMsg;             }     }

三、调用的例子:

package writeimg; public class T {      public static void main(String[] args) {         JpegTool j = new JpegTool();        try {            j.SetScale(0.7);            j.SetSmallHeight(100);            j.doFinal("D:\\305\\c\\javatest\\src\\11.jpg","D:\\305\\c\\javatest\\src\\22.jpg");        } catch (JpegToolException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    } }

关于“Java怎么实现自动生成缩略图片”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网精选频道,小编每天都会为大家更新不同的知识点。

--结束END--

本文标题: Java怎么实现自动生成缩略图片

本文链接: https://www.lsjlt.com/news/327837.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++ 生态系统中流行库和框架的贡献指南
    作为 c++++ 开发人员,通过遵循以下步骤即可为流行库和框架做出贡献:选择一个项目并熟悉其代码库。在 issue 跟踪器中寻找适合初学者的问题。创建一个新分支,实现修复并添加测试。提交...
    99+
    2024-05-15
    框架 c++ 流行库 git
  • C++ 生态系统中流行库和框架的社区支持情况
    c++++生态系统中流行库和框架的社区支持情况:boost:活跃的社区提供广泛的文档、教程和讨论区,确保持续的维护和更新。qt:庞大的社区提供丰富的文档、示例和论坛,积极参与开发和维护。...
    99+
    2024-05-15
    生态系统 社区支持 c++ overflow 标准库
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-15
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-15
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-15
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-15
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-15
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-15
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-15
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-15
    golang 数据库备份 mysql git 标准库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作