当前位置: 移动技术网 > IT编程>开发语言>Java > Java图片裁剪和生成缩略图的实例方法

Java图片裁剪和生成缩略图的实例方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、缩略图

在浏览相册的时候,可能需要生成相应的缩略图。

直接上代码:

public class imageutil {
 private logger log = loggerfactory.getlogger(getclass());
 private static string default_prevfix = "thumb_";
 private static boolean default_force = false;//建议该值为false
 /**
  * <p>title: thumbnailimage</p>
  * <p>description: 根据图片路径生成缩略图 </p>
  * @param imagepath 原图片路径
  * @param w   缩略图宽
  * @param h   缩略图高
  * @param prevfix 生成缩略图的前缀
  * @param force  是否强制按照宽高生成缩略图(如果为false,则生成最佳比例缩略图)
  */
 public void thumbnailimage(string imagepath, int w, int h, string prevfix, boolean force){
  file imgfile = new file(imagepath);
  if(imgfile.exists()){
   try {
    // imageio 支持的图片类型 : [bmp, bmp, jpg, jpg, wbmp, jpeg, png, png, jpeg, wbmp, gif, gif]
    string types = arrays.tostring(imageio.getreaderformatnames());
    string suffix = null;
    // 获取图片后缀
    if(imgfile.getname().indexof(".") > -1) {
     suffix = imgfile.getname().substring(imgfile.getname().lastindexof(".") + 1);
    }// 类型和图片后缀全部小写,然后判断后缀是否合法
    if(suffix == null || types.tolowercase().indexof(suffix.tolowercase()) < 0){
     log.error("sorry, the image suffix is illegal. the standard image suffix is {}." + types);
     return ;
    }
    log.debug("target image's size, width:{}, height:{}.",w,h);
    image img = imageio.read(imgfile);
    if(!force){
     // 根据原图与要求的缩略图比例,找到最合适的缩略图比例
     int width = img.getwidth(null);
     int height = img.getheight(null);
     if((width*1.0)/w < (height*1.0)/h){
      if(width > w){
       h = integer.parseint(new java.text.decimalformat("0").format(height * w/(width*1.0)));
       log.debug("change image's height, width:{}, height:{}.",w,h);
      }
     } else {
      if(height > h){
       w = integer.parseint(new java.text.decimalformat("0").format(width * h/(height*1.0)));
       log.debug("change image's width, width:{}, height:{}.",w,h);
      }
     }
    }
    bufferedimage bi = new bufferedimage(w, h, bufferedimage.type_int_rgb);
    graphics g = bi.getgraphics();
    g.drawimage(img, 0, 0, w, h, color.light_gray, null);
    g.dispose();
    string p = imgfile.getpath();
    // 将图片保存在原目录并加上前缀
    imageio.write(bi, suffix, new file(p.substring(0,p.lastindexof(file.separator)) + file.separator + prevfix +imgfile.getname()));
    log.debug("缩略图在原路径下生成成功");
   } catch (ioexception e) {
    log.error("generate thumbnail image failed.",e);
   }
  }else{
   log.warn("the image is not exist.");
  }
 }
 public static void main(string[] args) {
  new imageutil().thumbnailimage("c:/users/cm/desktop/我的页面/images/girlnoimg.jpg", 100, 150,default_prevfix,default_force);
 }
}

直接运行main方法,填入相对应的参数即可。

二、配合 js 生成裁剪图片

在我们修改个人微博、qq资料的时候可以上传个人头像,并可以剪裁个人头像然后上传。剪裁图片的大小样式是通过javascript实现的,但是它并不能生成一个新的图片。但是js剪裁图片提供图片的x,y坐标以及宽高,通过这四个参数我们可以根据原图片生成新的剪裁图片。

步骤:

1、首先通过页面利用js实现图片剪切浏览功能,我参照慕课网提供的资料并稍微更改了一下。另外我们也可以使用插件,比如jcrop是款很不错的图片裁剪插件。

下载地址:

这里写图片描述 

最后效果图如上,左侧可以拖动拖拉,右侧是预览图。

2、java生成并保存剪切图片

public class imageutil2 {
 private logger log = loggerfactory.getlogger(getclass());
 private static string default_cut_prevfix = "cut_";
 /**
  * description: 根据原图与裁切size截取局部图片
  * @param srcimg 源图片
  * @param output 图片输出流
  * @param rect 需要截取部分的坐标和大小
  */
 public void cutimage(file srcimg, outputstream output,java.awt.rectangle rect) {
  if (srcimg.exists()) {
   java.io.fileinputstream fis = null;
   imageinputstream iis = null;
   try {
    fis = new fileinputstream(srcimg);
    // imageio 支持的图片类型 : [bmp, bmp, jpg, jpg, wbmp, jpeg, png, png,
    // jpeg, wbmp, gif, gif]
    string types = arrays.tostring(imageio.getreaderformatnames())
      .replace("]", ",");
    string suffix = null;
    // 获取图片后缀
    if (srcimg.getname().indexof(".") > -1) {
     suffix = srcimg.getname().substring(srcimg.getname().lastindexof(".") + 1);
    }// 类型和图片后缀全部小写,然后判断后缀是否合法
    if (suffix == null
      || types.tolowercase().indexof(suffix.tolowercase() + ",") < 0) {
     log.error("sorry, the image suffix is illegal. the standard image suffix is {}."+ types);
     return;
    }
    // 将fileinputstream 转换为imageinputstream
    iis = imageio.createimageinputstream(fis);
    // 根据图片类型获取该种类型的imagereader
    imagereader reader = imageio.getimagereadersbysuffix(suffix).next();
    reader.setinput(iis, true);
    imagereadparam param = reader.getdefaultreadparam();
    param.setsourceregion(rect);
    bufferedimage bi = reader.read(0, param);
    imageio.write(bi, suffix, output);
    log.info("图片生成成功,请到目录下查看");
   } catch (filenotfoundexception e) {
    e.printstacktrace();
   } catch (ioexception e) {
    e.printstacktrace();
   } finally {
    try {
     if (fis != null)
      fis.close();
     if (iis != null)
      iis.close();
    } catch (ioexception e) {
     e.printstacktrace();
    }
   }
  } else {
   log.warn("the src image is not exist.");
  }
 }
 //生成目标文件路径
 public void cutimage(file srcimg, string destimgpath,java.awt.rectangle rect) {
  file destimg = new file(destimgpath);
  if (destimg.exists()) {
   string p = destimg.getpath();
   try {
    if (!destimg.isdirectory())
     p = destimg.getparent();
    if (!p.endswith(file.separator))
     p = p + file.separator;
    cutimage(srcimg,new java.io.fileoutputstream(p + default_cut_prevfix+ "_"+ srcimg.getname()), rect);
   } catch (filenotfoundexception e) {
    log.warn("the dest image is not exist.");
   }
  } else
   log.warn("the dest image folder is not exist.");
 }
 public void cutimage(string srcimg, string destimg, int x, int y, int width,
   int height) {
  cutimage(new file(srcimg), destimg, new java.awt.rectangle(x, y, width, height));
 }
 public static void main(string[] args) {
  new imageutil2().cutimage("c:/users/cm/desktop/我的页面/images/boynoimg.jpg", "c:/users/cm/desktop/我的页面/images/imgs",0, 0, 61, 166);
  //new imageutil2().cutimage("c:/users/cm/desktop/jcrop-master/demos/demo_files/sago.jpg", "c:/users/cm/desktop/我的页面/images/imgs",124, 110, 196, 176);
 }
}

该方法可以直接在main里运行。传入的四个参数分别为图像路径、left值、top值,长度、宽度。

下面分析一下怎么获取这四个参数:

1、以我的js剪切为例,通过f12获得如下:

这里写图片描述 

图片中红色部分就是图片剪切的div,我们可以通过拖拽剪裁区大小宽度等来观察改变了哪些参数,然后确定具体对应的参数值分别对应哪个参数值。如图片中我的x/y/width/height分别为40,28,224,228。

注意:在js中我将该div和图片的长宽都定义为300*300,为配合测试所以我下载的图片也为300*300。如果你测试的图片大小不为300*300,那么你直接在上面java中测试的效果会和你前端看到的不一样,因为你前端的图片宽高我定义为300*300,而你实际图片(即java中的图片)不是。

那么这个问题如果处理呢?

在你的java代码中获取原图片的长宽,然后判断原图片的长宽是不是300*300,如果不是就生成该图片的300*300的缩略图,然后将该300*300的缩略图作为裁剪的图片原型。(我的代码中没有处理,需要的自己处理,用完图片之后删除缩略图)

2、jcrop获取参数

这里写图片描述

如图,jcrop直接提供了参数,可以直接使用。但是它有个缺点就是在前端页面的图片大小区域不固定,如果你有个大像素图片,那么会非常丑,比如我在对应文件里有个图片soga_bak.jpg,换成这个图片就不好了。

所以综上建议用第一个js,然后判断原图片的长宽是不是300,不是的生成300*300缩略图,然后将缩略图作为裁剪原型图,用完了再删除缩略图。

以上所述是小编给大家介绍的java图片裁剪和生成缩略图的实例方法,对get请求无效的问题,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网