質問編集履歴
2
ソースコードの編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -16,74 +16,254 @@
|
|
16
16
|
|
17
17
|
以下に簡単なコードを載せます
|
18
18
|
```java
|
19
|
-
public final class MainActivity extends AppCompatActivity {
|
20
|
-
private CameraSource cameraSource;
|
21
|
-
~省略~
|
22
|
-
@Override
|
23
|
-
public void onCreate(Bundle bundle) {
|
24
|
-
super.onCreate(bundle);
|
25
|
-
setContentView(R.layout.ocr_capture);
|
26
19
|
|
27
|
-
|
20
|
+
package com.example.textrecognizer;
|
28
|
-
graphicOverlay = (GraphicOverlay<OcrGraphic>) findViewById(R.id.graphicOverlay);
|
29
21
|
|
22
|
+
|
23
|
+
import android.content.Context;
|
24
|
+
|
25
|
+
import android.graphics.Bitmap;
|
26
|
+
import android.graphics.Canvas;
|
27
|
+
import android.graphics.Rect;
|
28
|
+
import android.support.annotation.NonNull;
|
29
|
+
import android.support.v7.app.AppCompatActivity;
|
30
|
+
import android.os.Bundle;
|
31
|
+
import android.util.Log;
|
32
|
+
import android.util.SparseArray;
|
33
|
+
import android.view.SurfaceHolder;
|
34
|
+
import android.view.SurfaceView;
|
35
|
+
import android.view.View;
|
36
|
+
import android.widget.Button;
|
37
|
+
import android.widget.TextView;
|
38
|
+
import com.google.android.gms.vision.CameraSource;
|
39
|
+
import com.google.android.gms.vision.Detector;
|
40
|
+
import com.google.android.gms.vision.text.TextBlock;
|
41
|
+
import com.google.android.gms.vision.text.TextRecognizer;
|
42
|
+
import java.io.FileOutputStream;
|
43
|
+
import java.io.IOException;
|
44
|
+
|
45
|
+
|
46
|
+
public class MainActivity extends AppCompatActivity {
|
47
|
+
|
48
|
+
|
49
|
+
private SurfaceView mCameraView;
|
50
|
+
private TextView mTextView;
|
51
|
+
private CameraSource mCameraSource;
|
52
|
+
private TextRecognizer textRecognizer;
|
53
|
+
private CameraPermission cameraPermission;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
@Override
|
58
|
+
protected void onCreate(Bundle savedInstanceState) {
|
59
|
+
super.onCreate(savedInstanceState);
|
60
|
+
setContentView(R.layout.activity_main);
|
61
|
+
|
62
|
+
cameraPermission = new CameraPermission(this);
|
63
|
+
mCameraView = findViewById(R.id.cameraView);
|
64
|
+
mTextView = findViewById(R.id.text_view);
|
65
|
+
|
66
|
+
startCamera();
|
67
|
+
|
30
68
|
Button button = (Button) findViewById(R.id.button);
|
31
69
|
button.setOnClickListener(new View.OnClickListener() {
|
32
70
|
@Override
|
33
|
-
public void onClick(View
|
71
|
+
public void onClick(View v) {
|
72
|
+
Log.d("debug", "ボタンをクリックしました");
|
34
73
|
|
74
|
+
Canvas canvas = mCameraView.getHolder().lockCanvas();
|
75
|
+
//別途BitmapとCanvasを用意する
|
76
|
+
Bitmap bmp = Bitmap.createBitmap(mCameraView.getWidth(), mCameraView.getHeight(), Bitmap.Config.ARGB_8888);
|
77
|
+
Canvas tmpCanvas = new Canvas(bmp);
|
35
|
-
//
|
78
|
+
//TODO tmpCanvasに対して描画処理を行う
|
79
|
+
Rect mScreenRect = new Rect(0, 0, mCameraView.getWidth(), mCameraView.getHeight());
|
80
|
+
tmpCanvas.drawBitmap(bmp, null, mScreenRect, null);
|
81
|
+
mCameraView.getHolder().unlockCanvasAndPost(canvas);
|
36
82
|
|
37
83
|
}
|
84
|
+
}); // btnRead
|
85
|
+
|
86
|
+
}
|
87
|
+
|
88
|
+
// ファイルを保存
|
89
|
+
public void saveFile(String file, Bitmap bitmap) {
|
90
|
+
// try-with-resources
|
91
|
+
try (FileOutputStream fileOutputstream = openFileOutput(file,
|
92
|
+
Context.MODE_PRIVATE)) {
|
93
|
+
//bitmapをJpeg画像化
|
94
|
+
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputstream);
|
95
|
+
fileOutputstream.flush();
|
96
|
+
} catch (IOException e) {
|
97
|
+
e.printStackTrace();
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
/**
|
103
|
+
* Starts camera source after camera permission is granted
|
104
|
+
*
|
105
|
+
* @param requestCode
|
106
|
+
* @param permissions
|
107
|
+
* @param grantResults
|
108
|
+
*/
|
109
|
+
@Override
|
110
|
+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
111
|
+
if (requestCode == cameraPermission.getCameraPermissionID()) {
|
112
|
+
|
113
|
+
if (cameraPermission.checkHasCameraPermission()) {
|
114
|
+
|
115
|
+
Log.i("onRequestResult", "Permission has been granted");
|
116
|
+
try {
|
117
|
+
mCameraSource.start(mCameraView.getHolder());
|
118
|
+
} catch (IOException e) {
|
119
|
+
e.printStackTrace();
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
}
|
125
|
+
|
126
|
+
|
127
|
+
/**
|
128
|
+
* Gets TextBlock from TextRecognizer, set Text to TextView.
|
129
|
+
*/
|
130
|
+
private void setDataToTextView() {
|
131
|
+
|
132
|
+
textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
|
133
|
+
@Override
|
134
|
+
public void receiveDetections(Detector.Detections<TextBlock> detections) {
|
135
|
+
final SparseArray<TextBlock> items = detections.getDetectedItems();
|
136
|
+
|
137
|
+
if (items.size() != 0) {
|
138
|
+
mTextView.post(new Runnable() {
|
139
|
+
@Override
|
140
|
+
public void run() {
|
141
|
+
|
142
|
+
//Gets strings from TextBlock and adds to StringBuilder
|
143
|
+
final StringBuilder stringBuilder = new StringBuilder();
|
144
|
+
for (int i = 0; i < items.size(); i++)
|
145
|
+
stringBuilder.append(items.valueAt(i).getValue());
|
146
|
+
|
147
|
+
mTextView.setText(stringBuilder.toString());
|
148
|
+
|
149
|
+
}
|
150
|
+
});
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
@Override
|
155
|
+
public void release() {
|
156
|
+
}
|
38
157
|
});
|
158
|
+
}
|
39
159
|
|
160
|
+
/**
|
161
|
+
* Init camera source with needed properties,
|
162
|
+
* then set camera view to surface view.
|
163
|
+
* 必要なプロパティでカメラソースを初期化し、
|
164
|
+
* カメラビューをサーフェスビューに設定します。
|
165
|
+
*/
|
166
|
+
private void startCamera() {
|
167
|
+
|
168
|
+
textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
|
169
|
+
|
170
|
+
if (textRecognizer.isOperational()) {
|
171
|
+
|
172
|
+
mCameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
|
173
|
+
.setFacing(CameraSource.CAMERA_FACING_BACK)
|
174
|
+
.setRequestedPreviewSize(1280, 1024)
|
175
|
+
.setAutoFocusEnabled(true)
|
176
|
+
.setRequestedFps(30.0f)
|
177
|
+
.build();
|
178
|
+
|
179
|
+
//If permission is granted cameraSource started and passed it to surfaceView
|
180
|
+
mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
|
181
|
+
@Override
|
182
|
+
public void surfaceCreated(SurfaceHolder holder) {
|
183
|
+
if (cameraPermission.checkHasCameraPermission()) {
|
184
|
+
|
185
|
+
try {
|
186
|
+
mCameraSource.start(mCameraView.getHolder());
|
187
|
+
|
188
|
+
} catch (IOException e) {
|
189
|
+
e.printStackTrace();
|
190
|
+
}
|
191
|
+
|
192
|
+
} else {
|
193
|
+
|
194
|
+
Log.i("surfaceCreated", "Permission request sent");
|
195
|
+
cameraPermission.requestCameraPermission();
|
196
|
+
}
|
197
|
+
Log.e(TAG, "surfaceCreated()");
|
198
|
+
}
|
199
|
+
|
200
|
+
@Override
|
201
|
+
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
202
|
+
Log.e(TAG, "surfaceChanged()");
|
203
|
+
|
204
|
+
}
|
205
|
+
|
206
|
+
@Override
|
207
|
+
public void surfaceDestroyed(SurfaceHolder holder) {
|
208
|
+
mCameraSource.stop();
|
209
|
+
Log.e(TAG, "surfaceDestroyed()");
|
210
|
+
}
|
211
|
+
});
|
212
|
+
|
213
|
+
setDataToTextView();
|
214
|
+
Log.e(TAG, "setDataToTextView()");
|
215
|
+
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
@Override
|
220
|
+
public void onPause() {
|
221
|
+
super.onPause();
|
222
|
+
}
|
223
|
+
|
224
|
+
|
225
|
+
}
|
40
226
|
```
|
41
227
|
|
42
228
|
```xml
|
43
229
|
|
44
|
-
<FrameLayout
|
45
|
-
|
230
|
+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
46
|
-
android:id="@+id/frameLayout"
|
47
231
|
android:layout_width="match_parent"
|
48
232
|
android:layout_height="match_parent"
|
233
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
49
234
|
android:orientation="vertical"
|
50
|
-
android:
|
235
|
+
android:weightSum="10">
|
51
|
-
android:fitsSystemWindows="false" >
|
52
236
|
|
237
|
+
<SurfaceView
|
238
|
+
android:id="@+id/cameraView"
|
239
|
+
android:scaleType="fitCenter"
|
240
|
+
android:layout_width="0dp"
|
241
|
+
android:layout_height="0dp"
|
242
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
243
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
244
|
+
app:layout_constraintRight_toRightOf="parent"
|
245
|
+
app:layout_constraintTop_toTopOf="parent" />
|
53
246
|
|
54
|
-
<
|
247
|
+
<TextView
|
55
|
-
android:id="@+id/
|
248
|
+
android:id="@+id/text_view"
|
56
249
|
android:layout_width="match_parent"
|
57
|
-
android:layout_height="
|
250
|
+
android:layout_height="50dp"
|
251
|
+
android:gravity="bottom"
|
252
|
+
android:textColor="@android:color/white"
|
253
|
+
android:textSize="20sp"
|
254
|
+
android:textStyle="bold"
|
255
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
256
|
+
app:layout_constraintRight_toRightOf="parent"
|
257
|
+
app:layout_constraintTop_toTopOf="parent" />
|
58
258
|
|
59
|
-
|
259
|
+
<Button
|
60
|
-
|
260
|
+
android:id="@+id/button"
|
61
|
-
|
261
|
+
android:layout_width="wrap_content"
|
62
|
-
|
262
|
+
android:layout_height="wrap_content"
|
263
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
264
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
265
|
+
app:layout_constraintRight_toRightOf="parent" />
|
63
266
|
|
64
|
-
</com.google.android.gms.samples.vision.ocrreader.ui.camera.CameraSourcePreview>
|
65
267
|
|
66
|
-
|
67
|
-
<androidx.constraintlayout.widget.ConstraintLayout
|
68
|
-
xmlns:android="http://schemas.android.com/apk/res/android"
|
69
|
-
xmlns:app="http://schemas.android.com/apk/res-auto"
|
70
|
-
xmlns:tools="http://schemas.android.com/tools"
|
71
|
-
android:layout_width="match_parent"
|
72
|
-
android:layout_height="match_parent"
|
73
|
-
tools:context=".MainActivity">
|
74
|
-
|
75
|
-
<Button
|
76
|
-
android:id="@+id/button"
|
77
|
-
android:text="Hello World!"
|
78
|
-
android:layout_width="wrap_content"
|
79
|
-
android:layout_height="wrap_content"
|
80
|
-
app:layout_constraintBottom_toBottomOf="parent"
|
81
|
-
app:layout_constraintLeft_toLeftOf="parent"
|
82
|
-
app:layout_constraintRight_toRightOf="parent"
|
83
|
-
/>
|
84
|
-
|
85
|
-
|
268
|
+
</android.support.constraint.ConstraintLayout>
|
86
|
-
|
87
|
-
|
88
|
-
</FrameLayout>
|
89
269
|
```
|
1
質問の加筆修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,6 +10,7 @@
|
|
10
10
|
しかし今回、SurfaceViewを使っており、どうしてもBitmapが取得したい状況です。
|
11
11
|
SurfaceViewはgetBitmap()メソッドが存在しないようなので、別の方法でbitmapを取得できるのでしょうか?
|
12
12
|
あれば教えていただきたいです。
|
13
|
+
とにかくSurfaceViewのキャプチャ画像が取得できれば大成功です。
|
13
14
|
|
14
15
|
色々インターネットで検索してみましたが、良い解決策が見つかりませんでしたので、どなたかご教授いただければ嬉しいです。
|
15
16
|
|