也谈图片切割

大体思路为:   

    当clientUser上传图像后,我们保存图像到数据库。这个时候我们需要的是一张缩略图,和本身大小的图。在这里我们用到spring中的MultipartHttpServletRequest及MultipartFile实现文件上传。上传的文件被异步保存到临时文件中,继而将临时文件copy到需保存的目录,并删除临时文件。我们所得到的是个从request中得到的文件流,缩略的操作还是放在目录里做。大概的实现过程是这样:

保存临时文件:

注:其中tempPath为在XML配置文件中配置过

public String saveTempFile(MultipartFile file) {
  String rd = Tools.getRandString(16);
  String suffixName = file.getOriginalFilename();

 int lastIndex = suffixName.lastIndexOf(".");
  if (lastIndex > 0) {
   suffixName = suffixName.substring(lastIndex);
   rd = rd + suffixName;
  }
  File temp = new File(tempPath + rd);
  try {
   file.transferTo(temp);
   return rd;
  } catch (Exception e) {
   logger.error("save upload file fail!", e);
  }

  return null;
 }

从临时文件copy到目录:

public void fileCopy(File oldFile,File newFile){
  FileInputStream inputStream = null;
  FileOutputStream outputStream = null;
  try{
   byte[] bt = new byte[1024];
   inputStream = new FileInputStream(oldFile);
   outputStream = new FileOutputStream(newFile);
   int count;
   while((count = inputStream.read(bt)) != -1){
    outputStream.write(bt,0,count);
   }
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   try{
    if(inputStream != null){
     inputStream.close();
    }
    if(outputStream != null){
     outputStream.close();
    }
   }catch(Exception e){
    e.printStackTrace();
   }
  }
 }

生成缩略图:  

public  void createPreviewImage(String srcFileName) {
  FileOutputStream outi=null;
  FileInputStream sFile = null;
  try {
   pathRoot = attachService.savePath();
   StringBuffer newUrl = new StringBuffer();
   newUrl.append(pathRoot);
   

   String fileName = newUrl + srcFileName;
   String outFileName = newUrl+"def_img" +srcFileName;

    sFile = new FileInputStream(fileName);
   Image src = javax.imageio.ImageIO.read(sFile);
   int width = 100; // final int IMAGE_SIZE = 120;
   int height = 100;
   BufferedImage image = new BufferedImage(width, height,
     BufferedImage.TYPE_INT_RGB);

   Graphics g = image.getGraphics();

   g.drawImage(src, 0, 0, width, height, null);

   outi = new FileOutputStream(outFileName);
   // 创建输出文件流
   JPEGImageEncoder encodera = JPEGCodec.createJPEGEncoder(outi);
   // 创建JPEG编码对象
   encodera.encode(image);
   // 对这个BufferedImage (image)进行JPEG编码

  } catch (Exception e) {
   logger.error("生成缩略图错误",e);
  }finally{
   try{
    if(sFile!=null){
     sFile.close();
    }
   }catch(Exception e){
    
   }
   try {
    if(outi!=null){
     outi.close();
    }
   } catch (Exception e) {
    // TODO: handle exception
   }
  }
 } 

如果我们只是单纯的从MultipartFile中得到缩略图 可用如下方法:

public void uploadPic(InputStream oldImg,int p_width,int p_height,String dir,MultipartFile file) {
        UploadPicTask task=new UploadPicTask( p_width, p_height, dir, oldImg,file);
        asyncService.doTask(task);
 }

异步task为:

public class UploadPicTask implements Runnable{
    private int p_width;
    private int p_height;
    private InputStream oldImg;
    private String dir;
    private MultipartFile file;
    public UploadPicTask(int p_width,int p_height,String dir,InputStream oldImg,MultipartFile file){
     this.oldImg=oldImg;
     this.p_height=p_height;
     this.p_width=p_width;
     this.dir=dir;
     this.file=file;
    }
 public void run() {
  try {
   BufferedImage bi=ImageIO.read(oldImg);
   BufferedImage newBi=new BufferedImage(p_width,p_height,1);
   newBi.createGraphics().drawImage(bi, 0, 0, p_width,p_height,null);
   FileOutputStream newImg=new FileOutputStream(dir);
   ImageIO.write(newBi, "jpg", newImg);
   file.transferTo(new File(dir));
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值