質問編集履歴
7
変更した画像の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -118,6 +118,8 @@
|
|
118
118
|
|
119
119
|
|
120
120
|
|
121
|
+
![イメージ説明](c0a9e093134feadd0e80988183f33b0d.png)
|
122
|
+
|
121
123
|
|
122
124
|
|
123
125
|
どうぞよろしくお願いします。
|
6
説明の追加
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
5
説明の詳細
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
androidで、Serviceで、Camera2APIで動画を撮影
|
1
|
+
for文に expected ';' が出ます。androidで、Serviceで、Camera2APIで動画を撮影したいです。
|
test
CHANGED
@@ -1,416 +1,8 @@
|
|
1
|
-
|
1
|
+
for文に expected ';' が出ます。どうすればいいのでしょうか?
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
丸投げの質問と言われるかと思いますので、
|
6
|
-
|
7
|
-
URLだけで大丈夫です。どうぞよろしくお願いします。
|
8
|
-
|
9
|
-
24時間、探し続けたのですが、見つからず、Fragmentを使ってるCamera2APIで動画を撮影しているコードをServiceように書き換えているのですが、エラーが大量に出てとても大変な思いをしています。
|
10
|
-
|
11
|
-
[参考にしているgithubに繋がるURL](http://devdroid123.blog.fc2.com/blog-entry-22.html)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
[CameraAPIだが、Serviceなので、参考にしているURL](https://stackoverflow.com/questions/10121660/how-to-record-video-from-background-of-application-android)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
上記2つのURLを組み合わせてアプリを作成しています。
|
20
|
-
|
21
|
-
|
22
2
|
|
23
3
|
```java
|
24
4
|
|
25
|
-
public class Camera2MovieService extends Service {
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
|
32
|
-
|
33
|
-
private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
|
34
|
-
|
35
|
-
private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
|
36
|
-
|
37
|
-
private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
private static final String TAG = "Camera2VideoFragment";
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
static {
|
48
|
-
|
49
|
-
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
|
50
|
-
|
51
|
-
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
|
52
|
-
|
53
|
-
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
|
54
|
-
|
55
|
-
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
|
56
|
-
|
57
|
-
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
static {
|
62
|
-
|
63
|
-
INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
|
64
|
-
|
65
|
-
INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
|
66
|
-
|
67
|
-
INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
|
68
|
-
|
69
|
-
INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
private AutoFitTextureView mTextureView;
|
78
|
-
|
79
|
-
private CameraDevice mCameraDevice;
|
80
|
-
|
81
|
-
private CameraCaptureSession mPreviewSession;
|
82
|
-
|
83
|
-
private TextureView.SurfaceTextureListener mSurfaceTextureListener
|
84
|
-
|
85
|
-
= new TextureView.SurfaceTextureListener() {
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
@Override
|
90
|
-
|
91
|
-
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
|
92
|
-
|
93
|
-
int width, int height) {
|
94
|
-
|
95
|
-
openCamera(width, height);
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
@Override
|
102
|
-
|
103
|
-
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
|
104
|
-
|
105
|
-
int width, int height) {
|
106
|
-
|
107
|
-
configureTransform(width, height);
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
@Override
|
114
|
-
|
115
|
-
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
|
116
|
-
|
117
|
-
return true;
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
@Override
|
124
|
-
|
125
|
-
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
};
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
private Size mPreviewSize;
|
136
|
-
|
137
|
-
private Size mVideoSize;
|
138
|
-
|
139
|
-
private MediaRecorder mMediaRecorder;
|
140
|
-
|
141
|
-
private boolean mIsRecordingVideo;
|
142
|
-
|
143
|
-
private HandlerThread mBackgroundThread;
|
144
|
-
|
145
|
-
private Handler mBackgroundHandler;
|
146
|
-
|
147
|
-
private Semaphore mCameraOpenCloseLock = new Semaphore(1);
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
//{@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its status.
|
152
|
-
|
153
|
-
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
@Override
|
158
|
-
|
159
|
-
public void onOpened(@NonNull CameraDevice cameraDevice) {
|
160
|
-
|
161
|
-
mCameraDevice = cameraDevice;
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
startPreview();
|
166
|
-
|
167
|
-
mCameraOpenCloseLock.release();
|
168
|
-
|
169
|
-
if (null != mTextureView) {
|
170
|
-
|
171
|
-
configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
@Override
|
180
|
-
|
181
|
-
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
|
182
|
-
|
183
|
-
mCameraOpenCloseLock.release();
|
184
|
-
|
185
|
-
cameraDevice.close();
|
186
|
-
|
187
|
-
mCameraDevice = null;
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
@Override
|
194
|
-
|
195
|
-
public void onError(@NonNull CameraDevice cameraDevice, int error) {
|
196
|
-
|
197
|
-
mCameraOpenCloseLock.release();
|
198
|
-
|
199
|
-
cameraDevice.close();
|
200
|
-
|
201
|
-
mCameraDevice = null;
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
};
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
private Integer mSensorOrientation;
|
214
|
-
|
215
|
-
private String mNextVideoAbsolutePath;
|
216
|
-
|
217
|
-
private CaptureRequest.Builder mPreviewBuilder;
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
public static com.example.android.camera2video.Camera2MovieService newInstance() {
|
222
|
-
|
223
|
-
return new com.example.android.camera2video.Camera2MovieService();
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
public Camera2MovieService() {
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
private static Size chooseVideoSize(Size[] choices) {
|
240
|
-
|
241
|
-
for (Size size : choices) {
|
242
|
-
|
243
|
-
if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
|
244
|
-
|
245
|
-
return size;
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
Log.e(TAG, "Couldn't find any suitable video size");
|
252
|
-
|
253
|
-
return choices[choices.length - 1];
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
|
262
|
-
|
263
|
-
List<Size> bigEnough = new ArrayList<>();
|
264
|
-
|
265
|
-
int w = aspectRatio.getWidth();
|
266
|
-
|
267
|
-
int h = aspectRatio.getHeight();
|
268
|
-
|
269
|
-
for (Size option : choices) {
|
270
|
-
|
271
|
-
if (option.getHeight() == option.getWidth() * h / w &&
|
272
|
-
|
273
|
-
option.getWidth() >= width && option.getHeight() >= height) {
|
274
|
-
|
275
|
-
bigEnough.add(option);
|
276
|
-
|
277
|
-
}
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
if (bigEnough.size() > 0) {
|
282
|
-
|
283
|
-
return Collections.min(bigEnough, new com.example.android.camera2video.Camera2MovieService().CompareSizesByArea());
|
284
|
-
|
285
|
-
} else {
|
286
|
-
|
287
|
-
Log.e(TAG, "Couldn't find any suitable preview size");
|
288
|
-
|
289
|
-
return choices[0];
|
290
|
-
|
291
|
-
}
|
292
|
-
|
293
|
-
}
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
@Override
|
300
|
-
|
301
|
-
public IBinder onBind(Intent intent) {
|
302
|
-
|
303
|
-
return null;
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
public void Resume() {
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
//BackgroundThredにする意味が?
|
316
|
-
|
317
|
-
startBackgroundThread();
|
318
|
-
|
319
|
-
if (mTextureView.isAvailable()) {
|
320
|
-
|
321
|
-
// openCamera(mTextureView.getWidth(), mTextureView.getHeight());
|
322
|
-
|
323
|
-
//permissionは確認済み
|
324
|
-
|
325
|
-
} else {
|
326
|
-
|
327
|
-
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
|
328
|
-
|
329
|
-
}
|
330
|
-
|
331
|
-
}
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
//close処理
|
338
|
-
|
339
|
-
public void Pause() {
|
340
|
-
|
341
|
-
closeCamera();
|
342
|
-
|
343
|
-
stopBackgroundThread();
|
344
|
-
|
345
|
-
}
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
//Clickはするものがないからいらない、代わりに、自動で呼び出されるようにしておく
|
350
|
-
|
351
|
-
public void Click(View view) {
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
// switch (view.getId()) {
|
356
|
-
|
357
|
-
//この下は、intentのExtra()で判断する
|
358
|
-
|
359
|
-
// case R.id.video: {
|
360
|
-
|
361
|
-
if (mIsRecordingVideo) {
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
//上の変数はboolean だから、
|
366
|
-
|
367
|
-
//trueだったら、videoを止める
|
368
|
-
|
369
|
-
stopRecordingVideo();
|
370
|
-
|
371
|
-
} else {
|
372
|
-
|
373
|
-
startRecordingVideo();
|
374
|
-
|
375
|
-
}
|
376
|
-
|
377
|
-
}
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
private void startBackgroundThread() {
|
382
|
-
|
383
|
-
mBackgroundThread = new HandlerThread("CameraBackground");
|
384
|
-
|
385
|
-
mBackgroundThread.start();
|
386
|
-
|
387
|
-
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
|
388
|
-
|
389
|
-
}
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
private void stopBackgroundThread() {
|
394
|
-
|
395
|
-
mBackgroundThread.quitSafely();
|
396
|
-
|
397
|
-
try {
|
398
|
-
|
399
|
-
mBackgroundThread.join();
|
400
|
-
|
401
|
-
mBackgroundThread = null;
|
402
|
-
|
403
|
-
mBackgroundHandler = null;
|
404
|
-
|
405
|
-
} catch (InterruptedException e) {
|
406
|
-
|
407
|
-
e.printStackTrace();
|
408
|
-
|
409
|
-
}
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
@SuppressWarnings("MissingPermission")
|
5
|
+
@SuppressWarnings("MissingPermission")
|
414
6
|
|
415
7
|
private void openCamera(int width, int height) {
|
416
8
|
|
@@ -434,9 +26,9 @@
|
|
434
26
|
|
435
27
|
String backCameraId;
|
436
28
|
|
437
|
-
|
29
|
+
//この下の())の部分に ' ; ' expected が出ます。
|
438
30
|
|
439
|
-
|
31
|
+
for(cameraId:manager.getCameraIdList()){
|
440
32
|
|
441
33
|
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
|
442
34
|
|
@@ -512,11 +104,21 @@
|
|
512
104
|
|
513
105
|
}
|
514
106
|
|
107
|
+
```
|
108
|
+
|
109
|
+
[参考にしているgithubに繋がるURL](http://devdroid123.blog.fc2.com/blog-entry-22.html)
|
515
110
|
|
516
111
|
|
517
|
-
|
518
112
|
|
113
|
+
[CameraAPIだが、Serviceなので、参考にしているURL](https://stackoverflow.com/questions/10121660/how-to-record-video-from-background-of-application-android)
|
114
|
+
|
115
|
+
|
116
|
+
|
519
|
-
|
117
|
+
上記2つのURLを組み合わせてアプリを作成しています。
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
520
122
|
|
521
123
|
どうぞよろしくお願いします。
|
522
124
|
|
4
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -410,9 +410,111 @@
|
|
410
410
|
|
411
411
|
}
|
412
412
|
|
413
|
-
|
413
|
+
@SuppressWarnings("MissingPermission")
|
414
|
+
|
414
|
-
|
415
|
+
private void openCamera(int width, int height) {
|
416
|
+
|
417
|
+
|
418
|
+
|
415
|
-
|
419
|
+
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
try {
|
424
|
+
|
425
|
+
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
|
426
|
+
|
427
|
+
throw new RuntimeException("Time out waiting to lock camera opening.");
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
String cameraId = manager.getCameraIdList()[0];
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
String backCameraId;
|
436
|
+
|
437
|
+
for (cameraId:
|
438
|
+
|
439
|
+
manager.getCameraIdList()) {
|
440
|
+
|
441
|
+
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
|
442
|
+
|
443
|
+
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
if (facing != null && facing ==
|
448
|
+
|
449
|
+
CameraCharacteristics.LENS_FACING_FRONT) {
|
450
|
+
|
451
|
+
//インカメラだったら
|
452
|
+
|
453
|
+
backCameraId = cameraId;
|
454
|
+
|
455
|
+
}
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
// Choose the sizes for camera preview and video recording
|
460
|
+
|
461
|
+
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
|
462
|
+
|
463
|
+
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
|
464
|
+
|
465
|
+
mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
if (map == null) {
|
470
|
+
|
471
|
+
throw new RuntimeException("Cannot get available preview/video sizes");
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
|
476
|
+
|
477
|
+
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, mVideoSize);
|
478
|
+
|
479
|
+
int orientation = getResources().getConfiguration().orientation;
|
480
|
+
|
481
|
+
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
482
|
+
|
483
|
+
mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
|
484
|
+
|
485
|
+
} else {
|
486
|
+
|
487
|
+
mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
configureTransform(width, height);
|
492
|
+
|
493
|
+
mMediaRecorder = new MediaRecorder();
|
494
|
+
|
495
|
+
manager.openCamera(cameraId, mStateCallback, null);
|
496
|
+
|
497
|
+
} catch (CameraAccessException e) {
|
498
|
+
|
499
|
+
} catch (NullPointerException e) {
|
500
|
+
|
501
|
+
//camera2 API のバージョンはサポートしてるからいい
|
502
|
+
|
503
|
+
//ErrorDialog.newInstance(getString(R.string.camera_error))
|
504
|
+
|
505
|
+
// .show(getChildFragmentManager(), FRAGMENT_DIALOG);
|
506
|
+
|
507
|
+
} catch (InterruptedException e) {
|
508
|
+
|
509
|
+
throw new RuntimeException("Interrupted while trying to lock camera opening.");
|
510
|
+
|
511
|
+
}
|
512
|
+
|
513
|
+
}
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
|
416
518
|
|
417
519
|
```
|
418
520
|
|
3
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -414,8 +414,6 @@
|
|
414
414
|
|
415
415
|
|
416
416
|
|
417
|
-
|
418
|
-
|
419
417
|
```
|
420
418
|
|
421
419
|
どうぞよろしくお願いします。
|
2
コードの追加 上半分
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,6 +20,404 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
+
```java
|
24
|
+
|
25
|
+
public class Camera2MovieService extends Service {
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
|
32
|
+
|
33
|
+
private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
|
34
|
+
|
35
|
+
private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
|
36
|
+
|
37
|
+
private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
private static final String TAG = "Camera2VideoFragment";
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
static {
|
48
|
+
|
49
|
+
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
|
50
|
+
|
51
|
+
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
|
52
|
+
|
53
|
+
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
|
54
|
+
|
55
|
+
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
static {
|
62
|
+
|
63
|
+
INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
|
64
|
+
|
65
|
+
INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
|
66
|
+
|
67
|
+
INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
|
68
|
+
|
69
|
+
INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
private AutoFitTextureView mTextureView;
|
78
|
+
|
79
|
+
private CameraDevice mCameraDevice;
|
80
|
+
|
81
|
+
private CameraCaptureSession mPreviewSession;
|
82
|
+
|
83
|
+
private TextureView.SurfaceTextureListener mSurfaceTextureListener
|
84
|
+
|
85
|
+
= new TextureView.SurfaceTextureListener() {
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
@Override
|
90
|
+
|
91
|
+
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
|
92
|
+
|
93
|
+
int width, int height) {
|
94
|
+
|
95
|
+
openCamera(width, height);
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
@Override
|
102
|
+
|
103
|
+
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
|
104
|
+
|
105
|
+
int width, int height) {
|
106
|
+
|
107
|
+
configureTransform(width, height);
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
@Override
|
114
|
+
|
115
|
+
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
|
116
|
+
|
117
|
+
return true;
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
@Override
|
124
|
+
|
125
|
+
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
};
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
private Size mPreviewSize;
|
136
|
+
|
137
|
+
private Size mVideoSize;
|
138
|
+
|
139
|
+
private MediaRecorder mMediaRecorder;
|
140
|
+
|
141
|
+
private boolean mIsRecordingVideo;
|
142
|
+
|
143
|
+
private HandlerThread mBackgroundThread;
|
144
|
+
|
145
|
+
private Handler mBackgroundHandler;
|
146
|
+
|
147
|
+
private Semaphore mCameraOpenCloseLock = new Semaphore(1);
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
//{@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its status.
|
152
|
+
|
153
|
+
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
@Override
|
158
|
+
|
159
|
+
public void onOpened(@NonNull CameraDevice cameraDevice) {
|
160
|
+
|
161
|
+
mCameraDevice = cameraDevice;
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
startPreview();
|
166
|
+
|
167
|
+
mCameraOpenCloseLock.release();
|
168
|
+
|
169
|
+
if (null != mTextureView) {
|
170
|
+
|
171
|
+
configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
@Override
|
180
|
+
|
181
|
+
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
|
182
|
+
|
183
|
+
mCameraOpenCloseLock.release();
|
184
|
+
|
185
|
+
cameraDevice.close();
|
186
|
+
|
187
|
+
mCameraDevice = null;
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
@Override
|
194
|
+
|
195
|
+
public void onError(@NonNull CameraDevice cameraDevice, int error) {
|
196
|
+
|
197
|
+
mCameraOpenCloseLock.release();
|
198
|
+
|
199
|
+
cameraDevice.close();
|
200
|
+
|
201
|
+
mCameraDevice = null;
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
};
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
private Integer mSensorOrientation;
|
214
|
+
|
215
|
+
private String mNextVideoAbsolutePath;
|
216
|
+
|
217
|
+
private CaptureRequest.Builder mPreviewBuilder;
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
public static com.example.android.camera2video.Camera2MovieService newInstance() {
|
222
|
+
|
223
|
+
return new com.example.android.camera2video.Camera2MovieService();
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
public Camera2MovieService() {
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
private static Size chooseVideoSize(Size[] choices) {
|
240
|
+
|
241
|
+
for (Size size : choices) {
|
242
|
+
|
243
|
+
if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
|
244
|
+
|
245
|
+
return size;
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
Log.e(TAG, "Couldn't find any suitable video size");
|
252
|
+
|
253
|
+
return choices[choices.length - 1];
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
|
262
|
+
|
263
|
+
List<Size> bigEnough = new ArrayList<>();
|
264
|
+
|
265
|
+
int w = aspectRatio.getWidth();
|
266
|
+
|
267
|
+
int h = aspectRatio.getHeight();
|
268
|
+
|
269
|
+
for (Size option : choices) {
|
270
|
+
|
271
|
+
if (option.getHeight() == option.getWidth() * h / w &&
|
272
|
+
|
273
|
+
option.getWidth() >= width && option.getHeight() >= height) {
|
274
|
+
|
275
|
+
bigEnough.add(option);
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
if (bigEnough.size() > 0) {
|
282
|
+
|
283
|
+
return Collections.min(bigEnough, new com.example.android.camera2video.Camera2MovieService().CompareSizesByArea());
|
284
|
+
|
285
|
+
} else {
|
286
|
+
|
287
|
+
Log.e(TAG, "Couldn't find any suitable preview size");
|
288
|
+
|
289
|
+
return choices[0];
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
@Override
|
300
|
+
|
301
|
+
public IBinder onBind(Intent intent) {
|
302
|
+
|
303
|
+
return null;
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
public void Resume() {
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
//BackgroundThredにする意味が?
|
316
|
+
|
317
|
+
startBackgroundThread();
|
318
|
+
|
319
|
+
if (mTextureView.isAvailable()) {
|
320
|
+
|
321
|
+
// openCamera(mTextureView.getWidth(), mTextureView.getHeight());
|
322
|
+
|
323
|
+
//permissionは確認済み
|
324
|
+
|
325
|
+
} else {
|
326
|
+
|
327
|
+
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
//close処理
|
338
|
+
|
339
|
+
public void Pause() {
|
340
|
+
|
341
|
+
closeCamera();
|
342
|
+
|
343
|
+
stopBackgroundThread();
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
//Clickはするものがないからいらない、代わりに、自動で呼び出されるようにしておく
|
350
|
+
|
351
|
+
public void Click(View view) {
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
// switch (view.getId()) {
|
356
|
+
|
357
|
+
//この下は、intentのExtra()で判断する
|
358
|
+
|
359
|
+
// case R.id.video: {
|
360
|
+
|
361
|
+
if (mIsRecordingVideo) {
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
//上の変数はboolean だから、
|
366
|
+
|
367
|
+
//trueだったら、videoを止める
|
368
|
+
|
369
|
+
stopRecordingVideo();
|
370
|
+
|
371
|
+
} else {
|
372
|
+
|
373
|
+
startRecordingVideo();
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
private void startBackgroundThread() {
|
382
|
+
|
383
|
+
mBackgroundThread = new HandlerThread("CameraBackground");
|
384
|
+
|
385
|
+
mBackgroundThread.start();
|
386
|
+
|
387
|
+
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
private void stopBackgroundThread() {
|
394
|
+
|
395
|
+
mBackgroundThread.quitSafely();
|
396
|
+
|
397
|
+
try {
|
398
|
+
|
399
|
+
mBackgroundThread.join();
|
400
|
+
|
401
|
+
mBackgroundThread = null;
|
402
|
+
|
403
|
+
mBackgroundHandler = null;
|
404
|
+
|
405
|
+
} catch (InterruptedException e) {
|
406
|
+
|
407
|
+
e.printStackTrace();
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
```
|
420
|
+
|
23
421
|
どうぞよろしくお願いします。
|
24
422
|
|
25
423
|
![イメージ説明](f9d6c106bcea16798e5136757c9fa040.png)
|
1
写真の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -21,3 +21,5 @@
|
|
21
21
|
|
22
22
|
|
23
23
|
どうぞよろしくお願いします。
|
24
|
+
|
25
|
+
![イメージ説明](f9d6c106bcea16798e5136757c9fa040.png)
|