android版java.awt.imageproducer的替代方案
我正在尝试使用stackoverflow上的指南实现一些ocr。使用该应用程序的示例代码使用java,我正在尝试在android上运行它。下面是示例中的代码android版java.awt.imageproducer的替代方案,java,android,awt,Java,Android,Awt,我正在尝试使用stackoverflow上的指南实现一些ocr。使用该应用程序的示例代码使用java,我正在尝试在android上运行它。下面是示例中的代码 // 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 un
// 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.");
}
}
//OCRScannerDemo.java
//版权所有(c)2003-2009罗纳德·B·塞默
//版权所有。
/*
此程序是免费软件:您可以重新发布和/或修改它
它是根据GNU通用公共许可证的条款发布的
自由软件基金会,版本2的许可证。
这个节目的发布是希望它会有用,
但无任何保证;甚至没有任何关于
适销性或适合某一特定目的。见
有关更多详细信息,请参阅GNU通用公共许可证。
您应该已经收到GNU通用公共许可证的副本
和这个节目一起。如果没有,请参阅。
*/
包com.roncemer.ocr.main;
导入java.awt.Frame;
导入java.awt.Image;
导入java.awt.MediaTracker;
导入java.awt.ScrollPane;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入java.awt.image.ImageProducer;
导入java.io.File;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.HashMap;
导入com.roncemer.ocr.CharacterRange;
导入com.roncemer.ocr.OCRImageCanvas;
导入com.roncemer.ocr.OCRScanner;
导入com.roncemer.ocr.PixelImage;
导入com.roncemer.ocr.TrainingImageLoader;
导入com.roncemer.ocr.tracker.MediaTrackerProxy;
/**
*演示OCR文档扫描和解码的演示应用程序。
*@作者罗纳德·B·塞默
*/
公共类OCRScannerDemo扩展框架{
私有静态最终长serialVersionUID=-8030499184564680363L;
私有布尔调试=true;
私有图像;
私有OCR图像画布;
私人OCR扫描仪;
公共OCRScannerDemo(){
超级(“来自扫描图像的OCR”);
设置大小(1024768);
ScrollPane ScrollPane=新的ScrollPane();
imageCanvas=新的OCRIGECanvas();
scrollPane.add(imageCanvas);
添加(滚动窗格);
addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
((Frame)(e.getSource()).hide();
系统出口(0);
}
});
scanner=新的OCRConner();
show();
}
/**
*加载演示训练图像。
*@param trainingImageDir从中加载图像的目录。
*/
公共void加载TrainingImages(字符串trainingImageDir){
if(debug)System.err.println(“loadTrainingImages(“+trainingImageDir+”));
如果(!trainingImageDir.endsWith(File.separator)){
trainingImageDir+=File.separator;
}
试一试{
扫描器。clearTrainingImages();
TrainingImageLoader=新的TrainingImageLoader();
HashMap images=新的HashMap();
if(debug)System.err.println(“ascii.png”);
装载机(
这
trainingImageDir+“ascii.png”,
新字符范围(“!”、“~”),
图像);
if(debug)System.err.println(“hpljPica.jpg”);
装载机(
这
trainingImageDir+“hpljPica.jpg”,
新字符范围(“!”、“~”),
图像);
if(debug)System.err.println(“digits.jpg”);
装载机(
这
trainingImageDir+“digits.jpg”,
新字符范围('0','9'),
图像);
if(debug)System.err.println(“添加图像”);
扫描仪。添加培训图像(图像);
if(debug)System.err.println(“loadTrainingImages()完成”);
}
捕获(IOEX异常){
例如printStackTrace();
系统出口(2);
}
}
公共作废进程(字符串imageFilename){
if(debug)System.err.println(“进程(“+imageFilename+”)”);
字符串imageFileUrlString=“”;
试一试{
imageFileUrlString=新文件(imageFilename).toURL().toString();
}捕获(格式错误){
e、 printStackTrace();
}
试一试{
ImageProducer ImageProducer=(ImageProducer)
(新URL(imageFileUrlString.getContent());
image=createImage(imageProducer);
}
捕获(IOE异常){
e、 printStackTrace();
}
if(image==null){
System.err.println(“在“+imageFileUrlString”处找不到图像文件);
回来
}
MediaTracker mt=新的MediaTrackerProxy(此);
mt.ADIMAGE(图像,0);
试一试{
韦特福尔山();
}catch(InterruptedException ex){}
if(debug)System.err.println(“图像加载”);
/*int w=image.getWidth(null);
int h=image.getHeight(null);
如果((w>0)和&(h>0)){
浮点比例因子=2048.0f/(浮点)数学最大值(w,h);
if(比例因子<1.0f){
image=image.getScaledInstance(
(int)((float)w*scaleFactor),
(int)((float)h*scaleFactor),Image.SCALE\u平滑);
mt=新的MediaTrackerProxy(本);
mt.ADIMAGE(图像,0);
请尝试{mt.waitForAll();}catch(InterruptedException ex){}
}
}*/
setSize(image.getWidth(null)、image.getHeight(null));
if(debug)System.err.println(“构建新像素图像”);
PixelImage PixelImage=新的PixelImage(图像);
if(debug)System.err.println(“将像素图像转换为灰度”);
pixelImage.toGrayScale(真);
if(调试)System.err.println(“过滤”);
pixelImage.filter();
if(debug)System.err.println(“设置显示图像”);
imageCanvas.setImage(
pixelImage.rgbToImage(
pixelImage.grayScaleToRGB(pixelImage.pix