From 969c5a8c74517bb29d8f13429ce370b1cf01fb03 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Sat, 3 Mar 2018 03:42:45 +0100 Subject: [PATCH] Fix preview size computation, demo app details --- .../cameraview/CameraController.java | 16 +++-- .../cameraview/demo/CameraActivity.java | 17 ++++-- .../cameraview/demo/Control.java | 6 -- demo/src/main/res/layout/activity_camera.xml | 61 +++++++++++++++++-- 4 files changed, 78 insertions(+), 22 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 5b6e01ed..8b09cd3a 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -507,18 +507,22 @@ abstract class CameraController implements protected final Size computePreviewSize(List previewSizes) { // instead of flipping everything to the view world, we can just flip the // surface size to the sensor world - boolean flip = flip(REF_SENSOR, REF_VIEW); AspectRatio targetRatio = AspectRatio.of(mPictureSize.getWidth(), mPictureSize.getHeight()); Size targetMinSize = mPreview.getSurfaceSize(); + boolean flip = flip(REF_SENSOR, REF_VIEW); if (flip) targetMinSize = targetMinSize.flip(); LOG.i("size:", "computePreviewSize:", "targetRatio:", targetRatio, "targetMinSize:", targetMinSize); - SizeSelector matchRatio = SizeSelectors.aspectRatio(targetRatio, 0); - SizeSelector matchSize = SizeSelectors.and( + SizeSelector matchRatio = SizeSelectors.and( // Match this aspect ratio and sort by biggest + SizeSelectors.aspectRatio(targetRatio, 0), + SizeSelectors.biggest()); + SizeSelector matchSize = SizeSelectors.and( // Bigger than this size, and sort by smallest SizeSelectors.minHeight(targetMinSize.getHeight()), - SizeSelectors.minWidth(targetMinSize.getWidth())); + SizeSelectors.minWidth(targetMinSize.getWidth()), + SizeSelectors.smallest()); SizeSelector matchAll = SizeSelectors.or( - SizeSelectors.and(matchRatio, matchSize), - SizeSelectors.and(matchRatio, SizeSelectors.biggest()), // If couldn't match both, match ratio and biggest. + SizeSelectors.and(matchRatio, matchSize), // Try to respect both constraints. + matchSize, // If couldn't match aspect ratio, at least respect the size + matchRatio, // If couldn't respect size, at least match aspect ratio SizeSelectors.biggest() // If couldn't match any, take the biggest. ); Size result = matchAll.select(previewSizes).get(0); diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java index 16d5fede..d17c5cf1 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java @@ -19,7 +19,6 @@ import com.otaliastudios.cameraview.CameraOptions; import com.otaliastudios.cameraview.CameraView; import com.otaliastudios.cameraview.PictureResult; import com.otaliastudios.cameraview.SessionType; -import com.otaliastudios.cameraview.Size; import com.otaliastudios.cameraview.VideoResult; import java.io.File; @@ -58,7 +57,8 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis }); findViewById(R.id.edit).setOnClickListener(this); - findViewById(R.id.capturePhoto).setOnClickListener(this); + findViewById(R.id.capturePicture).setOnClickListener(this); + findViewById(R.id.capturePictureSnapshot).setOnClickListener(this); findViewById(R.id.captureVideo).setOnClickListener(this); findViewById(R.id.toggleCamera).setOnClickListener(this); @@ -124,7 +124,8 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis public void onClick(View view) { switch (view.getId()) { case R.id.edit: edit(); break; - case R.id.capturePhoto: capturePhoto(); break; + case R.id.capturePicture: capturePicture(); break; + case R.id.capturePictureSnapshot: capturePictureSnapshot(); break; case R.id.captureVideo: captureVideo(); break; case R.id.toggleCamera: toggleCamera(); break; } @@ -145,7 +146,7 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis b.setState(BottomSheetBehavior.STATE_COLLAPSED); } - private void capturePhoto() { + private void capturePicture() { if (mCapturingPicture) return; mCapturingPicture = true; mCaptureTime = System.currentTimeMillis(); @@ -153,6 +154,14 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis camera.takePicture(); } + private void capturePictureSnapshot() { + if (mCapturingPicture) return; + mCapturingPicture = true; + mCaptureTime = System.currentTimeMillis(); + message("Capturing picture snapshot...", false); + camera.takePictureSnapshot(); + } + private void captureVideo() { if (camera.getSessionType() != SessionType.VIDEO) { message("Can't record video while session type is 'picture'.", false); diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java index 8b8e2eda..3e4e7bad 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java @@ -28,7 +28,6 @@ public enum Control { WIDTH("Width", false), HEIGHT("Height", true), SESSION("Session type", false), - CROP_OUTPUT("Crop output", true), FLASH("Flash", false), WHITE_BALANCE("White balance", false), GRID("Grid", true), @@ -73,7 +72,6 @@ public enum Control { list.add(i); } return list; - case CROP_OUTPUT: return Arrays.asList(true, false); case SESSION: return options.getSupportedControls(SessionType.class); case FLASH: return options.getSupportedControls(Flash.class); case WHITE_BALANCE: return options.getSupportedControls(WhiteBalance.class); @@ -111,7 +109,6 @@ public enum Control { case WIDTH: return view.getLayoutParams().width; case HEIGHT: return view.getLayoutParams().height; case SESSION: return view.getSessionType(); - case CROP_OUTPUT: return view.getCropOutput(); case FLASH: return view.getFlash(); case WHITE_BALANCE: return view.getWhiteBalance(); case GRID: return view.getGrid(); @@ -146,9 +143,6 @@ public enum Control { case HDR: camera.set((com.otaliastudios.cameraview.Control) value); break; - case CROP_OUTPUT: - camera.setCropOutput((boolean) value); - break; case PINCH: camera.mapGesture(Gesture.PINCH, (GestureAction) value); break; diff --git a/demo/src/main/res/layout/activity_camera.xml b/demo/src/main/res/layout/activity_camera.xml index 01e9b719..e5bf910d 100644 --- a/demo/src/main/res/layout/activity_camera.xml +++ b/demo/src/main/res/layout/activity_camera.xml @@ -27,6 +27,16 @@ app:cameraGestureScrollVertical="none" app:cameraSessionType="picture" /> + + + + android:weightSum="4"> + + + + + + + - + android:orientation="vertical" + android:gravity="center" + android:background="@drawable/background" > + + +