也谈图片切割

大体思路为:   

    当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
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值