2012-06-09 8 views
2

나는 stackoverflow에서 가이드를 사용하여 일부 ocr을 구현하려고합니다. 응용 프로그램의 사용을위한 예제 코드는 자바를 사용하고 나는 그것을 안드로이드에서 실행하려고합니다. 예제 코드는 다음과 같습니다.안드로이드에 대한 java.awt.imageproducer에 대한 대안

// OCRScannerDemo.java 
// Copyright (c) 2003-2009 Ronald B. Cemer 
// All rights reserved. 
/* 
This program is free software: you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation, version 2 of the License. 

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
GNU General Public License for more details. 

You should have received a copy of the GNU General Public License 
along with this program. If not, see <http://www.gnu.org/licenses/>. 
*/ 

package com.roncemer.ocr.main; 

import java.awt.Frame; 
import java.awt.Image; 
import java.awt.MediaTracker; 
import java.awt.ScrollPane; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.image.ImageProducer; 
import java.io.File; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.HashMap; 

import com.roncemer.ocr.CharacterRange; 
import com.roncemer.ocr.OCRImageCanvas; 
import com.roncemer.ocr.OCRScanner; 
import com.roncemer.ocr.PixelImage; 
import com.roncemer.ocr.TrainingImageLoader; 
import com.roncemer.ocr.tracker.MediaTrackerProxy; 

/** 
    * Demo application to demonstrate OCR document scanning and decoding. 
    * @author Ronald B. Cemer 
    */ 
public class OCRScannerDemo extends Frame { 

    private static final long serialVersionUID = -8030499184564680363L; 

    private boolean debug = true; 

    private Image image; 
    private OCRImageCanvas imageCanvas; 
    private OCRScanner scanner; 

    public OCRScannerDemo() { 
     super("OCR from a scanned image"); 
     setSize(1024, 768); 
     ScrollPane scrollPane = new ScrollPane(); 
     imageCanvas = new OCRImageCanvas(); 
     scrollPane.add(imageCanvas); 
     add(scrollPane); 
     addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       ((Frame)(e.getSource())).hide(); 
       System.exit(0); 
      } 
     }); 
     scanner = new OCRScanner(); 
     show(); 
    } 

    /** 
     * Load demo training images. 
     * @param trainingImageDir The directory from which to load the images. 
     */ 
    public void loadTrainingImages(String trainingImageDir) { 
     if (debug) System.err.println("loadTrainingImages(" + trainingImageDir + ")"); 
     if (!trainingImageDir.endsWith(File.separator)) { 
      trainingImageDir += File.separator; 
     } 
     try { 
      scanner.clearTrainingImages(); 
      TrainingImageLoader loader = new TrainingImageLoader(); 
      HashMap images = new HashMap(); 
      if (debug) System.err.println("ascii.png"); 
      loader.load(
       this, 
       trainingImageDir + "ascii.png", 
       new CharacterRange('!', '~'), 
       images); 
      if (debug) System.err.println("hpljPica.jpg"); 
      loader.load(
       this, 
       trainingImageDir + "hpljPica.jpg", 
       new CharacterRange('!', '~'), 
       images); 
      if (debug) System.err.println("digits.jpg"); 
      loader.load(
       this, 
       trainingImageDir + "digits.jpg", 
       new CharacterRange('0', '9'), 
       images); 
      if (debug) System.err.println("adding images"); 
      scanner.addTrainingImages(images); 
      if (debug) System.err.println("loadTrainingImages() done"); 
     } 
     catch(IOException ex) { 
      ex.printStackTrace(); 
      System.exit(2); 
     } 
    } 

    public void process(String imageFilename) { 
     if (debug) System.err.println("process(" + imageFilename + ")"); 
     String imageFileUrlString = ""; 
     try { 
      imageFileUrlString = new File(imageFilename).toURL().toString(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     try { 
      ImageProducer imageProducer = (ImageProducer) 
       (new URL(imageFileUrlString).getContent()); 
      image = createImage(imageProducer); 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } 
     if (image == null) { 
      System.err.println("Cannot find image file at " + imageFileUrlString); 
      return; 
     } 
     MediaTracker mt = new MediaTrackerProxy(this); 
     mt.addImage(image, 0); 
     try { 
      mt.waitForAll(); 
     } catch(InterruptedException ex) {} 
     if (debug) System.err.println("image loaded"); 

/* int w = image.getWidth(null); 
    int h = image.getHeight(null); 
    if ((w > 0) && (h > 0)) { 
     float scaleFactor = 2048.0f/(float)Math.max(w, h); 
     if (scaleFactor < 1.0f) { 
     image = image.getScaledInstance(
      (int)((float)w*scaleFactor), 
      (int)((float)h*scaleFactor), Image.SCALE_SMOOTH); 
     mt = new MediaTrackerProxy(this); 
     mt.addImage(image, 0); 
     try { mt.waitForAll(); } catch(InterruptedException ex) {} 
     } 
    }*/ 

     imageCanvas.setSize(image.getWidth(null), image.getHeight(null)); 

     if (debug) System.err.println("constructing new PixelImage"); 
     PixelImage pixelImage = new PixelImage(image); 
     if (debug) System.err.println("converting PixelImage to grayScale"); 
     pixelImage.toGrayScale(true); 
     if (debug) System.err.println("filtering"); 
     pixelImage.filter(); 
     if (debug) System.err.println("setting image for display"); 
     imageCanvas.setImage(
      pixelImage.rgbToImage(
       pixelImage.grayScaleToRGB(pixelImage.pixels), 
       pixelImage.width, 
       pixelImage.height, 
       imageCanvas)); 
     System.out.println(imageFilename + ":"); 
     String text = scanner.scan(image, 0, 0, 0, 0, null, imageCanvas.getGraphics()); 
     System.out.println("[" + text + "]"); 
    } 

    public static void main(String[]args) { 
     if (args.length < 1) { 
      System.err.println("Please specify one or more image filenames."); 
      System.exit(1); 
     } 
     String trainingImageDir = System.getProperty("TRAINING_IMAGE_DIR"); 
     if (trainingImageDir == null) { 
      System.err.println 
       ("Please specify -DTRAINING_IMAGE_DIR=<dir> on " + 
       "the java command line."); 
      return; 
     } 
     OCRScannerDemo demo = new OCRScannerDemo(); 
     demo.loadTrainingImages(trainingImageDir); 
     for (int i = 0; i < args.length; i++) 
      demo.process(args[i]); 
     System.out.println("done."); 
    } 
} 

처리 방법에서 ImageProducer가 사용됩니다. 거기에 안드로이드에 대한 대안이나 내가이 방법을 사용하여 포기 수도 있습니다?

친절에 대해서

답변

0

약간 늦었지만 독자가 도움이 될 것 같습니다.

Android에서는 android.graphics.Bitmap 클래스를 이미지 빌드의 진입 점으로 간주합니다. 자바 AWT API와 비교할 때 이미지를 만들고 사용하는 것이 간단합니다. 안드로이드의 Bitmap 클래스는 java.awt.BufferedImage와 비슷하지만 set/getPixel 메도가있는 클래스와 set/getRGB가있는 클래스가 있습니다. 또한 다른 소스에서 비트 맵 (이미지)을 만드는 몇 가지 메소드와 비슷합니다.