Created
December 11, 2017 12:13
-
-
Save hiroto3432/a94291627d6513da4b4b9168b306bc08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.ArrayList; | |
import net.sourceforge.tess4j.ITesseract; | |
import net.sourceforge.tess4j.Tesseract; | |
import net.sourceforge.tess4j.TesseractException; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterFactory; | |
public class OCRtest { | |
public static void main(String[] args){ | |
String folderPath = "path"; | |
ArrayList<String> fileList; | |
fileList = readFiles(folderPath); | |
int n=0; | |
while(fileList.size() > n) { | |
String path = folderPath + fileList.get(n); | |
readAndTweet(path); | |
deleteFile(path); | |
n++; | |
} | |
} | |
private static ArrayList readFiles(String folderPath) throws NullPointerException{ | |
File file = new File(folderPath); | |
File files[] = file.listFiles(); | |
ArrayList<String> fileList = new ArrayList(); | |
for(int n=0;n<files.length;n++){ | |
fileList.add(files[n].getName()); | |
} | |
return fileList; | |
} | |
private static void readAndTweet(String path){ | |
File imageFile = new File(path); | |
ITesseract instance = new Tesseract(); | |
String result = null; | |
try { | |
instance.setLanguage("jpn"); | |
result = instance.doOCR(imageFile); | |
System.out.println(result); | |
} | |
catch (TesseractException ex) { | |
ex.printStackTrace(); | |
} | |
try { | |
Twitter tw = new TwitterFactory().getInstance(); | |
if(result != null){ | |
result += "\n #byPaper"; | |
Status status = tw.updateStatus(result); | |
} | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
private static void deleteFile(String path){ | |
java.io.File dFile = new java.io.File(path); | |
if(!dFile.exists()) { | |
return; | |
} | |
if(dFile.isFile()) { | |
dFile.delete(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment