Finished work on the local binarizer and renamed it to HybridBinarizer. It uses the...
[zxing.git] / zxingorg / src / com / google / zxing / web / DecodeEmailTask.java
index 08133d6..550ff3f 100644 (file)
 
 package com.google.zxing.web;
 
+import com.google.zxing.BinaryBitmap;
+import com.google.zxing.LuminanceSource;
 import com.google.zxing.MultiFormatReader;
 import com.google.zxing.Reader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
-import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
+import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
+import com.google.zxing.common.HybridBinarizer;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.Properties;
+import java.util.TimerTask;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.imageio.ImageIO;
 import javax.mail.Address;
@@ -29,6 +40,7 @@ import javax.mail.Flags;
 import javax.mail.Folder;
 import javax.mail.Message;
 import javax.mail.MessagingException;
+import javax.mail.Service;
 import javax.mail.Session;
 import javax.mail.Store;
 import javax.mail.Transport;
@@ -36,20 +48,13 @@ import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
-import java.awt.image.BufferedImage;
-import java.io.UnsupportedEncodingException;
-import java.io.IOException;
-import java.util.Properties;
-import java.util.TimerTask;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 /**
  * A {@link TimerTask} which repeatedly checks an e-mail account for messages with an attached
  * image. When one is found it attempts to decode the image and replies with the decoded messages
  * by e-mail.
  *
- * @author Sean Owen (srowen@google.com)
+ * @author Sean Owen
  */
 final class DecodeEmailTask extends TimerTask {
 
@@ -118,7 +123,8 @@ final class DecodeEmailTask extends TimerTask {
     }
   }
 
-  private void processMessage(Session session, Message message) throws MessagingException, IOException {
+  private void processMessage(Session session, Message message) throws MessagingException,
+      IOException {
     Object content = message.getContent();
     if (content instanceof MimeMultipart) {
       MimeMultipart mimeContent = (MimeMultipart) content;
@@ -140,7 +146,9 @@ final class DecodeEmailTask extends TimerTask {
         Reader reader = new MultiFormatReader();
         Result result = null;
         try {
-          result = reader.decode(new BufferedImageMonochromeBitmapSource(image), DecodeServlet.HINTS);
+          LuminanceSource source = new BufferedImageLuminanceSource(image);
+          BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
+          result = reader.decode(bitmap, DecodeServlet.HINTS);
         } catch (ReaderException re) {
           log.info("Decoding FAILED");
         }
@@ -163,13 +171,13 @@ final class DecodeEmailTask extends TimerTask {
     }
   }
 
-  private void closeResources(Store store, Folder inbox) {
+  private static void closeResources(Service service, Folder inbox) {
     try {
       if (inbox != null) {
         inbox.close(true);
       }
-      if (store != null) {
-        store.close();
+      if (service != null) {
+        service.close();
       }
     } catch (MessagingException me) {
       // continue
@@ -183,4 +191,4 @@ final class DecodeEmailTask extends TimerTask {
     new DecodeEmailTask(emailAddress, emailAuthenticator).run();
   }
 
-}
\ No newline at end of file
+}