关闭

也谈图片切割

292人阅读 评论(0) 收藏 举报

大体思路为:   

    当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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2784次
    • 积分:91
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档