質問編集履歴

2

ソースコードの編集

2019/12/23 02:50

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -34,25 +34,101 @@
34
34
 
35
35
  ```java
36
36
 
37
+
38
+
39
+ package com.example.textrecognizer;
40
+
41
+
42
+
43
+
44
+
45
+ import android.content.Context;
46
+
47
+
48
+
49
+ import android.graphics.Bitmap;
50
+
51
+ import android.graphics.Canvas;
52
+
53
+ import android.graphics.Rect;
54
+
55
+ import android.support.annotation.NonNull;
56
+
57
+ import android.support.v7.app.AppCompatActivity;
58
+
59
+ import android.os.Bundle;
60
+
61
+ import android.util.Log;
62
+
63
+ import android.util.SparseArray;
64
+
65
+ import android.view.SurfaceHolder;
66
+
67
+ import android.view.SurfaceView;
68
+
69
+ import android.view.View;
70
+
71
+ import android.widget.Button;
72
+
73
+ import android.widget.TextView;
74
+
75
+ import com.google.android.gms.vision.CameraSource;
76
+
77
+ import com.google.android.gms.vision.Detector;
78
+
79
+ import com.google.android.gms.vision.text.TextBlock;
80
+
81
+ import com.google.android.gms.vision.text.TextRecognizer;
82
+
83
+ import java.io.FileOutputStream;
84
+
85
+ import java.io.IOException;
86
+
87
+
88
+
89
+
90
+
37
- public final class MainActivity extends AppCompatActivity {
91
+ public class MainActivity extends AppCompatActivity {
92
+
93
+
94
+
95
+
96
+
38
-
97
+ private SurfaceView mCameraView;
98
+
99
+ private TextView mTextView;
100
+
39
- private CameraSource cameraSource;
101
+ private CameraSource mCameraSource;
40
-
102
+
41
- ~省略~
103
+ private TextRecognizer textRecognizer;
104
+
42
-
105
+ private CameraPermission cameraPermission;
106
+
107
+
108
+
109
+
110
+
111
+
112
+
43
- @Override
113
+ @Override
44
-
114
+
45
- public void onCreate(Bundle bundle) {
115
+ protected void onCreate(Bundle savedInstanceState) {
46
-
116
+
47
- super.onCreate(bundle);
117
+ super.onCreate(savedInstanceState);
48
-
118
+
49
- setContentView(R.layout.ocr_capture);
119
+ setContentView(R.layout.activity_main);
120
+
121
+
122
+
50
-
123
+ cameraPermission = new CameraPermission(this);
51
-
52
-
124
+
53
- preview = (CameraSourcePreview) findViewById(R.id.preview);
125
+ mCameraView = findViewById(R.id.cameraView);
54
-
126
+
55
- graphicOverlay = (GraphicOverlay<OcrGraphic>) findViewById(R.id.graphicOverlay);
127
+ mTextView = findViewById(R.id.text_view);
128
+
129
+
130
+
131
+ startCamera();
56
132
 
57
133
 
58
134
 
@@ -62,19 +138,315 @@
62
138
 
63
139
  @Override
64
140
 
65
- public void onClick(View view) {
141
+ public void onClick(View v) {
142
+
66
-
143
+ Log.d("debug", "ボタンをクリックしました");
144
+
145
+
146
+
67
-
147
+ Canvas canvas = mCameraView.getHolder().lockCanvas();
148
+
68
-
149
+ //別途BitmapとCanvasを用意する
150
+
151
+ Bitmap bmp = Bitmap.createBitmap(mCameraView.getWidth(), mCameraView.getHeight(), Bitmap.Config.ARGB_8888);
152
+
153
+ Canvas tmpCanvas = new Canvas(bmp);
154
+
69
- //ボタン押すと、ビットマップを取得できるよにしたい。
155
+ //TODO tmpCanvasに対して描画処理
156
+
157
+ Rect mScreenRect = new Rect(0, 0, mCameraView.getWidth(), mCameraView.getHeight());
158
+
159
+ tmpCanvas.drawBitmap(bmp, null, mScreenRect, null);
160
+
161
+ mCameraView.getHolder().unlockCanvasAndPost(canvas);
70
162
 
71
163
 
72
164
 
73
165
  }
74
166
 
167
+ }); // btnRead
168
+
169
+
170
+
171
+ }
172
+
173
+
174
+
175
+ // ファイルを保存
176
+
177
+ public void saveFile(String file, Bitmap bitmap) {
178
+
179
+ // try-with-resources
180
+
181
+ try (FileOutputStream fileOutputstream = openFileOutput(file,
182
+
183
+ Context.MODE_PRIVATE)) {
184
+
185
+ //bitmapをJpeg画像化
186
+
187
+ bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputstream);
188
+
189
+ fileOutputstream.flush();
190
+
191
+ } catch (IOException e) {
192
+
193
+ e.printStackTrace();
194
+
195
+ }
196
+
197
+ }
198
+
199
+
200
+
201
+
202
+
203
+ /**
204
+
205
+ * Starts camera source after camera permission is granted
206
+
207
+ *
208
+
209
+ * @param requestCode
210
+
211
+ * @param permissions
212
+
213
+ * @param grantResults
214
+
215
+ */
216
+
217
+ @Override
218
+
219
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
220
+
221
+ if (requestCode == cameraPermission.getCameraPermissionID()) {
222
+
223
+
224
+
225
+ if (cameraPermission.checkHasCameraPermission()) {
226
+
227
+
228
+
229
+ Log.i("onRequestResult", "Permission has been granted");
230
+
231
+ try {
232
+
233
+ mCameraSource.start(mCameraView.getHolder());
234
+
235
+ } catch (IOException e) {
236
+
237
+ e.printStackTrace();
238
+
239
+ }
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
247
+ }
248
+
249
+
250
+
251
+
252
+
253
+ /**
254
+
255
+ * Gets TextBlock from TextRecognizer, set Text to TextView.
256
+
257
+ */
258
+
259
+ private void setDataToTextView() {
260
+
261
+
262
+
263
+ textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
264
+
265
+ @Override
266
+
267
+ public void receiveDetections(Detector.Detections<TextBlock> detections) {
268
+
269
+ final SparseArray<TextBlock> items = detections.getDetectedItems();
270
+
271
+
272
+
273
+ if (items.size() != 0) {
274
+
275
+ mTextView.post(new Runnable() {
276
+
277
+ @Override
278
+
279
+ public void run() {
280
+
281
+
282
+
283
+ //Gets strings from TextBlock and adds to StringBuilder
284
+
285
+ final StringBuilder stringBuilder = new StringBuilder();
286
+
287
+ for (int i = 0; i < items.size(); i++)
288
+
289
+ stringBuilder.append(items.valueAt(i).getValue());
290
+
291
+
292
+
293
+ mTextView.setText(stringBuilder.toString());
294
+
295
+
296
+
297
+ }
298
+
299
+ });
300
+
301
+ }
302
+
303
+ }
304
+
305
+
306
+
307
+ @Override
308
+
309
+ public void release() {
310
+
311
+ }
312
+
75
313
  });
76
314
 
77
-
315
+ }
316
+
317
+
318
+
319
+ /**
320
+
321
+ * Init camera source with needed properties,
322
+
323
+ * then set camera view to surface view.
324
+
325
+ * 必要なプロパティでカメラソースを初期化し、
326
+
327
+ * カメラビューをサーフェスビューに設定します。
328
+
329
+ */
330
+
331
+ private void startCamera() {
332
+
333
+
334
+
335
+ textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
336
+
337
+
338
+
339
+ if (textRecognizer.isOperational()) {
340
+
341
+
342
+
343
+ mCameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
344
+
345
+ .setFacing(CameraSource.CAMERA_FACING_BACK)
346
+
347
+ .setRequestedPreviewSize(1280, 1024)
348
+
349
+ .setAutoFocusEnabled(true)
350
+
351
+ .setRequestedFps(30.0f)
352
+
353
+ .build();
354
+
355
+
356
+
357
+ //If permission is granted cameraSource started and passed it to surfaceView
358
+
359
+ mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
360
+
361
+ @Override
362
+
363
+ public void surfaceCreated(SurfaceHolder holder) {
364
+
365
+ if (cameraPermission.checkHasCameraPermission()) {
366
+
367
+
368
+
369
+ try {
370
+
371
+ mCameraSource.start(mCameraView.getHolder());
372
+
373
+
374
+
375
+ } catch (IOException e) {
376
+
377
+ e.printStackTrace();
378
+
379
+ }
380
+
381
+
382
+
383
+ } else {
384
+
385
+
386
+
387
+ Log.i("surfaceCreated", "Permission request sent");
388
+
389
+ cameraPermission.requestCameraPermission();
390
+
391
+ }
392
+
393
+ Log.e(TAG, "surfaceCreated()");
394
+
395
+ }
396
+
397
+
398
+
399
+ @Override
400
+
401
+ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
402
+
403
+ Log.e(TAG, "surfaceChanged()");
404
+
405
+
406
+
407
+ }
408
+
409
+
410
+
411
+ @Override
412
+
413
+ public void surfaceDestroyed(SurfaceHolder holder) {
414
+
415
+ mCameraSource.stop();
416
+
417
+ Log.e(TAG, "surfaceDestroyed()");
418
+
419
+ }
420
+
421
+ });
422
+
423
+
424
+
425
+ setDataToTextView();
426
+
427
+ Log.e(TAG, "setDataToTextView()");
428
+
429
+
430
+
431
+ }
432
+
433
+ }
434
+
435
+
436
+
437
+ @Override
438
+
439
+ public void onPause() {
440
+
441
+ super.onPause();
442
+
443
+ }
444
+
445
+
446
+
447
+
448
+
449
+ }
78
450
 
79
451
  ```
80
452
 
@@ -84,94 +456,82 @@
84
456
 
85
457
 
86
458
 
87
- <FrameLayout
88
-
89
- xmlns:android="http://schemas.android.com/apk/res/android"
459
+ <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
90
-
91
- android:id="@+id/frameLayout"
92
460
 
93
461
  android:layout_width="match_parent"
94
462
 
95
463
  android:layout_height="match_parent"
96
464
 
465
+ xmlns:app="http://schemas.android.com/apk/res-auto"
466
+
97
467
  android:orientation="vertical"
98
468
 
469
+ android:weightSum="10">
470
+
471
+
472
+
473
+ <SurfaceView
474
+
475
+ android:id="@+id/cameraView"
476
+
477
+ android:scaleType="fitCenter"
478
+
479
+ android:layout_width="0dp"
480
+
99
- android:keepScreenOn="true"
481
+ android:layout_height="0dp"
482
+
100
-
483
+ app:layout_constraintBottom_toBottomOf="parent"
484
+
485
+ app:layout_constraintLeft_toLeftOf="parent"
486
+
487
+ app:layout_constraintRight_toRightOf="parent"
488
+
101
- android:fitsSystemWindows="false" >
489
+ app:layout_constraintTop_toTopOf="parent" />
102
-
103
-
104
-
105
-
106
-
490
+
491
+
492
+
107
- <com.google.android.gms.samples.vision.ocrreader.ui.camera.CameraSourcePreview
493
+ <TextView
108
-
494
+
109
- android:id="@+id/preview"
495
+ android:id="@+id/text_view"
110
496
 
111
497
  android:layout_width="match_parent"
112
498
 
113
- android:layout_height="match_parent">
114
-
115
-
116
-
117
- <com.google.android.gms.samples.vision.ocrreader.ui.camera.GraphicOverlay
118
-
119
- android:id="@+id/graphicOverlay"
120
-
121
- android:layout_width="match_parent"
122
-
123
- android:layout_height="match_parent"/>
124
-
125
-
126
-
127
- </com.google.android.gms.samples.vision.ocrreader.ui.camera.CameraSourcePreview>
128
-
129
-
130
-
131
-
132
-
133
- <androidx.constraintlayout.widget.ConstraintLayout
134
-
135
- xmlns:android="http://schemas.android.com/apk/res/android"
136
-
137
- xmlns:app="http://schemas.android.com/apk/res-auto"
138
-
139
- xmlns:tools="http://schemas.android.com/tools"
140
-
141
- android:layout_width="match_parent"
142
-
143
- android:layout_height="match_parent"
499
+ android:layout_height="50dp"
144
-
500
+
145
- tools:context=".MainActivity">
501
+ android:gravity="bottom"
502
+
146
-
503
+ android:textColor="@android:color/white"
504
+
147
-
505
+ android:textSize="20sp"
506
+
148
-
507
+ android:textStyle="bold"
508
+
509
+ app:layout_constraintLeft_toLeftOf="parent"
510
+
511
+ app:layout_constraintRight_toRightOf="parent"
512
+
513
+ app:layout_constraintTop_toTopOf="parent" />
514
+
515
+
516
+
149
- <Button
517
+ <Button
150
-
518
+
151
- android:id="@+id/button"
519
+ android:id="@+id/button"
152
-
153
- android:text="Hello World!"
520
+
154
-
155
- android:layout_width="wrap_content"
521
+ android:layout_width="wrap_content"
156
-
522
+
157
- android:layout_height="wrap_content"
523
+ android:layout_height="wrap_content"
158
-
524
+
159
- app:layout_constraintBottom_toBottomOf="parent"
525
+ app:layout_constraintBottom_toBottomOf="parent"
160
-
526
+
161
- app:layout_constraintLeft_toLeftOf="parent"
527
+ app:layout_constraintLeft_toLeftOf="parent"
162
-
528
+
163
- app:layout_constraintRight_toRightOf="parent"
529
+ app:layout_constraintRight_toRightOf="parent" />
164
-
165
- />
530
+
166
-
167
-
168
-
531
+
532
+
533
+
534
+
169
- </androidx.constraintlayout.widget.ConstraintLayout>
535
+ </android.support.constraint.ConstraintLayout>
170
-
171
-
172
-
173
-
174
-
175
- </FrameLayout>
176
536
 
177
537
  ```

1

質問の加筆修正

2019/12/23 02:50

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -21,6 +21,8 @@
21
21
  SurfaceViewはgetBitmap()メソッドが存在しないようなので、別の方法でbitmapを取得できるのでしょうか?
22
22
 
23
23
  あれば教えていただきたいです。
24
+
25
+ とにかくSurfaceViewのキャプチャ画像が取得できれば大成功です。
24
26
 
25
27
 
26
28