[sikuli] 181/385: support function for Tesseract4Java (from Tess4J also)

Gilles Filippini pini at moszumanska.debian.org
Sun Jun 29 19:26:08 UTC 2014


This is an automated email from the git hooks/post-receive script.

pini pushed a commit to tag upstream/1.1.0_beta1
in repository sikuli.

commit bc1f9c0f5e4aa662a8e664b21411f866bd0ab096
Author: Raimund Hocke <info at its-me-raiman.de>
Date:   Wed Jan 29 20:31:18 2014 +0100

    support function for Tesseract4Java (from Tess4J also)
---
 API/src/main/java/org/sikuli/script/Image.java | 42 +++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/API/src/main/java/org/sikuli/script/Image.java b/API/src/main/java/org/sikuli/script/Image.java
index ccd6826..44baa4e 100644
--- a/API/src/main/java/org/sikuli/script/Image.java
+++ b/API/src/main/java/org/sikuli/script/Image.java
@@ -21,6 +21,8 @@ import java.awt.image.SampleModel;
 import java.awt.image.WritableRaster;
 import java.io.File;
 import java.net.URL;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -639,7 +641,7 @@ public class Image {
   }
 
   /**
-   * 
+   *
    * @param factor
    * @return a new BufferedImage resized (width*factor, height*factor)
    */
@@ -914,4 +916,42 @@ public class Image {
     BufferedImage bm = new BufferedImage(cm, r, false, null);
     return bm;
   }
+
+	// **************** for Tesseract4Java ********************
+    /**
+     * Converts <code>BufferedImage</code> to <code>ByteBuffer</code>.
+     *
+     * @param bi Input image
+     * @return pixel data
+     */
+    public static ByteBuffer convertImageData(BufferedImage bi) {
+        DataBuffer buff = bi.getRaster().getDataBuffer();
+        // ClassCastException thrown if buff not instanceof DataBufferByte because raster data is not necessarily bytes.
+        // Convert the original buffered image to grayscale.
+        if (!(buff instanceof DataBufferByte)) {
+            bi = convertImageToGrayscale(bi);
+            buff = bi.getRaster().getDataBuffer();
+        }
+        byte[] pixelData = ((DataBufferByte) buff).getData();
+        //        return ByteBuffer.wrap(pixelData);
+        ByteBuffer buf = ByteBuffer.allocateDirect(pixelData.length);
+        buf.order(ByteOrder.nativeOrder());
+        buf.put(pixelData);
+        buf.flip();
+        return buf;
+    }
+
+		/**
+     * A simple method to convert an image to gray scale.
+     *
+     * @param image input image
+     * @return a monochrome image
+     */
+    public static BufferedImage convertImageToGrayscale(BufferedImage image) {
+        BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
+        Graphics2D g2 = tmp.createGraphics();
+        g2.drawImage(image, 0, 0, null);
+        g2.dispose();
+        return tmp;
+    }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/sikuli.git



More information about the pkg-java-commits mailing list