package common;
/**
* 网络图片扫描程序
*
*/
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import javax.imageio.ImageIO;
import com.asprise.util.ocr.OCR;
import java.net.*;
public class ImageWord {
//获得图片数字
public static String word(String str) {
try {
File file = new File(str);
BufferedImage image = ImageIO.read(file);
String s = new OCR().recognizeCharacters(image);
return s;
} catch(Exception e) {
return "";
}
}
//删除临时目录的图片
public static void deleteImage() {
try {
String path = Thread.currentThread().getContextClassLoader().getResource("")+"temp/";
String filePath = path.substring(6,path.length());
File file = new File(filePath);
File[] files = file.listFiles();
for(int i=0;i<files.length;i++) {
try {
//系统文件不删除
if(!(files[i].getName()).equalsIgnoreCase("Thumbs.db")) {
if(files[i].isFile()) {
files[i].delete();
} else if(files[i].isDirectory()) {
files[i].delete();
} else {
files[i].delete();
}
}
} catch (RuntimeException e) {
Print.print("ImageWordError","DELETE FILE ERROR");
}
}
} catch (RuntimeException e) {
Print.print("ImageWordError","图片删除失败Error!"+e.toString());
}
}
//保存图片到临时目录
public static String saveImage(String image) {
try {
if(image.length()>1024) {
return null;
}
if(image.equals("")) {
return null;
}
String fileName = image.substring(image.lastIndexOf("/")+1,image.length());
String path = Thread.currentThread().getContextClassLoader().getResource("")+"temp/"+fileName;
String filePath = path.substring(6,path.length());
URL url = null;
try {
url = new URL(image);
} catch(Exception e) {
Print.print("ImageWordError","URL ERROR");
return null;
}
FilterInputStream in=(FilterInputStream) url.openStream();
File fileOut=new File(filePath);
FileOutputStream out=new FileOutputStream(fileOut);
byte[] bytes=new byte[1024];
int c;
while((c=in.read(bytes))!=-1) {
out.write(bytes,0,c);
}
in.close();
out.close();
return filePath;
} catch(Exception e) {
Print.print("ImageWordError","图片保存失败Error!"+e.toString());
return null;
}
}
}
posted on 2007-03-21 10:02
我心依旧 阅读(1388)
评论(4) 编辑 收藏