Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 395|回复: 0

[图像处理学习]JAVA中通用JPEG缩影图Bean的制作与示例

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-30 23:57:12 | 显示全部楼层 |阅读模式
    说明:
         前一段时间,看到网上一个网友贴出一个用 java 跨平台的生成 Jpeg 图片缩影图的方法。最近整理了一下,形成了一个通用的类。并制作成一个 jar 包。以方便重用。并给出了使用示例。(作者: abnerchai ;联系: josserchai@yahoo.com )

    一、源代码  1、JpegTool.java  // JpegTool.java  package com.abner.jpegtool;  import javax.imageio.ImageIO;  
      
    import javax.imageio.IIOException;  import java.awt.image.BufferedImage;  import java.awt.Image;  import java.io.File;  import java.awt.image.AffineTransformOp;  import java.awt.geom.AffineTransform;  import com.abner.jpegtool.JpegToolException;  import java.io.*;  /**  * 本类实现一个对 JPG/JPEG 图像文件进行缩影处理的方法  * 即给定一个 JPG 文件,可以生成一个该 JPG 文件的缩影图像文件 (JPG 格式 )  * 提供三种生成缩影图像的方法:  * 1 、设置缩影文件的宽度,根据设置的宽度和源图像文件的大小来确定新缩影文件的长度来生成缩影图像  * 2 、设置缩影文件的长度,根据设置的长度和源图像文件的大小来确定新缩影文件的宽度来生成缩影图像  * 3 、设置缩影文件相对于源图像文件的比例大小,根据源图像文件的大小及设置的比例来确定新缩影文件的大小来生成缩影图像  * 新生成的缩影图像可以比原图像大,这时即是放大源图像。  * @author abnerchai contact:josserchai@yahoo.com  * @version 1.0  * @exception JpegToolException  */  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 double picscale = 0;  /**  * 构造函数  * @param 没有参数  */  public JpegTool(){  this.isInitFlag = false;  }  /**  * 私有函数,重置所有的参数  * @param 没有参数  * @return 没有返回参数  */  private void resetJpegToolParams(){  this.picscale = 0;  this.smallpicwidth = 0;  this.smallpicheight = 0;  this.isInitFlag = false;  }  /**  * @param scale 设置缩影图像相对于源图像的大小比例如 0.5  * @throws JpegToolException  */  public void SetScale(double scale) throws JpegToolException {  if(scale<=0){  throw new JpegToolException(" 缩放比例不能为 0 和负数! ");  }  this.resetJpegToolParams();  this.picscale = scale;  this.isInitFlag = true;  }  /**  * @param smallpicwidth 设置缩影图像的宽度  * @throws JpegToolException  */  public void SetSmallWidth(int smallpicwidth) throws JpegToolException {  if(smallpicwidth<=0){  throw new JpegToolException(" 缩影图片的宽度不能为 0 和负数! ");  }  this.resetJpegToolParams();  this.smallpicwidth = smallpicwidth;  this.isInitFlag = true;  }  /**  * @param smallpicheight 设置缩影图像的高度  * @throws JpegToolException  */  public void SetSmallHeight(int smallpicheight) throws JpegToolException {  if(smallpicheight<=0){     throw new JpegToolException(" 缩影图片的高度不能为 0 和负数! ");  }  this.resetJpegToolParams();  this.smallpicheight = smallpicheight;  this.isInitFlag = true;  }  /**  * 生成源图像的缩影图像  * @param pic_big_pathfilename 源图像文件名,包含路径(如 windows 下 C:\pic.jpg ; Linux 下 /home/abner/pic/pic.jpg )  * @param pic_small_pathfilename 生成的缩影图像文件名,包含路径(如 windows 下 C:\pic_small.jpg ; Linux 下 /home/abner/pic/pic_small.jpg )  * @throws JpegToolException  */  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 文件! ");  }  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(" 读取源图像文件出错! ");  }  int srcw = bsrc.getWidth();// 原图像的长度  int srch = bsrc.getHeight();// 原图像的宽度  double scale = (double)srcw/srch;// 图像的长宽比例  if(this.smallpicwidth!=0){// 根据设定的宽度求出长度  smallw = this.smallpicwidth;// 新生成的缩略图像的长度  smallh = (smallw*srch)/srcw ;// 新生成的缩略图像的宽度  }else if(this.smallpicheight!=0){// 根据设定的长度求出宽度  smallh = this.smallpicheight;// 新生成的缩略图像的长度  smallw = (smallh*srcw)/srch;// 新生成的缩略图像的宽度  }else if(this.picscale!=0){// 根据设置的缩小比例设置图像的长和宽  smallw = (int)((float)srcw*this.picscale);  smallh = (int)((float)srch*this.picscale);  }else{      throw new JpegToolException(" 对象参数初始化不正确! ");  }  System.out.println(" 源文件大小: "+srcw+"X"+srch);  System.out.println(" 新文件大小: "+smallw+"X"+smallh);  double sx = (double)smallw/srcw;// 小 / 大图像的宽度比例  double sy = (double)smallh/srch;// 小 / 大图像的高度比例  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(" 写入缩略图像文件出错! ");  }  }  }  2、JpegToolException.java  // JpegToolException.java  package com.abner.jpegtool;  /**  * <p>Description: JpegTool 使用的异常类 </p>  * @author abnerchai  * @version 1.0  */  public class JpegToolException extends Exception {  private String errMsg = "";  public JpegToolException(String errMsg) {    this.errMsg = errMsg;  }  public String getMsg(){      return "JpegToolException:"+this.errMsg;  }  }  二、 jar包制作  编译上面两个文件,形成两个 class 文件,均存放在目录:当前目录 comabnerjpegtool 目录下面。进入当前目录,执行: jar cvf jpegtool.jar com/* ;便在当前目录生成一个 jpegtool.jar 文件。发布这个 jar 包,以后便可以像使用其它 jar 包一样使用这个工具了,下面我们给出一个示例。  三、使用示例  为了方便,我们给出在 JSP 文件中使用该包的示例 JSP 代码,首先,设置你的类库路径,将 jpegtool.jar 文件放入你的类库路径中。然后设置你的应用服务器的类库路径,将 jpegtool.jar 放入你的应用服务器的类库路径中。然后测试下面的 JSP 代码:  <!―test.jsp -- >  <%@page contentType="text/HTML; charset=GB2312" %>  <%@page import = "com.abner.jpegtool.JpegTool"%>  <html><head><title>test</title></head>  <body bgcolor="#ffffff">  <%  try{  JpegTool jpegTool = new JpegTool();  jpegTool.SetSmallWidth(100);  jpegTool.doFinal("C:\big.jpeg","C:\small.jpeg");  }catch(Exception e){  e.printStackTrace();  }  %>  </body>  </html>  对于在 Servlet 和 Java 程序中使用,类似于上面的 JSP 。  注意:以上程序必须在支持 JDK1.4 的平台上运行。否则有些类库找不到。  

      
      
       
       

         
       

         
       
      



    源码下载:http://file.javaxxz.com/2014/10/30/235712500.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 21:55 , Processed in 0.374200 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表