質問編集履歴

1

ソースコードの編集

2019/10/07 15:01

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -36,8 +36,6 @@
36
36
 
37
37
 
38
38
 
39
-
40
-
41
39
  ズームの方法として,ScaleGestureDetectorを実装して,textureViewを大きくしたり,小さくしたりすることはできています.
42
40
 
43
41
 
@@ -46,32 +44,18 @@
46
44
 
47
45
  ScaleListenerクラスの中で,スケールに応じてbitmapを変更すればよいのでしょうか??
48
46
 
49
- どなた様かご教授お願い致します.
47
+
50
-
51
-
52
-
53
- 以下がソースコードになります.
54
48
 
55
49
  ```java
56
50
 
57
- package com.example.camera2api ;
58
-
59
-
60
-
61
-
62
-
63
51
  public class MainActivity extends AppCompatActivity {
64
52
 
65
53
  private Camera mCamera;
66
54
 
67
-
68
-
69
55
  private float scale = 1f;
70
56
 
71
57
  private ScaleGestureDetector detector;
72
58
 
73
-
74
-
75
59
  @Override
76
60
 
77
61
  protected void onCreate(Bundle savedInstanceState) {
@@ -88,8 +72,6 @@
88
72
 
89
73
 
90
74
 
91
-
92
-
93
75
  TextureView textureView = (TextureView) findViewById(R.id.texture_view);
94
76
 
95
77
  textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@@ -102,14 +84,10 @@
102
84
 
103
85
  }
104
86
 
105
-
106
-
107
87
  @Override
108
88
 
109
89
  public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
110
90
 
111
-
112
-
113
91
  @Override
114
92
 
115
93
  public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
@@ -144,8 +122,6 @@
144
122
 
145
123
 
146
124
 
147
- //スケールリスナークラス
148
-
149
125
  private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
150
126
 
151
127
  @Override
@@ -158,32 +134,20 @@
158
134
 
159
135
  mCamera.mTextureView.setScaleY(scale);
160
136
 
161
-
137
+ mCamera.changeZoomLevel(2);
162
138
 
163
139
  return true;
164
140
 
165
141
  }
166
142
 
167
-
168
-
169
143
  }
170
144
 
171
-
172
-
173
-
174
-
175
-
176
-
177
145
  @Override
178
146
 
179
147
  protected void onResume() {
180
148
 
181
149
  super.onResume();
182
150
 
183
-
184
-
185
-
186
-
187
151
  Button button = (Button) findViewById(R.id.button);
188
152
 
189
153
  button.setOnClickListener(new View.OnClickListener() {
@@ -192,8 +156,6 @@
192
156
 
193
157
  public void onClick(View v) {
194
158
 
195
-
196
-
197
159
  Bitmap bitmap = mCamera.mTextureView.getBitmap();
198
160
 
199
161
  int bitmapWidth = bitmap.getWidth();
@@ -204,17 +166,11 @@
204
166
 
205
167
  Log.d("debug", "bitmapHeight:" + bitmapHeight);
206
168
 
207
-
208
-
209
-
210
-
211
169
  try{
212
170
 
213
- // 読み書きするファイル名を指定
214
-
215
171
  File resultFile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + "capture.jpeg");
216
172
 
217
- resultFile.getParentFile().mkdir();// 指定したファイル名が無ければ作成する
173
+ resultFile.getParentFile().mkdir();
218
174
 
219
175
  String resultFilepath = resultFile.getPath();
220
176
 
@@ -234,36 +190,12 @@
234
190
 
235
191
  }
236
192
 
237
-
238
-
239
-
240
-
241
-
242
-
243
193
  }
244
194
 
245
195
  });
246
196
 
247
-
248
-
249
-
250
-
251
197
  }
252
198
 
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
- //カメラクラス
266
-
267
199
  class Camera {
268
200
 
269
201
  private CameraDevice mCamera;
@@ -276,6 +208,16 @@
276
208
 
277
209
  private CameraCaptureSession mPreviewSession;
278
210
 
211
+ private int mCurrentZoomLevel = 1;
212
+
213
+ private Rect mCropRegion;
214
+
215
+ private CameraManager manager;
216
+
217
+ private CameraCharacteristics characteristics;
218
+
219
+
220
+
279
221
 
280
222
 
281
223
  private CameraDevice.StateCallback mCameraDeviceCallback = new CameraDevice.StateCallback() {
@@ -342,25 +284,21 @@
342
284
 
343
285
  };
344
286
 
345
-
346
-
347
287
  public Camera(TextureView textureView) {
348
288
 
349
289
  mTextureView = textureView;
350
290
 
351
291
  }
352
292
 
353
-
354
-
355
293
  public void open() {
356
294
 
357
295
  try {
358
296
 
359
- CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
297
+ manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
360
298
 
361
299
  for (String cameraId : manager.getCameraIdList()) {
362
300
 
363
- CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
301
+ characteristics = manager.getCameraCharacteristics(cameraId);
364
302
 
365
303
  if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) {
366
304
 
@@ -370,6 +308,10 @@
370
308
 
371
309
 
372
310
 
311
+ Log.d("debug", "cameraId:" + cameraId);
312
+
313
+
314
+
373
315
 
374
316
 
375
317
  manager.openCamera(cameraId, mCameraDeviceCallback, null);
@@ -412,6 +354,8 @@
412
354
 
413
355
  Surface surface = new Surface(texture);
414
356
 
357
+
358
+
415
359
  try {
416
360
 
417
361
  mPreviewBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
@@ -438,12 +382,102 @@
438
382
 
439
383
  }
440
384
 
385
+ public void changeZoomLevel(int level) {
386
+
387
+ // cameraId = 対象のカメラID
388
+
389
+ Log.d("debug", "changeZoomLevel()が呼ばれました");
390
+
391
+ try {
392
+
393
+ manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
394
+
395
+ //CameraCharacteristics characteristics;
396
+
397
+
398
+
399
+ for (String cameraId : manager.getCameraIdList()) {
400
+
401
+ characteristics = manager.getCameraCharacteristics(cameraId);
402
+
403
+ if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) {
404
+
405
+ StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
406
+
407
+ mCameraSize = map.getOutputSizes(SurfaceTexture.class)[4];
408
+
409
+ Log.d("debug", "cameraId:" + cameraId);
410
+
411
+
412
+
413
+ }
414
+
415
+ }
416
+
417
+
418
+
419
+ } catch (CameraAccessException e) {
420
+
421
+ e.printStackTrace();
422
+
423
+ }
424
+
425
+
426
+
427
+ // 最大ズームサイズ
428
+
429
+ float max = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);
430
+
431
+ if ((int) max < level || mCurrentZoomLevel == level) {
432
+
433
+ return;
434
+
435
+ }
436
+
437
+ Log.d("debug", "max:" + max);
438
+
439
+
440
+
441
+ mCurrentZoomLevel = level;
442
+
443
+ final Rect activeArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
444
+
445
+
446
+
447
+ if (mCurrentZoomLevel == 1) {
448
+
449
+ mCropRegion.set(activeArraySize);
450
+
451
+ Log.d("debug", "mCurrentZoomLevel:" + mCurrentZoomLevel);
452
+
453
+ } else {
454
+
455
+ Log.d("debug", "mCurrentZoomLevel:" + mCurrentZoomLevel);
456
+
457
+ //noinspection ConstantConditions
458
+
459
+ int cx = activeArraySize.centerX();
460
+
461
+ int cy = activeArraySize.centerY();
462
+
463
+ int hw = (activeArraySize.width() >> 1) / mCurrentZoomLevel;
464
+
465
+ int hh = (activeArraySize.height() >> 1) / mCurrentZoomLevel;
466
+
467
+ mCropRegion = new Rect(cx - hw, cy - hh, cy - hh, cy + hh);
468
+
469
+ }
470
+
471
+ }
472
+
441
473
 
442
474
 
443
475
  private void updatePreview() {
444
476
 
445
477
  mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
446
478
 
479
+ mPreviewBuilder.set(CaptureRequest.SCALER_CROP_REGION, mCropRegion);
480
+
447
481
  HandlerThread thread = new HandlerThread("CameraPreview");
448
482
 
449
483
  thread.start();
@@ -464,72 +498,8 @@
464
498
 
465
499
  }
466
500
 
467
-
468
-
469
501
  }
470
502
 
471
-
472
-
473
-
474
-
475
503
  }
476
504
 
477
505
  ```
478
-
479
-
480
-
481
- ```xml
482
-
483
- <?xml version="1.0" encoding="utf-8"?>
484
-
485
- <android.support.constraint.ConstraintLayout
486
-
487
- xmlns:android="http://schemas.android.com/apk/res/android"
488
-
489
- xmlns:app="http://schemas.android.com/apk/res-auto"
490
-
491
- xmlns:tools="http://schemas.android.com/tools"
492
-
493
- android:layout_width="match_parent"
494
-
495
- android:layout_height="match_parent"
496
-
497
- tools:context=".MainActivity">
498
-
499
-
500
-
501
-
502
-
503
- <TextureView
504
-
505
- android:id="@+id/texture_view"
506
-
507
- android:layout_width="match_parent"
508
-
509
- android:layout_height="match_parent" />
510
-
511
-
512
-
513
-
514
-
515
- <Button
516
-
517
- android:id="@+id/button"
518
-
519
- android:layout_width="wrap_content"
520
-
521
- android:layout_height="wrap_content"
522
-
523
- app:layout_constraintBottom_toBottomOf="parent"
524
-
525
- app:layout_constraintLeft_toLeftOf="parent"
526
-
527
- app:layout_constraintRight_toRightOf="parent"/>
528
-
529
-
530
-
531
-
532
-
533
- </android.support.constraint.ConstraintLayout>
534
-
535
- ```