PCスペック
Windows10 HOME 64bit
メモリ 32GB
プロセッサ Intel Core i7-8700CPU 3.20GHz
Android Studio 3.4.1(最新ver)
デモ用端末(XPERIA OS:7.0 API 24)
camera2APIを使って,カメラアプリを開発しています.
現在の進捗
とりあえずカメラを起動させて,ボタンを押したら撮影されるところまでできました.
現在の課題
ズーム機能を実装したのですが,
撮影ボタンを押すと,もとのズームされていない方の画像が撮影されてしまうのを直したいです.
ズームの方法として,ScaleGestureDetectorを実装して,textureViewを大きくしたり,小さくしたりすることはできています.
mCamera.mTextureView.getBitmap();で,textureViewをビットマップ形式で取り出して,作成したファイルに保存する流れを取っているのですが,ズームの部分のコードでtextureViewのビットマップの値を変えてないから,もとのビットマップの値のまま取り出しているということが問題だとはわかっていますが,どう直せばよいのかが分かりません.
ScaleListenerクラスの中で,スケールに応じてbitmapを変更すればよいのでしょうか??
java
1public class MainActivity extends AppCompatActivity { 2 private Camera mCamera; 3 private float scale = 1f; 4 private ScaleGestureDetector detector; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 9 setContentView(R.layout.activity_main); 10 11 detector = new ScaleGestureDetector(this,new ScaleListener()); 12 13 TextureView textureView = (TextureView) findViewById(R.id.texture_view); 14 textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { 15 @Override 16 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 17 mCamera.open(); 18 } 19 @Override 20 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {} 21 @Override 22 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 23 return false; 24 } 25 26 @Override 27 public void onSurfaceTextureUpdated(SurfaceTexture surface) {} 28 }); 29 30 mCamera = new Camera(textureView); 31 } 32 33 public boolean onTouchEvent(MotionEvent event) { 34 detector.onTouchEvent(event); 35 return super.onTouchEvent(event); 36 } 37 38 private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { 39 @Override 40 public boolean onScale(ScaleGestureDetector detector) { 41 scale *= detector.getScaleFactor(); 42 mCamera.mTextureView.setScaleX(scale); 43 mCamera.mTextureView.setScaleY(scale); 44 mCamera.changeZoomLevel(2); 45 return true; 46 } 47 } 48 @Override 49 protected void onResume() { 50 super.onResume(); 51 Button button = (Button) findViewById(R.id.button); 52 button.setOnClickListener(new View.OnClickListener() { 53 @Override 54 public void onClick(View v) { 55 Bitmap bitmap = mCamera.mTextureView.getBitmap(); 56 int bitmapWidth = bitmap.getWidth(); 57 int bitmapHeight= bitmap.getHeight(); 58 Log.d("debug", "bitmapWidth:" + bitmapWidth); 59 Log.d("debug", "bitmapHeight:" + bitmapHeight); 60 try{ 61 File resultFile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + "capture.jpeg"); 62 resultFile.getParentFile().mkdir(); 63 String resultFilepath = resultFile.getPath(); 64 65 FileOutputStream output = new FileOutputStream(resultFilepath); 66 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output); 67 output.flush(); 68 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 } 73 }); 74 } 75 class Camera { 76 private CameraDevice mCamera; 77 private TextureView mTextureView; 78 private Size mCameraSize; 79 private CaptureRequest.Builder mPreviewBuilder; 80 private CameraCaptureSession mPreviewSession; 81 private int mCurrentZoomLevel = 1; 82 private Rect mCropRegion; 83 private CameraManager manager; 84 private CameraCharacteristics characteristics; 85 86 87 private CameraDevice.StateCallback mCameraDeviceCallback = new CameraDevice.StateCallback() { 88 @Override 89 public void onOpened(@NonNull CameraDevice camera) { 90 mCamera = camera; 91 createCaptureSession(); 92 } 93 94 @Override 95 public void onDisconnected(@NonNull CameraDevice camera) { 96 camera.close(); 97 mCamera = null; 98 } 99 100 @Override 101 public void onError(@NonNull CameraDevice camera, int error) { 102 camera.close(); 103 mCamera = null; 104 } 105 }; 106 107 CameraCaptureSession.StateCallback mCameraCaptureSessionCallback = new CameraCaptureSession.StateCallback() { 108 @Override 109 public void onConfigured(@NonNull CameraCaptureSession session) { 110 mPreviewSession = session; 111 updatePreview(); 112 } 113 114 @Override 115 public void onConfigureFailed(@NonNull CameraCaptureSession session) { 116 Toast.makeText(MainActivity.this, "onConfigureFailed", Toast.LENGTH_LONG).show(); 117 } 118 }; 119 public Camera(TextureView textureView) { 120 mTextureView = textureView; 121 } 122 public void open() { 123 try { 124 manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 125 for (String cameraId : manager.getCameraIdList()) { 126 characteristics = manager.getCameraCharacteristics(cameraId); 127 if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) { 128 StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 129 mCameraSize = map.getOutputSizes(SurfaceTexture.class)[4]; 130 131 Log.d("debug", "cameraId:" + cameraId); 132 133 134 manager.openCamera(cameraId, mCameraDeviceCallback, null); 135 136 137 138 return; 139 } 140 } 141 } catch (CameraAccessException e) { 142 e.printStackTrace(); 143 } 144 } 145 146 private void createCaptureSession() { 147 if (!mTextureView.isAvailable()) { 148 return; 149 } 150 151 SurfaceTexture texture = mTextureView.getSurfaceTexture(); 152 texture.setDefaultBufferSize(mCameraSize.getWidth(), mCameraSize.getHeight()); 153 Surface surface = new Surface(texture); 154 155 try { 156 mPreviewBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 157 } catch (CameraAccessException e) { 158 e.printStackTrace(); 159 } 160 161 mPreviewBuilder.addTarget(surface); 162 try { 163 mCamera.createCaptureSession(Collections.singletonList(surface), mCameraCaptureSessionCallback, null); 164 } catch (CameraAccessException e) { 165 e.printStackTrace(); 166 } 167 } 168 public void changeZoomLevel(int level) { 169 // cameraId = 対象のカメラID 170 Log.d("debug", "changeZoomLevel()が呼ばれました"); 171 try { 172 manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 173 //CameraCharacteristics characteristics; 174 175 for (String cameraId : manager.getCameraIdList()) { 176 characteristics = manager.getCameraCharacteristics(cameraId); 177 if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) { 178 StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 179 mCameraSize = map.getOutputSizes(SurfaceTexture.class)[4]; 180 Log.d("debug", "cameraId:" + cameraId); 181 182 } 183 } 184 185 } catch (CameraAccessException e) { 186 e.printStackTrace(); 187 } 188 189 // 最大ズームサイズ 190 float max = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM); 191 if ((int) max < level || mCurrentZoomLevel == level) { 192 return; 193 } 194 Log.d("debug", "max:" + max); 195 196 mCurrentZoomLevel = level; 197 final Rect activeArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); 198 199 if (mCurrentZoomLevel == 1) { 200 mCropRegion.set(activeArraySize); 201 Log.d("debug", "mCurrentZoomLevel:" + mCurrentZoomLevel); 202 } else { 203 Log.d("debug", "mCurrentZoomLevel:" + mCurrentZoomLevel); 204 //noinspection ConstantConditions 205 int cx = activeArraySize.centerX(); 206 int cy = activeArraySize.centerY(); 207 int hw = (activeArraySize.width() >> 1) / mCurrentZoomLevel; 208 int hh = (activeArraySize.height() >> 1) / mCurrentZoomLevel; 209 mCropRegion = new Rect(cx - hw, cy - hh, cy - hh, cy + hh); 210 } 211 } 212 213 private void updatePreview() { 214 mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 215 mPreviewBuilder.set(CaptureRequest.SCALER_CROP_REGION, mCropRegion); 216 HandlerThread thread = new HandlerThread("CameraPreview"); 217 thread.start(); 218 Handler backgroundHandler = new Handler(thread.getLooper()); 219 220 try { 221 mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, backgroundHandler); 222 } catch (CameraAccessException e) { 223 e.printStackTrace(); 224 } 225 } 226 } 227}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。