2017-09-08 3 views
0

Hallo Ich versuche, tess-zwei-API zu machen OCR AppWie kann ich mehrere Daten aus Assets lesen?

Ich brauche zu verwenden zwei Sprachen von trainierten Daten

ich meine Daten von Vermögenswerten laden, aber ich weiß nicht, wie mehr Daten zu verwenden laden von ihm

dies ist mein Code:

private void checkFile(File dir) { 
    if (!dir.exists()&& dir.mkdirs()){ 
     copyFiles(); 
    } 
    if(dir.exists()) { 
     String datafilepath = datapath+ "/tessdata/eng.traineddata"; 
     File datafile = new File(datafilepath); 

     if (!datafile.exists()) { 
      copyFiles(); 
     } 
    } 
} 

private void copyFiles() { 
    try { 
     String filepath = datapath + "/tessdata/eng.traineddata"; 
     AssetManager assetManager = getAssets(); 

     InputStream instream = assetManager.open("tessdata/eng.traineddata"); 
     OutputStream outstream = new FileOutputStream(filepath); 

     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = instream.read(buffer)) != -1) { 
      outstream.write(buffer, 0, read); 
     } 


     outstream.flush(); 
     outstream.close(); 
     instream.close(); 

     File file = new File(filepath); 
     if (!file.exists()) { 
      throw new FileNotFoundException(); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

Wie kann ich es ändern mehrere Daten zu kopieren?

Antwort

0

Sie sollten große Dateien im Hintergrund-Thread (AsyncTask oder Gewinde) kopieren, und Sie können folgende Logik mehrere trainedData Dateien verwenden zu kopieren:

private void checkFile(File dir) { 
     if (!dir.exists()) { 
      dir.mkdirs(); 
     } 
     if (dir.exists()) { 
      copyFiles("/tessdata/eng.traineddata"); 
      copyFiles("/tessdata/hin.traineddata"); 
      copyFiles("/tessdata/fra.traineddata"); 
     } 
    } 

    private void copyFiles(String path) { 
     try { 
      String filepath = datapath + path; 
      if (!new File(filepath).exists()) { 
       AssetManager assetManager = getAssets(); 

       InputStream instream = assetManager.open(path); 
       OutputStream outstream = new FileOutputStream(filepath); 

       byte[] buffer = new byte[1024]; 
       int read; 
       while ((read = instream.read(buffer)) != -1) { 
        outstream.write(buffer, 0, read); 
       } 


       outstream.flush(); 
       outstream.close(); 
       instream.close(); 

       File file = new File(filepath); 
       if (!file.exists()) { 
        throw new FileNotFoundException(); 
       } 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

ok Ich habe es für Ihre Antwort danken –

Verwandte Themen