teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

7

変更した画像の追加

2017/08/03 07:42

投稿

giant
giant

スコア132

title CHANGED
File without changes
body CHANGED
@@ -58,6 +58,7 @@
58
58
 
59
59
  上記2つのURLを組み合わせてアプリを作成しています。
60
60
 
61
+ ![イメージ説明](c0a9e093134feadd0e80988183f33b0d.png)
61
62
 
62
63
  どうぞよろしくお願いします。
63
64
  ![イメージ説明](f9d6c106bcea16798e5136757c9fa040.png)

6

説明の追加

2017/08/03 07:41

投稿

giant
giant

スコア132

title CHANGED
File without changes
body CHANGED
File without changes

5

説明の詳細

2017/08/03 07:16

投稿

giant
giant

スコア132

title CHANGED
@@ -1,1 +1,1 @@
1
- androidで、Serviceで、Camera2APIで動画を撮影できるサンプルコードを見つけられなですが、知りませんか?
1
+ for文に expected ';' が出ます。androidで、Serviceで、Camera2APIで動画を撮影したいです
body CHANGED
@@ -1,210 +1,6 @@
1
- androidで、Serviceで、Camera2APIで動画を撮影できるサンプルコードを見つけらいのですが、知りませんか?
1
+ for文に expected ';' が出ます。どうすのでしょうか?
2
-
3
- 丸投げの質問と言われるかと思いますので、
4
- URLだけで大丈夫です。どうぞよろしくお願いします。
5
- 24時間、探し続けたのですが、見つからず、Fragmentを使ってるCamera2APIで動画を撮影しているコードをServiceように書き換えているのですが、エラーが大量に出てとても大変な思いをしています。
6
- [参考にしているgithubに繋がるURL](http://devdroid123.blog.fc2.com/blog-entry-22.html)
7
-
8
- [CameraAPIだが、Serviceなので、参考にしているURL](https://stackoverflow.com/questions/10121660/how-to-record-video-from-background-of-application-android)
9
-
10
- 上記2つのURLを組み合わせてアプリを作成しています。
11
-
12
2
  ```java
13
- public class Camera2MovieService extends Service {
14
-
15
-
16
- private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
17
- private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
18
- private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
19
- private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
20
-
21
- private static final String TAG = "Camera2VideoFragment";
22
-
23
-
24
- static {
25
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
26
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
27
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
28
- DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
29
- }
30
-
31
- static {
32
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
33
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
34
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
35
- INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
36
- }
37
-
38
-
39
- private AutoFitTextureView mTextureView;
40
- private CameraDevice mCameraDevice;
41
- private CameraCaptureSession mPreviewSession;
42
- private TextureView.SurfaceTextureListener mSurfaceTextureListener
43
- = new TextureView.SurfaceTextureListener() {
44
-
45
- @Override
46
- public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
47
- int width, int height) {
48
- openCamera(width, height);
49
- }
50
-
51
- @Override
52
- public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
53
- int width, int height) {
54
- configureTransform(width, height);
55
- }
56
-
57
- @Override
58
- public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
59
- return true;
60
- }
61
-
62
- @Override
63
- public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
64
- }
65
-
66
- };
67
-
68
- private Size mPreviewSize;
69
- private Size mVideoSize;
70
- private MediaRecorder mMediaRecorder;
71
- private boolean mIsRecordingVideo;
72
- private HandlerThread mBackgroundThread;
73
- private Handler mBackgroundHandler;
74
- private Semaphore mCameraOpenCloseLock = new Semaphore(1);
75
-
76
- //{@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its status.
77
- private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
78
-
79
- @Override
80
- public void onOpened(@NonNull CameraDevice cameraDevice) {
81
- mCameraDevice = cameraDevice;
82
-
83
- startPreview();
84
- mCameraOpenCloseLock.release();
85
- if (null != mTextureView) {
86
- configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
87
- }
88
- }
89
-
90
- @Override
91
- public void onDisconnected(@NonNull CameraDevice cameraDevice) {
92
- mCameraOpenCloseLock.release();
93
- cameraDevice.close();
94
- mCameraDevice = null;
95
- }
96
-
97
- @Override
98
- public void onError(@NonNull CameraDevice cameraDevice, int error) {
99
- mCameraOpenCloseLock.release();
100
- cameraDevice.close();
101
- mCameraDevice = null;
102
- }
103
-
104
- };
105
-
106
-
107
- private Integer mSensorOrientation;
108
- private String mNextVideoAbsolutePath;
109
- private CaptureRequest.Builder mPreviewBuilder;
110
-
111
- public static com.example.android.camera2video.Camera2MovieService newInstance() {
112
- return new com.example.android.camera2video.Camera2MovieService();
113
- }
114
-
115
-
116
- public Camera2MovieService() {
117
- }
118
-
119
-
120
- private static Size chooseVideoSize(Size[] choices) {
121
- for (Size size : choices) {
122
- if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
123
- return size;
124
- }
125
- }
126
- Log.e(TAG, "Couldn't find any suitable video size");
127
- return choices[choices.length - 1];
128
- }
129
-
130
-
131
- private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
132
- List<Size> bigEnough = new ArrayList<>();
133
- int w = aspectRatio.getWidth();
134
- int h = aspectRatio.getHeight();
135
- for (Size option : choices) {
136
- if (option.getHeight() == option.getWidth() * h / w &&
137
- option.getWidth() >= width && option.getHeight() >= height) {
138
- bigEnough.add(option);
139
- }
140
- }
141
- if (bigEnough.size() > 0) {
142
- return Collections.min(bigEnough, new com.example.android.camera2video.Camera2MovieService().CompareSizesByArea());
143
- } else {
144
- Log.e(TAG, "Couldn't find any suitable preview size");
145
- return choices[0];
146
- }
147
- }
148
-
149
-
150
- @Override
151
- public IBinder onBind(Intent intent) {
152
- return null;
153
- }
154
-
155
-
156
- public void Resume() {
157
-
158
- //BackgroundThredにする意味が?
159
- startBackgroundThread();
160
- if (mTextureView.isAvailable()) {
161
- // openCamera(mTextureView.getWidth(), mTextureView.getHeight());
162
- //permissionは確認済み
163
- } else {
164
- mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
165
- }
166
- }
167
-
168
-
169
- //close処理
170
- public void Pause() {
171
- closeCamera();
172
- stopBackgroundThread();
173
- }
174
-
175
- //Clickはするものがないからいらない、代わりに、自動で呼び出されるようにしておく
176
- public void Click(View view) {
177
-
178
- // switch (view.getId()) {
179
- //この下は、intentのExtra()で判断する
180
- // case R.id.video: {
181
- if (mIsRecordingVideo) {
182
-
183
- //上の変数はboolean だから、
184
- //trueだったら、videoを止める
185
- stopRecordingVideo();
186
- } else {
187
- startRecordingVideo();
188
- }
189
- }
190
-
191
- private void startBackgroundThread() {
192
- mBackgroundThread = new HandlerThread("CameraBackground");
193
- mBackgroundThread.start();
194
- mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
195
- }
196
-
197
- private void stopBackgroundThread() {
198
- mBackgroundThread.quitSafely();
199
- try {
200
- mBackgroundThread.join();
201
- mBackgroundThread = null;
202
- mBackgroundHandler = null;
203
- } catch (InterruptedException e) {
204
- e.printStackTrace();
205
- }
206
- }
207
- @SuppressWarnings("MissingPermission")
3
+ @SuppressWarnings("MissingPermission")
208
4
  private void openCamera(int width, int height) {
209
5
 
210
6
  CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
@@ -216,8 +12,8 @@
216
12
  String cameraId = manager.getCameraIdList()[0];
217
13
 
218
14
  String backCameraId;
219
- for (cameraId:
15
+ //この下の())の部分に ' ; ' expected が出ます。
220
- manager.getCameraIdList()) {
16
+ for(cameraId:manager.getCameraIdList()){
221
17
  CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
222
18
  Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
223
19
 
@@ -255,8 +51,13 @@
255
51
  throw new RuntimeException("Interrupted while trying to lock camera opening.");
256
52
  }
257
53
  }
54
+ ```
55
+ [参考にしているgithubに繋がるURL](http://devdroid123.blog.fc2.com/blog-entry-22.html)
258
56
 
259
-
57
+ [CameraAPIだが、Serviceなので、参考にしているURL](https://stackoverflow.com/questions/10121660/how-to-record-video-from-background-of-application-android)
58
+
260
- ```
59
+ 上記2つのURLを組み合わせてアプリを作成しています。
60
+
61
+
261
62
  どうぞよろしくお願いします。
262
63
  ![イメージ説明](f9d6c106bcea16798e5136757c9fa040.png)

4

コードの追加

2017/08/03 07:08

投稿

giant
giant

スコア132

title CHANGED
File without changes
body CHANGED
@@ -204,8 +204,59 @@
204
204
  e.printStackTrace();
205
205
  }
206
206
  }
207
+ @SuppressWarnings("MissingPermission")
208
+ private void openCamera(int width, int height) {
207
209
 
210
+ CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
208
211
 
212
+ try {
213
+ if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
214
+ throw new RuntimeException("Time out waiting to lock camera opening.");
215
+ }
216
+ String cameraId = manager.getCameraIdList()[0];
217
+
218
+ String backCameraId;
219
+ for (cameraId:
220
+ manager.getCameraIdList()) {
221
+ CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
222
+ Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
223
+
224
+ if (facing != null && facing ==
225
+ CameraCharacteristics.LENS_FACING_FRONT) {
226
+ //インカメラだったら
227
+ backCameraId = cameraId;
228
+ }
229
+ }
230
+ // Choose the sizes for camera preview and video recording
231
+ CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
232
+ StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
233
+ mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
234
+
235
+ if (map == null) {
236
+ throw new RuntimeException("Cannot get available preview/video sizes");
237
+ }
238
+ mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
239
+ mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, mVideoSize);
240
+ int orientation = getResources().getConfiguration().orientation;
241
+ if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
242
+ mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
243
+ } else {
244
+ mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
245
+ }
246
+ configureTransform(width, height);
247
+ mMediaRecorder = new MediaRecorder();
248
+ manager.openCamera(cameraId, mStateCallback, null);
249
+ } catch (CameraAccessException e) {
250
+ } catch (NullPointerException e) {
251
+ //camera2 API のバージョンはサポートしてるからいい
252
+ //ErrorDialog.newInstance(getString(R.string.camera_error))
253
+ // .show(getChildFragmentManager(), FRAGMENT_DIALOG);
254
+ } catch (InterruptedException e) {
255
+ throw new RuntimeException("Interrupted while trying to lock camera opening.");
256
+ }
257
+ }
258
+
259
+
209
260
  ```
210
261
  どうぞよろしくお願いします。
211
262
  ![イメージ説明](f9d6c106bcea16798e5136757c9fa040.png)

3

コードの追加

2017/08/03 06:17

投稿

giant
giant

スコア132

title CHANGED
File without changes
body CHANGED
@@ -206,7 +206,6 @@
206
206
  }
207
207
 
208
208
 
209
-
210
209
  ```
211
210
  どうぞよろしくお願いします。
212
211
  ![イメージ説明](f9d6c106bcea16798e5136757c9fa040.png)

2

コードの追加 上半分

2017/08/03 06:12

投稿

giant
giant

スコア132

title CHANGED
File without changes
body CHANGED
@@ -9,5 +9,204 @@
9
9
 
10
10
  上記2つのURLを組み合わせてアプリを作成しています。
11
11
 
12
+ ```java
13
+ public class Camera2MovieService extends Service {
14
+
15
+
16
+ private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
17
+ private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
18
+ private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
19
+ private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
20
+
21
+ private static final String TAG = "Camera2VideoFragment";
22
+
23
+
24
+ static {
25
+ DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
26
+ DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
27
+ DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
28
+ DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
29
+ }
30
+
31
+ static {
32
+ INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
33
+ INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
34
+ INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
35
+ INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
36
+ }
37
+
38
+
39
+ private AutoFitTextureView mTextureView;
40
+ private CameraDevice mCameraDevice;
41
+ private CameraCaptureSession mPreviewSession;
42
+ private TextureView.SurfaceTextureListener mSurfaceTextureListener
43
+ = new TextureView.SurfaceTextureListener() {
44
+
45
+ @Override
46
+ public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
47
+ int width, int height) {
48
+ openCamera(width, height);
49
+ }
50
+
51
+ @Override
52
+ public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
53
+ int width, int height) {
54
+ configureTransform(width, height);
55
+ }
56
+
57
+ @Override
58
+ public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
59
+ return true;
60
+ }
61
+
62
+ @Override
63
+ public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
64
+ }
65
+
66
+ };
67
+
68
+ private Size mPreviewSize;
69
+ private Size mVideoSize;
70
+ private MediaRecorder mMediaRecorder;
71
+ private boolean mIsRecordingVideo;
72
+ private HandlerThread mBackgroundThread;
73
+ private Handler mBackgroundHandler;
74
+ private Semaphore mCameraOpenCloseLock = new Semaphore(1);
75
+
76
+ //{@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its status.
77
+ private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
78
+
79
+ @Override
80
+ public void onOpened(@NonNull CameraDevice cameraDevice) {
81
+ mCameraDevice = cameraDevice;
82
+
83
+ startPreview();
84
+ mCameraOpenCloseLock.release();
85
+ if (null != mTextureView) {
86
+ configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
87
+ }
88
+ }
89
+
90
+ @Override
91
+ public void onDisconnected(@NonNull CameraDevice cameraDevice) {
92
+ mCameraOpenCloseLock.release();
93
+ cameraDevice.close();
94
+ mCameraDevice = null;
95
+ }
96
+
97
+ @Override
98
+ public void onError(@NonNull CameraDevice cameraDevice, int error) {
99
+ mCameraOpenCloseLock.release();
100
+ cameraDevice.close();
101
+ mCameraDevice = null;
102
+ }
103
+
104
+ };
105
+
106
+
107
+ private Integer mSensorOrientation;
108
+ private String mNextVideoAbsolutePath;
109
+ private CaptureRequest.Builder mPreviewBuilder;
110
+
111
+ public static com.example.android.camera2video.Camera2MovieService newInstance() {
112
+ return new com.example.android.camera2video.Camera2MovieService();
113
+ }
114
+
115
+
116
+ public Camera2MovieService() {
117
+ }
118
+
119
+
120
+ private static Size chooseVideoSize(Size[] choices) {
121
+ for (Size size : choices) {
122
+ if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
123
+ return size;
124
+ }
125
+ }
126
+ Log.e(TAG, "Couldn't find any suitable video size");
127
+ return choices[choices.length - 1];
128
+ }
129
+
130
+
131
+ private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
132
+ List<Size> bigEnough = new ArrayList<>();
133
+ int w = aspectRatio.getWidth();
134
+ int h = aspectRatio.getHeight();
135
+ for (Size option : choices) {
136
+ if (option.getHeight() == option.getWidth() * h / w &&
137
+ option.getWidth() >= width && option.getHeight() >= height) {
138
+ bigEnough.add(option);
139
+ }
140
+ }
141
+ if (bigEnough.size() > 0) {
142
+ return Collections.min(bigEnough, new com.example.android.camera2video.Camera2MovieService().CompareSizesByArea());
143
+ } else {
144
+ Log.e(TAG, "Couldn't find any suitable preview size");
145
+ return choices[0];
146
+ }
147
+ }
148
+
149
+
150
+ @Override
151
+ public IBinder onBind(Intent intent) {
152
+ return null;
153
+ }
154
+
155
+
156
+ public void Resume() {
157
+
158
+ //BackgroundThredにする意味が?
159
+ startBackgroundThread();
160
+ if (mTextureView.isAvailable()) {
161
+ // openCamera(mTextureView.getWidth(), mTextureView.getHeight());
162
+ //permissionは確認済み
163
+ } else {
164
+ mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
165
+ }
166
+ }
167
+
168
+
169
+ //close処理
170
+ public void Pause() {
171
+ closeCamera();
172
+ stopBackgroundThread();
173
+ }
174
+
175
+ //Clickはするものがないからいらない、代わりに、自動で呼び出されるようにしておく
176
+ public void Click(View view) {
177
+
178
+ // switch (view.getId()) {
179
+ //この下は、intentのExtra()で判断する
180
+ // case R.id.video: {
181
+ if (mIsRecordingVideo) {
182
+
183
+ //上の変数はboolean だから、
184
+ //trueだったら、videoを止める
185
+ stopRecordingVideo();
186
+ } else {
187
+ startRecordingVideo();
188
+ }
189
+ }
190
+
191
+ private void startBackgroundThread() {
192
+ mBackgroundThread = new HandlerThread("CameraBackground");
193
+ mBackgroundThread.start();
194
+ mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
195
+ }
196
+
197
+ private void stopBackgroundThread() {
198
+ mBackgroundThread.quitSafely();
199
+ try {
200
+ mBackgroundThread.join();
201
+ mBackgroundThread = null;
202
+ mBackgroundHandler = null;
203
+ } catch (InterruptedException e) {
204
+ e.printStackTrace();
205
+ }
206
+ }
207
+
208
+
209
+
210
+ ```
12
211
  どうぞよろしくお願いします。
13
212
  ![イメージ説明](f9d6c106bcea16798e5136757c9fa040.png)

1

写真の追加

2017/08/03 06:08

投稿

giant
giant

スコア132

title CHANGED
File without changes
body CHANGED
@@ -9,4 +9,5 @@
9
9
 
10
10
  上記2つのURLを組み合わせてアプリを作成しています。
11
11
 
12
- どうぞよろしくお願いします。
12
+ どうぞよろしくお願いします。
13
+ ![イメージ説明](f9d6c106bcea16798e5136757c9fa040.png)