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

質問編集履歴

1

ソースコードの編集

2019/10/07 15:01

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

title CHANGED
File without changes
body CHANGED
@@ -17,24 +17,16 @@
17
17
  撮影ボタンを押すと,もとのズームされていない方の画像が撮影されてしまうのを直したいです.
18
18
 
19
19
 
20
-
21
20
  ズームの方法として,ScaleGestureDetectorを実装して,textureViewを大きくしたり,小さくしたりすることはできています.
22
21
 
23
22
  mCamera.mTextureView.getBitmap();で,textureViewをビットマップ形式で取り出して,作成したファイルに保存する流れを取っているのですが,ズームの部分のコードでtextureViewのビットマップの値を変えてないから,もとのビットマップの値のまま取り出しているということが問題だとはわかっていますが,どう直せばよいのかが分かりません.
24
23
  ScaleListenerクラスの中で,スケールに応じてbitmapを変更すればよいのでしょうか??
25
- どなた様かご教授お願い致します.
26
24
 
27
- 以下がソースコードになります.
28
25
  ```java
29
- package com.example.camera2api ;
30
-
31
-
32
26
  public class MainActivity extends AppCompatActivity {
33
27
  private Camera mCamera;
34
-
35
28
  private float scale = 1f;
36
29
  private ScaleGestureDetector detector;
37
-
38
30
  @Override
39
31
  protected void onCreate(Bundle savedInstanceState) {
40
32
  super.onCreate(savedInstanceState);
@@ -43,17 +35,14 @@
43
35
 
44
36
  detector = new ScaleGestureDetector(this,new ScaleListener());
45
37
 
46
-
47
38
  TextureView textureView = (TextureView) findViewById(R.id.texture_view);
48
39
  textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
49
40
  @Override
50
41
  public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
51
42
  mCamera.open();
52
43
  }
53
-
54
44
  @Override
55
45
  public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
56
-
57
46
  @Override
58
47
  public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
59
48
  return false;
@@ -71,42 +60,31 @@
71
60
  return super.onTouchEvent(event);
72
61
  }
73
62
 
74
- //スケールリスナークラス
75
63
  private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
76
64
  @Override
77
65
  public boolean onScale(ScaleGestureDetector detector) {
78
66
  scale *= detector.getScaleFactor();
79
67
  mCamera.mTextureView.setScaleX(scale);
80
68
  mCamera.mTextureView.setScaleY(scale);
81
-
69
+ mCamera.changeZoomLevel(2);
82
70
  return true;
83
71
  }
84
-
85
72
  }
86
-
87
-
88
-
89
73
  @Override
90
74
  protected void onResume() {
91
75
  super.onResume();
92
-
93
-
94
76
  Button button = (Button) findViewById(R.id.button);
95
77
  button.setOnClickListener(new View.OnClickListener() {
96
78
  @Override
97
79
  public void onClick(View v) {
98
-
99
80
  Bitmap bitmap = mCamera.mTextureView.getBitmap();
100
81
  int bitmapWidth = bitmap.getWidth();
101
82
  int bitmapHeight= bitmap.getHeight();
102
83
  Log.d("debug", "bitmapWidth:" + bitmapWidth);
103
84
  Log.d("debug", "bitmapHeight:" + bitmapHeight);
104
-
105
-
106
85
  try{
107
- // 読み書きするファイル名を指定
108
86
  File resultFile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + "capture.jpeg");
109
- resultFile.getParentFile().mkdir();// 指定したファイル名が無ければ作成する
87
+ resultFile.getParentFile().mkdir();
110
88
  String resultFilepath = resultFile.getPath();
111
89
 
112
90
  FileOutputStream output = new FileOutputStream(resultFilepath);
@@ -116,28 +94,21 @@
116
94
  } catch (IOException e) {
117
95
  e.printStackTrace();
118
96
  }
119
-
120
-
121
-
122
97
  }
123
98
  });
124
-
125
-
126
99
  }
127
-
128
-
129
-
130
-
131
-
132
-
133
- //カメラクラス
134
100
  class Camera {
135
101
  private CameraDevice mCamera;
136
102
  private TextureView mTextureView;
137
103
  private Size mCameraSize;
138
104
  private CaptureRequest.Builder mPreviewBuilder;
139
105
  private CameraCaptureSession mPreviewSession;
106
+ private int mCurrentZoomLevel = 1;
107
+ private Rect mCropRegion;
108
+ private CameraManager manager;
109
+ private CameraCharacteristics characteristics;
140
110
 
111
+
141
112
  private CameraDevice.StateCallback mCameraDeviceCallback = new CameraDevice.StateCallback() {
142
113
  @Override
143
114
  public void onOpened(@NonNull CameraDevice camera) {
@@ -170,21 +141,21 @@
170
141
  Toast.makeText(MainActivity.this, "onConfigureFailed", Toast.LENGTH_LONG).show();
171
142
  }
172
143
  };
173
-
174
144
  public Camera(TextureView textureView) {
175
145
  mTextureView = textureView;
176
146
  }
177
-
178
147
  public void open() {
179
148
  try {
180
- CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
149
+ manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
181
150
  for (String cameraId : manager.getCameraIdList()) {
182
- CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
151
+ characteristics = manager.getCameraCharacteristics(cameraId);
183
152
  if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) {
184
153
  StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
185
154
  mCameraSize = map.getOutputSizes(SurfaceTexture.class)[4];
186
155
 
156
+ Log.d("debug", "cameraId:" + cameraId);
187
157
 
158
+
188
159
  manager.openCamera(cameraId, mCameraDeviceCallback, null);
189
160
 
190
161
 
@@ -205,6 +176,7 @@
205
176
  SurfaceTexture texture = mTextureView.getSurfaceTexture();
206
177
  texture.setDefaultBufferSize(mCameraSize.getWidth(), mCameraSize.getHeight());
207
178
  Surface surface = new Surface(texture);
179
+
208
180
  try {
209
181
  mPreviewBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
210
182
  } catch (CameraAccessException e) {
@@ -218,9 +190,54 @@
218
190
  e.printStackTrace();
219
191
  }
220
192
  }
193
+ public void changeZoomLevel(int level) {
194
+ // cameraId = 対象のカメラID
195
+ Log.d("debug", "changeZoomLevel()が呼ばれました");
196
+ try {
197
+ manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
198
+ //CameraCharacteristics characteristics;
221
199
 
200
+ for (String cameraId : manager.getCameraIdList()) {
201
+ characteristics = manager.getCameraCharacteristics(cameraId);
202
+ if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) {
203
+ StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
204
+ mCameraSize = map.getOutputSizes(SurfaceTexture.class)[4];
205
+ Log.d("debug", "cameraId:" + cameraId);
206
+
207
+ }
208
+ }
209
+
210
+ } catch (CameraAccessException e) {
211
+ e.printStackTrace();
212
+ }
213
+
214
+ // 最大ズームサイズ
215
+ float max = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);
216
+ if ((int) max < level || mCurrentZoomLevel == level) {
217
+ return;
218
+ }
219
+ Log.d("debug", "max:" + max);
220
+
221
+ mCurrentZoomLevel = level;
222
+ final Rect activeArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
223
+
224
+ if (mCurrentZoomLevel == 1) {
225
+ mCropRegion.set(activeArraySize);
226
+ Log.d("debug", "mCurrentZoomLevel:" + mCurrentZoomLevel);
227
+ } else {
228
+ Log.d("debug", "mCurrentZoomLevel:" + mCurrentZoomLevel);
229
+ //noinspection ConstantConditions
230
+ int cx = activeArraySize.centerX();
231
+ int cy = activeArraySize.centerY();
232
+ int hw = (activeArraySize.width() >> 1) / mCurrentZoomLevel;
233
+ int hh = (activeArraySize.height() >> 1) / mCurrentZoomLevel;
234
+ mCropRegion = new Rect(cx - hw, cy - hh, cy - hh, cy + hh);
235
+ }
236
+ }
237
+
222
238
  private void updatePreview() {
223
239
  mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
240
+ mPreviewBuilder.set(CaptureRequest.SCALER_CROP_REGION, mCropRegion);
224
241
  HandlerThread thread = new HandlerThread("CameraPreview");
225
242
  thread.start();
226
243
  Handler backgroundHandler = new Handler(thread.getLooper());
@@ -231,38 +248,6 @@
231
248
  e.printStackTrace();
232
249
  }
233
250
  }
234
-
235
251
  }
236
-
237
-
238
252
  }
239
- ```
240
-
241
- ```xml
242
- <?xml version="1.0" encoding="utf-8"?>
243
- <android.support.constraint.ConstraintLayout
244
- xmlns:android="http://schemas.android.com/apk/res/android"
245
- xmlns:app="http://schemas.android.com/apk/res-auto"
246
- xmlns:tools="http://schemas.android.com/tools"
247
- android:layout_width="match_parent"
248
- android:layout_height="match_parent"
249
- tools:context=".MainActivity">
250
-
251
-
252
- <TextureView
253
- android:id="@+id/texture_view"
254
- android:layout_width="match_parent"
255
- android:layout_height="match_parent" />
256
-
257
-
258
- <Button
259
- android:id="@+id/button"
260
- android:layout_width="wrap_content"
261
- android:layout_height="wrap_content"
262
- app:layout_constraintBottom_toBottomOf="parent"
263
- app:layout_constraintLeft_toLeftOf="parent"
264
- app:layout_constraintRight_toRightOf="parent"/>
265
-
266
-
267
- </android.support.constraint.ConstraintLayout>
268
253
  ```