現在、Androidでの撮影した写真を文字認識し、自動的にフォルダ分けするアプリを製作しています。
そこで、文字認識はこちらのサイト
https://www.programming-shimomura.com/2017/06/29/【android】-画像からの文字認識を実装/
写真の撮影・保存はこちらのサイト
https://akira-watson.com/android/camera-intent.html#2
を参考にしました。
文字認識のサイトの方があらかじめdrawableに画像を格納している前提での話だったので、一旦撮影した写真をdrawableに保存し、文字認識し、パスで保存フォルダを指定し、フォルダ分けする、というようにやりたいと思っています。
フォルダ分けはMapを使用しています。一番最初の配列に格納された数字で判断します。
しかし、一旦撮影した写真をdrawableに保存するというところができなくて困っています。どのようにソースコードを変えたらいいでしょうか?
現在組み合わせたコードは以下のようになっています。他にもおかしな点があったら教えていただきたいです。
Java
1public class Main6Activity extends AppCompatActivity { 2 3 private final static int RESULT_CAMERA = 1001; 4 private final static int REQUEST_PERMISSION = 1002; 5 6 private ImageView imageView; 7 private Uri cameraUri; 8 private File cameraFile; 9 private String filePath; 10 private String result; 11 12 static final String DEFAULT_LANGUAGE = "jpn"; 13 14 String filepath; 15 Bitmap bitmap; 16 TessBaseAPI tessBaseAPI; 17 18 //<キーの型, 格納する要素の型> 19 Map<String, Integer> dir_test = new HashMap<String, Integer>(){ 20 { 21 put("math",1); 22 put("Japanese",2); 23 put("English",3); 24 } 25 }; 26 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main6); 32 33 //bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1); 34 filepath = getFilesDir() + "/tesseract/"; 35 tessBaseAPI = new TessBaseAPI(); 36 checkFile(new File(filepath + "tessdata/")); 37 tessBaseAPI.init(filepath, DEFAULT_LANGUAGE); 38 39 40 if (savedInstanceState != null){ 41 cameraUri = savedInstanceState.getParcelable("CaptureUri"); 42 } 43 44 imageView = findViewById(R.id.image_view); 45 46 Button cameraButton = findViewById(R.id.camera_button); 47 cameraButton.setOnClickListener(new View.OnClickListener(){ 48 49 @Override 50 public void onClick(View v) { 51 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 52 startActivityForResult(intent, RESULT_CAMERA); 53 54 // Android 6, API 23以上でパーミッシンの確認 55 if (Build.VERSION.SDK_INT >= 23) { 56 checkPermission(); 57 } else { 58 //一旦drawableに保存する処理 59 60 61 62 cameraIntent(); 63 64 } 65 } 66 }); 67 } 68 69 private void checkFile(File file) { 70 if (!file.exists() && file.mkdirs()){ 71 copyFiles(); 72 } 73 if(file.exists()) { 74 String datafilepath = filepath+ "/tessdata/jpn.traineddata"; 75 File datafile = new File(datafilepath); 76 77 if (!datafile.exists()) { 78 copyFiles(); 79 } 80 } 81 } 82 83 private void copyFiles() { 84 try { 85 String datapath = filepath + "/tessdata/jpn.traineddata"; 86 InputStream instream = getAssets().open("tessdata/jpn.traineddata"); 87 OutputStream outstream = new FileOutputStream(datapath); 88 89 byte[] buffer = new byte[1024]; 90 int read; 91 while ((read = instream.read(buffer)) != -1) { 92 outstream.write(buffer, 0, read); 93 } 94 95 outstream.flush(); 96 outstream.close(); 97 instream.close(); 98 99 File file = new File(datapath); 100 if (!file.exists()) { 101 throw new FileNotFoundException(); 102 } 103 } catch (Exception ex) { 104 ex.printStackTrace(); 105 } 106 } 107 108 109 110 protected void onSaveInstanceState(Bundle outState) { 111 super.onSaveInstanceState(outState); 112 outState.putParcelable("CaptureUri", cameraUri); 113 } 114 115 private void cameraIntent(){ 116 // 保存先のフォルダーを作成 117 118 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1); //変える 119 String[] result = tessBaseAPI.getUTF8Text().split("\n"); 120 int a = Integer.parseInt(result[0]); //型変換 121 for(String key:dir_test.keySet()){ 122 if(a == dir_test.get(key)) { 123 // 保存ファイル名 124 String fileName = new SimpleDateFormat("ddHHmmss").format(new Date()); 125 File nestDir = new File("/storage/emulated/0/Pictures/"+key+"/"); 126 if(!nestDir.exists()){ 127 if(nestDir.mkdir()){ 128 129 } 130 } 131 filePath = "/storage/emulated/0/Pictures/"+key+"/" + fileName + ".jpg"; 132 //filePath = "/storage/emulated/0/Pictures/math" +"/" + fileName + ".jpg"; 133 registAndroidDB(filePath); 134 Log.d("debug","filePath:"+filePath); 135 136 // capture画像のファイルパス 137 cameraFile = new File(filePath); 138 cameraUri = Uri.fromFile(cameraFile); 139 140 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 141 intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri); 142 startActivityForResult(intent, RESULT_CAMERA); 143 } 144 } 145 146 147 File cameraFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "IMG"); 148 cameraFolder.mkdirs(); 149 150 151 } 152 153 @Override 154 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 155 /*if(requestCode == RESULT_CAMERA) { 156 try { 157 // 画像データの取得 158 Bitmap img = (Bitmap)data.getExtras().get("data"); 159 imageView.setImageBitmap(img); 160 } catch (Exception e) { 161 162 }*/ 163 if (requestCode == RESULT_CAMERA) { 164 if(cameraUri != null){ 165 imageView.setImageURI(cameraUri); 166 //imageView = findViewById(R.id.image_view); 167 //imageView.setImageResource(R.drawable.image1); 168 } 169 else{ 170 Log.d("debug","cameraUri == null"); 171 } 172 } 173 } 174 175 // Runtime Permission check 176 private void checkPermission(){ 177 // 既に許可している 178 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){ 179 cameraIntent(); 180 } 181 // 拒否していた場合 182 else{ 183 requestLocationPermission(); 184 } 185 } 186 187 // 許可を求める 188 private void requestLocationPermission() { 189 if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 190 ActivityCompat.requestPermissions(Main6Activity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION); 191 192 } else { 193 Toast toast = Toast.makeText(this, "許可されないとアプリが実行できません", Toast.LENGTH_SHORT); 194 toast.show(); 195 196 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,}, REQUEST_PERMISSION); 197 198 } 199 } 200 201 // 結果の受け取り 202 @Override 203 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 204 if (requestCode == REQUEST_PERMISSION) { 205 // 使用が許可された 206 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 207 cameraIntent(); 208 return; 209 210 } else { 211 // それでも拒否された時の対応 212 Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT); 213 toast.show(); 214 } 215 } 216 } 217 218 private void registAndroidDB(String path){ 219 ContentValues values = new ContentValues(); 220 ContentResolver contentResolver = Main6Activity.this.getContentResolver(); 221 values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg"); 222 values.put("_data",path); 223 contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values); 224 } 225} 226
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。