質問編集履歴

1

ソースコードを分割して見やすくしました。再描画するためのonResume()メソッドは、今回の問題とはあまり関係なのかな?と思い割愛しました。

2020/09/08 00:07

投稿

Takuya615
Takuya615

スコア9

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  ### 該当のソースコード
22
22
 
23
-
23
+ 録画時のボタンアクション。
24
24
 
25
25
  ```ここに言語名を入力
26
26
 
@@ -110,256 +110,174 @@
110
110
 
111
111
 
112
112
 
113
+
114
+
115
+ ```
116
+
117
+
118
+
119
+
120
+
121
+ openCamera() のコード
122
+
123
+ ```
124
+
125
+ fun openCamera(width: Int, height: Int) {
126
+
113
- private fun prepareBackgroundHandler() {
127
+ val manager: CameraManager =
114
-
128
+
115
- backgroundThread = HandlerThread("backgroundThread")
129
+ getSystemService(Context.CAMERA_SERVICE) as CameraManager
130
+
116
-
131
+ //CameraIdListのゼロ番目は多くの場合、背面カメラの1倍
132
+
133
+
134
+
135
+ val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
136
+
137
+ val permission2 = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
138
+
139
+
140
+
141
+ if (permission != PackageManager.PERMISSION_GRANTED) {
142
+
117
- backgroundThread?.start()
143
+ requestCameraPermission()
118
-
144
+
119
- backgroundHandler = Handler(backgroundThread?.looper)
145
+ return
120
-
146
+
121
- }
147
+ }
148
+
149
+
150
+
151
+ if (permission2 != PackageManager.PERMISSION_GRANTED) {
152
+
153
+ requestRECORD_AUDIOPermission()
154
+
155
+ return
156
+
157
+ }
158
+
159
+
160
+
161
+ setUpCameraOptions()
162
+
163
+ configureTransform(width, height)
164
+
165
+ //ここにオプションやコンフィグトランスフォームの設定関数入れる?
166
+
167
+
168
+
169
+ manager.openCamera(cameraId, stateCallback, backgroundHandler);
170
+
171
+
172
+
173
+ }
174
+
175
+ ```
176
+
177
+
178
+
179
+ openCamera()で使う設定
180
+
181
+ ```
122
182
 
123
183
 
124
184
 
125
- override fun onResume() {
126
-
127
- super.onResume()
128
-
129
-
130
-
131
- prepareBackgroundHandler()
132
-
133
-
134
-
135
- if (textureView.isAvailable) {
136
-
137
- openCamera(textureView.width, textureView.height)
138
-
139
- } else {
140
-
141
- textureView.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
142
-
143
- override fun onSurfaceTextureAvailable(
144
-
145
- surface: SurfaceTexture,
146
-
147
- width: Int,
148
-
149
- height: Int
150
-
151
- ) {
152
-
153
- imageReader = ImageReader.newInstance(
154
-
155
- width, height,
156
-
157
- ImageFormat.JPEG, /*maxImages*/ 2
158
-
159
- );
160
-
161
- openCamera(width, height)
185
+ //           ↑  上のopenCameraの第2引数として利用  ↑
186
+
187
+ private val stateCallback = object : CameraDevice.StateCallback() {
188
+
189
+ override fun onOpened(cameraDevice: CameraDevice) {
190
+
191
+ this@Camera2Activity.cameraDevice = cameraDevice
192
+
193
+ createCameraPreviewSession()
194
+
195
+ }
196
+
197
+
198
+
199
+ override fun onDisconnected(cameraDevice: CameraDevice) {
200
+
201
+ cameraDevice.close()
202
+
203
+ this@Camera2Activity.cameraDevice = null
204
+
205
+ }
206
+
207
+
208
+
209
+ override fun onError(cameraDevice: CameraDevice, error: Int) {
210
+
211
+ onDisconnected(cameraDevice)
212
+
213
+ finish()
214
+
215
+ }
216
+
217
+ }
218
+
219
+ // ↑  上のstateCallback関数のonOpendで使われるl関数   ↑
220
+
221
+ private fun createCameraPreviewSession() {
222
+
223
+
224
+
225
+ val texture = textureView.surfaceTexture
226
+
227
+ texture.setDefaultBufferSize(previewSize.width, previewSize.height)
228
+
229
+ val surface = Surface(texture)//TextureViewからの撮影データの受取先
230
+
231
+
232
+
233
+ previewRequestBuilder = cameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
234
+
235
+ previewRequestBuilder.addTarget(surface)
236
+
237
+
238
+
239
+ // CameraCaptureSession はカメラから送られてくる画像を受け取ったり、
240
+
241
+ // 同一セッションで前に受け取った画像の再処理などをするクラスです
242
+
243
+ cameraDevice?.createCaptureSession(Arrays.asList(surface, imageReader?.surface),
244
+
245
+ @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
246
+
247
+ object : CameraCaptureSession.StateCallback() {
248
+
249
+ override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
250
+
251
+ if (cameraDevice == null) return;
252
+
253
+ captureSession = cameraCaptureSession
254
+
255
+
256
+
257
+ previewRequestBuilder.set(
258
+
259
+ CaptureRequest.CONTROL_AF_MODE,
260
+
261
+ CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE
262
+
263
+ )
264
+
265
+
266
+
267
+ //プレビュー画像を TextureView に送ります
268
+
269
+ previewRequest = previewRequestBuilder.build()
270
+
271
+ captureSession?.setRepeatingRequest(
272
+
273
+ previewRequest,
274
+
275
+ null, null
276
+
277
+ )
162
278
 
163
279
  }
164
280
 
165
-
166
-
167
- override fun onSurfaceTextureSizeChanged(
168
-
169
- p0: SurfaceTexture?,
170
-
171
- p1: Int,
172
-
173
- p2: Int
174
-
175
- ) {
176
-
177
- configureTransform(p1, p2)
178
-
179
- }
180
-
181
-
182
-
183
- override fun onSurfaceTextureUpdated(p0: SurfaceTexture?) {
184
-
185
- }
186
-
187
-
188
-
189
- override fun onSurfaceTextureDestroyed(p0: SurfaceTexture?): Boolean {
190
-
191
- Log.e("TAG", "onSurfaceTextureDestroyedの関数が仕事してます");
192
-
193
- if(cameraDevice != null){
194
-
195
- closeCamera();
196
-
197
-
198
-
199
- cameraDevice = null;
200
-
201
- }
202
-
203
- return false
204
-
205
- }
206
-
207
- }
208
-
209
- }
210
-
211
- }
212
-
213
-
214
-
215
-
216
-
217
- fun openCamera(width: Int, height: Int) {
218
-
219
- val manager: CameraManager =
220
-
221
- getSystemService(Context.CAMERA_SERVICE) as CameraManager
222
-
223
- //CameraIdListのゼロ番目は多くの場合、背面カメラの1倍
224
-
225
-
226
-
227
- val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
228
-
229
- val permission2 = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
230
-
231
-
232
-
233
- if (permission != PackageManager.PERMISSION_GRANTED) {
234
-
235
- requestCameraPermission()
236
-
237
- return
238
-
239
- }
240
-
241
-
242
-
243
- if (permission2 != PackageManager.PERMISSION_GRANTED) {
244
-
245
- requestRECORD_AUDIOPermission()
246
-
247
- return
248
-
249
- }
250
-
251
-
252
-
253
- setUpCameraOptions()
254
-
255
- configureTransform(width, height)
256
-
257
- //ここにオプションやコンフィグトランスフォームの設定関数入れる?
258
-
259
-
260
-
261
- manager.openCamera(cameraId, stateCallback, backgroundHandler);
262
-
263
-
264
-
265
- }
266
-
267
- //           ↑  上のopenCameraの第2引数として利用  ↑
268
-
269
- private val stateCallback = object : CameraDevice.StateCallback() {
270
-
271
- override fun onOpened(cameraDevice: CameraDevice) {
272
-
273
- this@Camera2Activity.cameraDevice = cameraDevice
274
-
275
- createCameraPreviewSession()
276
-
277
- }
278
-
279
-
280
-
281
- override fun onDisconnected(cameraDevice: CameraDevice) {
282
-
283
- cameraDevice.close()
284
-
285
- this@Camera2Activity.cameraDevice = null
286
-
287
- }
288
-
289
-
290
-
291
- override fun onError(cameraDevice: CameraDevice, error: Int) {
292
-
293
- onDisconnected(cameraDevice)
294
-
295
- finish()
296
-
297
- }
298
-
299
- }
300
-
301
- // ↑  上のstateCallback関数のonOpendで使われるl関数   ↑
302
-
303
- private fun createCameraPreviewSession() {
304
-
305
-
306
-
307
- val texture = textureView.surfaceTexture
308
-
309
- texture.setDefaultBufferSize(previewSize.width, previewSize.height)
310
-
311
- val surface = Surface(texture)//TextureViewからの撮影データの受取先
312
-
313
-
314
-
315
- previewRequestBuilder = cameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
316
-
317
- previewRequestBuilder.addTarget(surface)
318
-
319
-
320
-
321
- // CameraCaptureSession はカメラから送られてくる画像を受け取ったり、
322
-
323
- // 同一セッションで前に受け取った画像の再処理などをするクラスです
324
-
325
- cameraDevice?.createCaptureSession(Arrays.asList(surface, imageReader?.surface),
326
-
327
- @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
328
-
329
- object : CameraCaptureSession.StateCallback() {
330
-
331
- override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
332
-
333
- if (cameraDevice == null) return;
334
-
335
- captureSession = cameraCaptureSession
336
-
337
-
338
-
339
- previewRequestBuilder.set(
340
-
341
- CaptureRequest.CONTROL_AF_MODE,
342
-
343
- CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE
344
-
345
- )
346
-
347
-
348
-
349
- //プレビュー画像を TextureView に送ります
350
-
351
- previewRequest = previewRequestBuilder.build()
352
-
353
- captureSession?.setRepeatingRequest(
354
-
355
- previewRequest,
356
-
357
- null, null
358
-
359
- )
360
-
361
- }
362
-
363
281
  override fun onConfigureFailed(session: CameraCaptureSession) {}
364
282
 
365
283
  }, null
@@ -368,10 +286,6 @@
368
286
 
369
287
  }
370
288
 
371
-
372
-
373
-
374
-
375
289
  ```
376
290
 
377
291