質問編集履歴

3

111

2019/12/05 23:39

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- kotlin android studio camerax
1
+ android studio
test CHANGED
File without changes

2

123

2019/12/05 23:39

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,4 @@
1
- お世話になります。kotlinでcameraxを実装しようとしているのですがエラーが出てしまいます。エラーはcapture_buttonの箇所で以下になります。
1
+ エラーはcapture_buttonの箇所で以下になります。
2
-
3
- ![イメージ説明](f7e313eac39579f04e77b53baa581f08.png)
4
-
5
-
6
2
 
7
3
  ```
8
4
 
@@ -12,6 +8,4 @@
12
8
 
13
9
  ```
14
10
 
15
-
16
-
17
11
  capture_buttonが型相違なのですが具体的にどのようにコードを修正すれば良いのかわからずご教示頂けますでしょうか?

1

a

2019/12/05 23:38

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -15,475 +15,3 @@
15
15
 
16
16
 
17
17
  capture_buttonが型相違なのですが具体的にどのようにコードを修正すれば良いのかわからずご教示頂けますでしょうか?
18
-
19
-
20
-
21
- #ためして作ったfragment_camera
22
-
23
-
24
-
25
- ```ここに言語を入力
26
-
27
- <?xml version="1.0" encoding="utf-8"?>
28
-
29
- <androidx.constraintlayout.widget.ConstraintLayout
30
-
31
- xmlns:android="http://schemas.android.com/apk/res/android"
32
-
33
- xmlns:tools="http://schemas.android.com/tools"
34
-
35
- xmlns:app="http://schemas.android.com/apk/res-auto"
36
-
37
- android:layout_width="match_parent"
38
-
39
- android:layout_height="match_parent"
40
-
41
- tools:context=".MainActivity">
42
-
43
-
44
-
45
- <TextureView
46
-
47
- android:id="@+id/view_finder"
48
-
49
- android:layout_width="640px"
50
-
51
- android:layout_height="640px"
52
-
53
- app:layout_constraintTop_toTopOf="parent"
54
-
55
- app:layout_constraintBottom_toBottomOf="parent"
56
-
57
- app:layout_constraintStart_toStartOf="parent"
58
-
59
- app:layout_constraintEnd_toEndOf="parent" />
60
-
61
- <ImageButton
62
-
63
- android:id="@+id/capture_button" >>>>>>>>>>>>>>>ここで設定しているがエラーとなる。
64
-
65
- android:layout_width="72dp"
66
-
67
- android:layout_height="72dp"
68
-
69
- android:layout_margin="24dp"
70
-
71
- app:srcCompat="@android:drawable/ic_menu_camera"
72
-
73
- app:layout_constraintBottom_toBottomOf="parent"
74
-
75
- app:layout_constraintEnd_toEndOf="parent"
76
-
77
- app:layout_constraintStart_toStartOf="parent" />
78
-
79
- </androidx.constraintlayout.widget.ConstraintLayout>
80
-
81
-
82
-
83
- ```
84
-
85
-
86
-
87
-
88
-
89
- #設定している Activity_Main
90
-
91
-
92
-
93
- ```ここに言語を入力
94
-
95
- <?xml version="1.0" encoding="utf-8"?>
96
-
97
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
98
-
99
- xmlns:app="http://schemas.android.com/apk/res-auto"
100
-
101
- xmlns:tools="http://schemas.android.com/tools"
102
-
103
- android:id="@+id/container"
104
-
105
- android:layout_width="match_parent"
106
-
107
- android:layout_height="match_parent"
108
-
109
- tools:context=".MainActivity">
110
-
111
-
112
-
113
- <WebView
114
-
115
- android:id="@+id/webview"
116
-
117
- android:layout_width="match_parent"
118
-
119
- android:layout_height="match_parent"
120
-
121
- />
122
-
123
-
124
-
125
- <com.google.android.material.bottomnavigation.BottomNavigationView
126
-
127
- android:id="@+id/nav_view"
128
-
129
- android:layout_width="0dp"
130
-
131
- android:layout_height="wrap_content"
132
-
133
- android:layout_marginStart="0dp"
134
-
135
- android:layout_marginEnd="0dp"
136
-
137
- android:background="?android:attr/windowBackground"
138
-
139
- app:layout_constraintBottom_toBottomOf="parent"
140
-
141
- app:layout_constraintLeft_toLeftOf="parent"
142
-
143
- app:layout_constraintRight_toRightOf="parent"
144
-
145
- app:menu="@menu/bottom_nav_menu" />
146
-
147
-
148
-
149
- <fragment
150
-
151
- android:id="@+id/nav_host_fragment"
152
-
153
- android:name="androidx.navigation.fragment.NavHostFragment"
154
-
155
- android:layout_width="match_parent"
156
-
157
- android:layout_height="match_parent"
158
-
159
- app:defaultNavHost="true"
160
-
161
- app:layout_constraintBottom_toTopOf="@id/nav_view"
162
-
163
- app:layout_constraintLeft_toLeftOf="parent"
164
-
165
- app:layout_constraintRight_toRightOf="parent"
166
-
167
- app:layout_constraintTop_toTopOf="parent"
168
-
169
- app:navGraph="@navigation/mobile_navigation" />
170
-
171
-
172
-
173
- <TextureView
174
-
175
- android:id="@+id/view_finder"
176
-
177
- android:layout_width="640px"
178
-
179
- android:layout_height="640px"
180
-
181
- app:layout_constraintTop_toTopOf="parent"
182
-
183
- app:layout_constraintBottom_toBottomOf="parent"
184
-
185
- app:layout_constraintStart_toStartOf="parent"
186
-
187
- app:layout_constraintEnd_toEndOf="parent" />
188
-
189
-
190
-
191
- </androidx.constraintlayout.widget.ConstraintLayout>
192
-
193
- ```
194
-
195
-
196
-
197
- #MainActivity
198
-
199
-
200
-
201
- ```ここに言語を入力
202
-
203
- package com.hoge.hogeapprication
204
-
205
-
206
-
207
- //CameraXに必要となるインポートです
208
-
209
- import android.Manifest
210
-
211
- import android.content.pm.PackageManager
212
-
213
- import android.graphics.Matrix
214
-
215
- import android.os.Bundle
216
-
217
- import android.util.Log
218
-
219
- import android.util.Size
220
-
221
- import android.view.Surface
222
-
223
- import android.view.TextureView
224
-
225
- import android.view.ViewGroup
226
-
227
- import android.widget.ImageButton
228
-
229
- import android.widget.Toast
230
-
231
- import androidx.appcompat.app.AppCompatActivity
232
-
233
- import androidx.camera.core.*
234
-
235
- import androidx.core.app.ActivityCompat
236
-
237
- import androidx.core.content.ContextCompat
238
-
239
- import java.io.File
240
-
241
- import java.util.concurrent.Executors
242
-
243
-
244
-
245
- private const val REQUEST_CODE_PERMISSIONS = 10
246
-
247
- private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE)
248
-
249
-
250
-
251
- class MainActivity : AppCompatActivity() {
252
-
253
- override fun onCreate(savedInstanceState: Bundle?) {
254
-
255
- super.onCreate(savedInstanceState)
256
-
257
- setContentView(R.layout.activity_main)
258
-
259
-
260
-
261
- viewFinder = findViewById(R.id.view_finder)
262
-
263
-
264
-
265
- if (allPermissionsGranted()) {
266
-
267
- viewFinder.post { startCamera() }
268
-
269
- } else {
270
-
271
- ActivityCompat.requestPermissions(
272
-
273
- this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
274
-
275
- }
276
-
277
- }
278
-
279
- //カメラ撮影時に利用する単一のスレッドを取得し再利用できるようにexecutorを作成します。
280
-
281
- private val executor = Executors.newSingleThreadExecutor()
282
-
283
-
284
-
285
- //後に利用するTextureViewをここで定義します。
286
-
287
- private lateinit var viewFinder: TextureView
288
-
289
-
290
-
291
- private fun startCamera() {
292
-
293
- val previewConfig = PreviewConfig.Builder().apply {
294
-
295
- setTargetResolution(Size(640, 480))
296
-
297
- }.build()
298
-
299
-
300
-
301
- val preview = Preview(previewConfig)
302
-
303
-
304
-
305
- preview.setOnPreviewOutputUpdateListener {
306
-
307
-
308
-
309
- val parent = viewFinder.parent as ViewGroup
310
-
311
-
312
-
313
- parent.removeView(viewFinder)
314
-
315
- parent.addView(viewFinder, 0)
316
-
317
-
318
-
319
- viewFinder.surfaceTexture = it.surfaceTexture
320
-
321
-
322
-
323
- updateTransform()
324
-
325
- }
326
-
327
- val imageCaptureConfig = ImageCaptureConfig.Builder()
328
-
329
- .apply {
330
-
331
- setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
332
-
333
- }.build()
334
-
335
-
336
-
337
- val imageCapture = ImageCapture(imageCaptureConfig)
338
-
339
-
340
-
341
- findViewById(R.id.capture_button).setOnClickListener {
342
-
343
- val file = File(externalMediaDirs.first(),
344
-
345
- "${System.currentTimeMillis()}.jpg")
346
-
347
-
348
-
349
- imageCapture.takePicture(file, executor,
350
-
351
- object : ImageCapture.OnImageSavedListener {
352
-
353
-
354
-
355
- override fun onError(
356
-
357
- imageCaptureError: ImageCapture.ImageCaptureError,
358
-
359
- message: String,
360
-
361
- exc: Throwable?
362
-
363
- ) {
364
-
365
- val msg = "Photo capture failed: $message"
366
-
367
- Log.e("CameraXApp", msg, exc)
368
-
369
- viewFinder.post {
370
-
371
- Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
372
-
373
- }
374
-
375
- }
376
-
377
-
378
-
379
- override fun onImageSaved(file: File) {
380
-
381
- val msg = "Photo capture succeeded: ${file.absolutePath}"
382
-
383
- Log.d("CameraXApp", msg)
384
-
385
- viewFinder.post {
386
-
387
- Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
388
-
389
- }
390
-
391
- }
392
-
393
- })
394
-
395
- }
396
-
397
-
398
-
399
- CameraX.bindToLifecycle(this, preview, imageCapture)
400
-
401
- }
402
-
403
-
404
-
405
- private fun updateTransform() {
406
-
407
- val matrix = Matrix()
408
-
409
-
410
-
411
- val centerX = viewFinder.width / 2f
412
-
413
- val centerY = viewFinder.height / 2f
414
-
415
-
416
-
417
- val rotationDegrees = when(viewFinder.display.rotation) {
418
-
419
- Surface.ROTATION_0 -> 0
420
-
421
- Surface.ROTATION_90 -> 90
422
-
423
- Surface.ROTATION_180 -> 180
424
-
425
- Surface.ROTATION_270 -> 270
426
-
427
- else -> return
428
-
429
- }
430
-
431
-
432
-
433
- matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)
434
-
435
-
436
-
437
- viewFinder.setTransform(matrix)
438
-
439
- }
440
-
441
-
442
-
443
- override fun onRequestPermissionsResult(
444
-
445
- requestCode: Int, permissions: Array, grantResults: IntArray) {
446
-
447
- if (requestCode == REQUEST_CODE_PERMISSIONS) {
448
-
449
- if (allPermissionsGranted()) {
450
-
451
- viewFinder.post { startCamera() }
452
-
453
- } else {
454
-
455
- Toast.makeText(this,
456
-
457
- "Permissions not granted by the user.",
458
-
459
- Toast.LENGTH_SHORT).show()
460
-
461
- finish()
462
-
463
- }
464
-
465
- }
466
-
467
- }
468
-
469
- //マニフェストで指定されたすべての権限が付与されているかどうかを確認します
470
-
471
- private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
472
-
473
- ContextCompat.checkSelfPermission(
474
-
475
- baseContext, it) == PackageManager.PERMISSION_GRANTED
476
-
477
- }
478
-
479
- }
480
-
481
- ```
482
-
483
-
484
-
485
- 恐れ入りますが宜しくお願いいたします。参考にしたサイトは以下になります。
486
-
487
-
488
-
489
- https://codelabs.developers.google.com/codelabs/camerax-getting-started/index.html?index=..%2F..index#5