1
으로, 초기화되지 않습니다 여기에 나는 현재하고 있어요 무엇 :TESS-이도 올바른 사용 권한
이- 이 TESS-이가 내 안드로이드 프로젝트에 설정되어
- 내 응용 프로그램의 AndroidManifest.xml에 권한이 있습니다 (tess-two AndroidManifest.xml 아님).
나는 또한 명시 적으로 내 코드에 권한을 확인 : 나는 응용 프로그램을 실행하면
private void initTess() { // Check we have the eng.traineddata file in the correct place mTessDataPath = getFilesDir() + "/tesseract/"; checkTessFile(new File(mTessDataPath + "tessdata/")); // Initialise TessBaseAPI mTess = new TessBaseAPI(); mTess.init(mTessDataPath, "eng"); } private void checkTessFile(File dir) { // Check if directory already exists if (dir.exists()) { // Check if file already exists String dataFilePath = mTessDataPath + "tessdata/eng.traineddata"; File datafile = new File(dataFilePath); if (!datafile.exists()) { // If file doesn't exist, copy it over from assets folder copyTessFiles(); } } else { if (dir.mkdirs()) { // If directory doesn't exist, but we can create it, copy file from assets folder copyTessFiles(); } } } private void copyTessFiles() { try { // Location we want the file to be at String filepath = mTessDataPath + "tessdata/eng.traineddata"; // Get access to AssetManager AssetManager assetManager = getAssets(); // Open byte streams for reading/writing InputStream instream = assetManager.open("tessdata/eng.traineddata"); OutputStream outstream = new FileOutputStream(filepath); // Copy the file to the location specified by 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(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_EXTERNAL_STORAGE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Initialise Tesseract API initTess(); } return; } } }
다음 TessBaseAPI를 초기화하는
int readPermission = ActivityCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE);
int writePermission = ActivityCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE);
// Check we have both read and write permissions
if (readPermission != PackageManager.PERMISSION_GRANTED
|| writePermission != PackageManager.PERMISSION_GRANTED)
{
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
new String[] {READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE},
REQUEST_EXTERNAL_STORAGE
);
}
else
{
Log.d(TAG, "Read and write external permissions granted");
initTess();
}
시도 , 다음 오류가 발생합니다. 로그 :
E/Tesseract(native): Could not initialize Tesseract API with language=eng!
내가 당장은 아무 생각이 없다, 그래서 어떤 도움이나 조언은 대단히 감사하겠습니다, 감사합니다 :)
권한을 처리하는 onRequestPermissionsResult 메소드는 어디에 있습니까? –
나는 이것을 보여주기 위해 질문을 편집했고 이미 (내가하는) 두 가지 권한을 이미 가지고 있다면 initTess() 메소드에 호출을 추가했다. – TinyDancer