Notes 생성 QR코드, 바코드 등

24029 단어 QR코드바코드notes
Google zxing 패키지를 사용했습니다.
 

  
  
  
  
  1. package com.mmm.picture; 
  2. import java.awt.image.BufferedImage; 
  3. import java.io.File; 
  4. import java.io.IOException; 
  5. import java.util.Vector; 
  6.  
  7. import javax.imageio.ImageIO; 
  8.  
  9. import lotus.domino.Agent; 
  10. import lotus.domino.AgentBase; 
  11. import lotus.domino.AgentContext; 
  12. import lotus.domino.Database; 
  13. import lotus.domino.Document; 
  14. import lotus.domino.DocumentCollection; 
  15. import lotus.domino.EmbeddedObject; 
  16. import lotus.domino.NotesException; 
  17. import lotus.domino.RichTextItem; 
  18. import lotus.domino.Session; 
  19.  
  20. import com.google.zxing.BarcodeFormat; 
  21. import com.google.zxing.BinaryBitmap; 
  22. import com.google.zxing.LuminanceSource; 
  23. import com.google.zxing.MultiFormatReader; 
  24. import com.google.zxing.MultiFormatWriter; 
  25. import com.google.zxing.ReaderException; 
  26. import com.google.zxing.Result; 
  27. import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 
  28. import com.google.zxing.common.ByteMatrix; 
  29. import com.google.zxing.common.HybridBinarizer; 
  30.  
  31. public class QRCode extends AgentBase { 
  32.     private static final int BLACK = 0xff000000
  33.     private static final int WHITE = 0xFFFFFFFF
  34.     Session session = null
  35.     Agent curAgent = null
  36.     Database curDB = null
  37.     DocumentCollection docCol = null
  38.     Document doc = null
  39.     private static StringBuffer log = new StringBuffer(); 
  40.  
  41.     public void NotesMain() { 
  42.         try { 
  43.             session = getSession(); 
  44.             AgentContext agentContext = session.getAgentContext(); 
  45.  
  46.             // (Your code goes here) 
  47.             curAgent = agentContext.getCurrentAgent(); 
  48.             curDB = agentContext.getCurrentDatabase(); 
  49.             log.append("Agent: " + curAgent.getName() + " in " 
  50.                     + curDB.getFilePath() + " started at " 
  51.                     + new java.util.Date().toString() + "
    "
    ); 
  52.             docCol = curDB 
  53.                     .search("Form = \"Certificate\" & StatusFlag = \"3\" & NeedMail = \"1\""); 
  54.             log.append("Find " + Integer.toString(docCol.getCount()) + " docs"); 
  55.             for (int i = 1; i <= docCol.getCount(); i++) { 
  56.                 doc = docCol.getNthDocument(i); 
  57.                 if (doc != null) { 
  58.                     String imgFolder = "C:\\QRCode\\"
  59.                     String imgPath = imgFolder + doc.getItemValueString("SN") + ".png"
  60.                     File imgFolderFile = new File(imgFolder); 
  61.                     if(!imgFolderFile.isDirectory()){ 
  62.                         imgFolderFile.mkdir(); 
  63.                     } 
  64.                     File imgFile = new File(imgPath); 
  65.                     encode(" " 
  66.                             + doc.getItemValueString("Ecode"), imgFile, "png"); 
  67.                     RichTextItem rtfField = doc.createRichTextItem("QRCode"); 
  68.                     try { 
  69.                         rtfField.embedObject(EmbeddedObject.EMBED_ATTACHMENT, 
  70.                                 "", imgPath, null); 
  71.                         doc.replaceItemValue("QRCoded""Y"); 
  72.                         doc.save(truefalse); 
  73.                         rtfField.recycle(); 
  74.                         imgFile.delete(); 
  75.                     } catch (NotesException e) { 
  76.                         // TODO Auto-generated catch block 
  77.                         log.append("embedObject1: " + e.toString()); 
  78.                     } catch (Exception e) { 
  79.                         log.append("embedObject2: " + e.toString()); 
  80.                     } 
  81.                     doc.recycle(); 
  82.                 } 
  83.             } 
  84.             docCol.recycle(); 
  85.             log.append("Agent: " + curAgent.getName() + " in " 
  86.                     + curDB.getFilePath() + " finished at " 
  87.                     + new java.util.Date().toString() + "
    "
    ); 
  88.             sendMail(log.toString()); 
  89.  
  90.             curDB.recycle(); 
  91.             curAgent.recycle(); 
  92.             session.recycle(); 
  93.  
  94.         } catch (Exception e) { 
  95.             e.printStackTrace(); 
  96.         } 
  97.     } 
  98.  
  99.     private void sendMail(String msg) { 
  100.         try { 
  101.             Vector sendTo = new Vector(); 
  102.             sendTo.add("XXX/XXX"); 
  103.             sendTo.add("XXX/XXX"); 
  104.  
  105.             Document mail = curDB.createDocument(); 
  106.             mail.replaceItemValue("SendTo", sendTo); 
  107.             mail.replaceItemValue( 
  108.                     "Subject"
  109.                     "Message from Agent " + curAgent.getName() + " in " 
  110.                             + curDB.getFilePath()); 
  111.             mail.replaceItemValue("Principal""EForm/XXX"); 
  112.             RichTextItem body = mail.createRichTextItem("Body"); 
  113.             body.appendText(msg); 
  114.             mail.send(false); 
  115.             mail.remove(true); 
  116.             body.recycle(); 
  117.             mail.recycle(); 
  118.             // System.out.println(msg); 
  119.         } catch (NotesException e) { 
  120.             // TODO Auto-generated catch block 
  121.             e.printStackTrace(); 
  122.         } 
  123.     } 
  124.  
  125.     /** 
  126.      * @param args 
  127.      */ 
  128.     public static void main(String[] args) { 
  129.         QRCode qrCode = new QRCode(); 
  130.         File imgFile = new File("C:\\QRCode.png"); 
  131.         //   
  132.         qrCode.encode(" "
  133.                 imgFile, "png"); 
  134.  
  135.         //   
  136.         qrCode.decode(imgFile); 
  137.         System.out.println(log.toString()); 
  138.     } 
  139.  
  140.     /** 
  141.      *    com.google.zxing.qrcode.encoder.Encoder.java  static final 
  142.      * String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1"; UTF-8,  
  143.      */ 
  144.     public void encode(String strCode, File imgFile, String fileType) { 
  145.         try { 
  146.             ByteMatrix byteMatrix; 
  147.             byteMatrix = new MultiFormatWriter().encode(strCode, 
  148.                     BarcodeFormat.QR_CODE, 200200); 
  149.             writeToFile(byteMatrix, fileType, imgFile); 
  150.         } catch (Exception e) { 
  151.             log.append("encode: " + e.toString()); 
  152.         } 
  153.     } 
  154.  
  155.     public static void writeToFile(ByteMatrix matrix, String format, File file) 
  156.             throws IOException { 
  157.         BufferedImage image = toBufferedImage(matrix); 
  158.         ImageIO.write(image, format, file); 
  159.     } 
  160.  
  161.     public static BufferedImage toBufferedImage(ByteMatrix matrix) { 
  162.         int width = matrix.getWidth(); 
  163.         int height = matrix.getHeight(); 
  164.         BufferedImage image = new BufferedImage(width, height, 
  165.                 BufferedImage.TYPE_INT_ARGB); 
  166.         for (int x = 0; x < width; x++) { 
  167.             for (int y = 0; y < height; y++) { 
  168.                 image.setRGB(x, y, matrix.get(x, y) == 0 ? BLACK : WHITE); 
  169.             } 
  170.         } 
  171.         return image; 
  172.     } 
  173.  
  174.     /** 
  175.      *     
  176.      */ 
  177.     public void decode(File imgFile) { 
  178.         try { 
  179.             BufferedImage image; 
  180.             try { 
  181.                 image = ImageIO.read(imgFile); 
  182.                 if (image == null) { 
  183.                     log.append("Could not decode image"); 
  184.                 } 
  185.                 LuminanceSource source = new BufferedImageLuminanceSource(image); 
  186.                 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer( 
  187.                         source)); 
  188.  
  189.                 Result result; 
  190.  
  191.                 //  :utf-8, 
  192.                 log.append(" :" 
  193.                         + new MultiFormatReader().decode(bitmap).getText()); 
  194.  
  195.             } catch (IOException ioe) { 
  196.                 log.append("decode1: " + ioe.toString()); 
  197.             } catch (ReaderException re) { 
  198.                 log.append("decode2: " + re.toString()); 
  199.             } 
  200.  
  201.         } catch (Exception ex) { 
  202.             log.append("decode3: " + ex.toString()); 
  203.         } 
  204.     } 

좋은 웹페이지 즐겨찾기