Notes压缩图片附件实例

   同上篇博文一样,这只是一个后台代理实例,在Web页面选择上传的图片和压缩比例后Submit,然后代理在后台进行压缩,可以非常有效的减小文档大小。

 

  
  
  
  
  1. import java.awt.image.BufferedImage; 
  2. import java.awt.image.ColorModel; 
  3. import java.io.File; 
  4. import java.io.FileOutputStream; 
  5. import java.io.IOException; 
  6. import java.io.InputStream; 
  7. import java.io.PrintWriter; 
  8. import java.util.Iterator; 
  9. import java.util.Vector; 
  10. import java.lang.Float; 
  11.  
  12. import javax.imageio.IIOImage; 
  13. import javax.imageio.ImageIO; 
  14. import javax.imageio.ImageWriteParam; 
  15. import javax.imageio.ImageWriter; 
  16.  
  17. import lotus.domino.AgentBase; 
  18. import lotus.domino.AgentContext; 
  19. import lotus.domino.Session; 
  20. import lotus.domino.Document; 
  21. import lotus.domino.RichTextItem; 
  22. import lotus.domino.EmbeddedObject; 
  23. import lotus.domino.NotesException; 
  24.  
  25. public class JavaAgent extends AgentBase { 
  26.  
  27.     public void NotesMain() { 
  28.         Vector v = null
  29.         PrintWriter pt = null
  30.         try { 
  31.             Session session = getSession(); 
  32.             AgentContext agentContext = session.getAgentContext(); 
  33.             pt = getAgentOutput(); 
  34.             pt.println("content_type:text/html"); 
  35.             pt.println("<html>"); 
  36.             pt.println("<head></head>"); 
  37.             pt.println("<body>"); 
  38.  
  39.             // RichTextItem picField = null; 
  40.             Document doc = agentContext.getDocumentContext(); 
  41.             String imageName = doc.getItemValueString("image"); 
  42.             if ("Y".equals(doc.getItemValueString("image_new")) 
  43.                     && !"".equals(imageName)) { 
  44.                 // 删除其他图片 
  45.                 Vector vl = session.evaluate("@AttachmentNames", doc); 
  46.                 for (int i = 0; i < vl.size(); i++) { 
  47.                     String atch = vl.get(i).toString(); 
  48.                     if (!atch.equals(imageName)) { 
  49.                         EmbeddedObject eo = doc.getAttachment(atch); 
  50.                         if (eo != null) { 
  51.                             // System.out.println(atch + " removed"); 
  52.                             eo.remove(); 
  53.                             eo.recycle(); 
  54.                         } 
  55.                     } 
  56.                 } 
  57.  
  58.                 File picFile = null
  59.  
  60.                 RichTextItem pirField = (RichTextItem) doc 
  61.                         .getFirstItem("picField"); 
  62.                 if (pirField == null) { 
  63.                     pirField = doc.createRichTextItem("picField"); 
  64.                 } 
  65.  
  66.                 EmbeddedObject eo = doc.getAttachment(imageName); 
  67.                 InputStream in = eo.getInputStream(); 
  68.                 float orgPress = Float.parseFloat(doc 
  69.                         .getItemValueString("OrgPress")); 
  70.                 String pathPre = "C:\\Temp\\"
  71.                  
  72.                 File tempFile = new File(pathPre); 
  73.                 if (!tempFile.isDirectory()){ 
  74.                     tempFile.mkdir(); 
  75.                 } 
  76.                  
  77.                 String filePath = pathPre + imageName; 
  78.                 compressPic(in, 600, orgPress, filePath); 
  79.                 in.close(); 
  80.  
  81.                 String imagePreName = doc.getItemValueString("image_pre"); 
  82.                 if (!"".equals(imagePreName)) { 
  83.                     in = eo.getInputStream(); 
  84.                     String filePathPre = pathPre + imagePreName; 
  85.                     float preOrgPree = (float) (orgPress + 0.3); 
  86.                     if (preOrgPree > 0.9) { 
  87.                         compressPic(in, 200, orgPress, filePathPre); 
  88.                     } else { 
  89.                         compressPic(in, 200, preOrgPree, filePathPre); 
  90.                     } 
  91.                     in.close(); 
  92.  
  93.                     pirField.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null
  94.                             filePathPre, null); 
  95.                     picFile = new File(filePathPre); 
  96.                     picFile.delete(); 
  97.                 } 
  98.  
  99.                 eo.remove(); 
  100.                 eo.recycle(); 
  101.  
  102.                 pirField.embedObject(EmbeddedObject.EMBED_ATTACHMENT, null
  103.                         filePath, null); 
  104.                  
  105.                 picFile = new File(filePath); 
  106.                 picFile.delete(); 
  107.                  
  108.                 doc.replaceItemValue("image_new""N"); 
  109.                 doc.save(truefalse); 
  110.                 pirField.recycle(); 
  111.                 doc.recycle(); 
  112.             } 
  113.             pt.println("<script language=\"JavaScript\">"); 
  114.             pt.println("window.parent.document.forms[0].submit();"); 
  115.             pt.println("</script>"); 
  116.             pt.println("</body>"); 
  117.             pt.println("</html>"); 
  118.              
  119.             agentContext.recycle(); 
  120.             session.recycle(); 
  121.              
  122.         } catch (IOException e) { 
  123.             // TODO Auto-generated catch block 
  124.             e.printStackTrace(); 
  125.             pt.println(e.getMessage()); 
  126.             pt.println("</body>"); 
  127.             pt.println("</html>"); 
  128.         } catch (NotesException e) { 
  129.             e.printStackTrace(); 
  130.             pt.println(e.getMessage()); 
  131.             pt.println("</body>"); 
  132.             pt.println("</html>"); 
  133.         } 
  134.     } 
  135.  
  136.     public static boolean compressPic(InputStream in, int width, float percent, 
  137.             String descFilePath) { 
  138.         File file = null
  139.         BufferedImage src = null
  140.         FileOutputStream out = null
  141.         ImageWriter imgWrier; 
  142.         ImageWriteParam imgWriteParams; 
  143.  
  144.         // 指定写图片的方式如jpg 
  145.         //Iterator iter = ImageIO.getImageWritersByFormatName(descFilePath.substring(descFilePath.lastIndexOf(".") + 1, descFilePath.length())); 
  146.         Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); 
  147.         imgWrier = (ImageWriter) iter.next(); 
  148.         imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam( 
  149.                 null); 
  150.         // 要使用压缩,必须指定压缩方式为MODE_EXPLICIT 
  151.         imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT); 
  152.         // 这里指定压缩的程度,参数qality是取值0~1范围内, 
  153.         imgWriteParams.setCompressionQuality(percent); 
  154.         imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED); 
  155.         ColorModel colorModel = ColorModel.getRGBdefault(); 
  156.         // 指定压缩时使用的色彩模式 
  157.         imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier( 
  158.                 colorModel, colorModel.createCompatibleSampleModel(1616))); 
  159.  
  160.         try { 
  161.             if (width == 0) { 
  162.                 src = ImageIO.read(in); 
  163.             } else { 
  164.                 BufferedImage bf = ImageIO.read(in); 
  165.                 if (width < bf.getWidth()) { 
  166.                     int height = width * bf.getHeight() / bf.getWidth(); 
  167.                     src = new BufferedImage(width, height, 
  168.                             BufferedImage.TYPE_INT_RGB); 
  169.                     src.getGraphics().drawImage(bf, 00, width, height, null); 
  170.                 } else { 
  171.                     src = ImageIO.read(in); 
  172.                 } 
  173.             } 
  174.  
  175.             out = new FileOutputStream(descFilePath); 
  176.  
  177.             imgWrier.reset(); 
  178.             // 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何 
  179.             // OutputStream构造 
  180.             imgWrier.setOutput(ImageIO.createImageOutputStream(out)); 
  181.             // 调用write方法,就可以向输入流写图片 
  182.             imgWrier.write(nullnew IIOImage(src, nullnull), imgWriteParams); 
  183.             out.flush(); 
  184.             out.close(); 
  185.         } catch (Exception e) { 
  186.             e.printStackTrace(); 
  187.             return false
  188.         } 
  189.         return true
  190.     } 

 

你可能感兴趣的:(Lotus,图片压缩,notes)