Fix preview size computation, demo app details

v2
Mattia Iavarone 7 years ago
parent 1fd0e957aa
commit 348cda70ad
  1. 16
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  2. 17
      demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java
  3. 6
      demo/src/main/java/com/otaliastudios/cameraview/demo/Control.java
  4. 61
      demo/src/main/res/layout/activity_camera.xml

@ -507,18 +507,22 @@ abstract class CameraController implements
protected final Size computePreviewSize(List<Size> previewSizes) { protected final Size computePreviewSize(List<Size> previewSizes) {
// instead of flipping everything to the view world, we can just flip the // instead of flipping everything to the view world, we can just flip the
// surface size to the sensor world // surface size to the sensor world
boolean flip = flip(REF_SENSOR, REF_VIEW);
AspectRatio targetRatio = AspectRatio.of(mPictureSize.getWidth(), mPictureSize.getHeight()); AspectRatio targetRatio = AspectRatio.of(mPictureSize.getWidth(), mPictureSize.getHeight());
Size targetMinSize = mPreview.getSurfaceSize(); Size targetMinSize = mPreview.getSurfaceSize();
boolean flip = flip(REF_SENSOR, REF_VIEW);
if (flip) targetMinSize = targetMinSize.flip(); if (flip) targetMinSize = targetMinSize.flip();
LOG.i("size:", "computePreviewSize:", "targetRatio:", targetRatio, "targetMinSize:", targetMinSize); LOG.i("size:", "computePreviewSize:", "targetRatio:", targetRatio, "targetMinSize:", targetMinSize);
SizeSelector matchRatio = SizeSelectors.aspectRatio(targetRatio, 0); SizeSelector matchRatio = SizeSelectors.and( // Match this aspect ratio and sort by biggest
SizeSelector matchSize = SizeSelectors.and( SizeSelectors.aspectRatio(targetRatio, 0),
SizeSelectors.biggest());
SizeSelector matchSize = SizeSelectors.and( // Bigger than this size, and sort by smallest
SizeSelectors.minHeight(targetMinSize.getHeight()), SizeSelectors.minHeight(targetMinSize.getHeight()),
SizeSelectors.minWidth(targetMinSize.getWidth())); SizeSelectors.minWidth(targetMinSize.getWidth()),
SizeSelectors.smallest());
SizeSelector matchAll = SizeSelectors.or( SizeSelector matchAll = SizeSelectors.or(
SizeSelectors.and(matchRatio, matchSize), SizeSelectors.and(matchRatio, matchSize), // Try to respect both constraints.
SizeSelectors.and(matchRatio, SizeSelectors.biggest()), // If couldn't match both, match ratio and biggest. 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. SizeSelectors.biggest() // If couldn't match any, take the biggest.
); );
Size result = matchAll.select(previewSizes).get(0); Size result = matchAll.select(previewSizes).get(0);

@ -19,7 +19,6 @@ import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.CameraView; import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.PictureResult; import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.SessionType; import com.otaliastudios.cameraview.SessionType;
import com.otaliastudios.cameraview.Size;
import com.otaliastudios.cameraview.VideoResult; import com.otaliastudios.cameraview.VideoResult;
import java.io.File; 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.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.captureVideo).setOnClickListener(this);
findViewById(R.id.toggleCamera).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) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.edit: edit(); break; 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.captureVideo: captureVideo(); break;
case R.id.toggleCamera: toggleCamera(); break; case R.id.toggleCamera: toggleCamera(); break;
} }
@ -145,7 +146,7 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis
b.setState(BottomSheetBehavior.STATE_COLLAPSED); b.setState(BottomSheetBehavior.STATE_COLLAPSED);
} }
private void capturePhoto() { private void capturePicture() {
if (mCapturingPicture) return; if (mCapturingPicture) return;
mCapturingPicture = true; mCapturingPicture = true;
mCaptureTime = System.currentTimeMillis(); mCaptureTime = System.currentTimeMillis();
@ -153,6 +154,14 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis
camera.takePicture(); camera.takePicture();
} }
private void capturePictureSnapshot() {
if (mCapturingPicture) return;
mCapturingPicture = true;
mCaptureTime = System.currentTimeMillis();
message("Capturing picture snapshot...", false);
camera.takePictureSnapshot();
}
private void captureVideo() { private void captureVideo() {
if (camera.getSessionType() != SessionType.VIDEO) { if (camera.getSessionType() != SessionType.VIDEO) {
message("Can't record video while session type is 'picture'.", false); message("Can't record video while session type is 'picture'.", false);

@ -28,7 +28,6 @@ public enum Control {
WIDTH("Width", false), WIDTH("Width", false),
HEIGHT("Height", true), HEIGHT("Height", true),
SESSION("Session type", false), SESSION("Session type", false),
CROP_OUTPUT("Crop output", true),
FLASH("Flash", false), FLASH("Flash", false),
WHITE_BALANCE("White balance", false), WHITE_BALANCE("White balance", false),
GRID("Grid", true), GRID("Grid", true),
@ -73,7 +72,6 @@ public enum Control {
list.add(i); list.add(i);
} }
return list; return list;
case CROP_OUTPUT: return Arrays.asList(true, false);
case SESSION: return options.getSupportedControls(SessionType.class); case SESSION: return options.getSupportedControls(SessionType.class);
case FLASH: return options.getSupportedControls(Flash.class); case FLASH: return options.getSupportedControls(Flash.class);
case WHITE_BALANCE: return options.getSupportedControls(WhiteBalance.class); case WHITE_BALANCE: return options.getSupportedControls(WhiteBalance.class);
@ -111,7 +109,6 @@ public enum Control {
case WIDTH: return view.getLayoutParams().width; case WIDTH: return view.getLayoutParams().width;
case HEIGHT: return view.getLayoutParams().height; case HEIGHT: return view.getLayoutParams().height;
case SESSION: return view.getSessionType(); case SESSION: return view.getSessionType();
case CROP_OUTPUT: return view.getCropOutput();
case FLASH: return view.getFlash(); case FLASH: return view.getFlash();
case WHITE_BALANCE: return view.getWhiteBalance(); case WHITE_BALANCE: return view.getWhiteBalance();
case GRID: return view.getGrid(); case GRID: return view.getGrid();
@ -146,9 +143,6 @@ public enum Control {
case HDR: case HDR:
camera.set((com.otaliastudios.cameraview.Control) value); camera.set((com.otaliastudios.cameraview.Control) value);
break; break;
case CROP_OUTPUT:
camera.setCropOutput((boolean) value);
break;
case PINCH: case PINCH:
camera.mapGesture(Gesture.PINCH, (GestureAction) value); camera.mapGesture(Gesture.PINCH, (GestureAction) value);
break; break;

@ -27,6 +27,16 @@
app:cameraGestureScrollVertical="none" app:cameraGestureScrollVertical="none"
app:cameraSessionType="picture" /> app:cameraSessionType="picture" />
<ImageButton
android:id="@+id/toggleCamera"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="16dp"
android:layout_gravity="top|end"
android:background="@drawable/background"
app:srcCompat="@drawable/ic_switch" />
<!-- Controls --> <!-- Controls -->
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -34,7 +44,7 @@
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:padding="16dp" android:padding="16dp"
android:orientation="horizontal" android:orientation="horizontal"
android:weightSum="3"> android:weightSum="4">
<ImageButton <ImageButton
android:id="@+id/edit" android:id="@+id/edit"
@ -49,12 +59,37 @@
android:layout_weight="1" /> android:layout_weight="1" />
<ImageButton <ImageButton
android:id="@+id/capturePhoto" android:id="@+id/capturePicture"
android:layout_width="56dp" android:layout_width="56dp"
android:layout_height="56dp" android:layout_height="56dp"
android:background="@drawable/background" android:background="@drawable/background"
app:srcCompat="@drawable/ic_photo" /> app:srcCompat="@drawable/ic_photo" />
<Space
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/capturePictureSnapshot"
android:layout_width="56dp"
android:layout_height="56dp"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/background" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_photo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="10sp"
android:text="SNAP"/>
</LinearLayout>
<Space <Space
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="1dp" android:layout_height="1dp"
@ -72,12 +107,26 @@
android:layout_height="1dp" android:layout_height="1dp"
android:layout_weight="1" /> android:layout_weight="1" />
<ImageButton
android:id="@+id/toggleCamera" <LinearLayout
android:id="@+id/captureVideoSnapshot"
android:layout_width="56dp" android:layout_width="56dp"
android:layout_height="56dp" android:layout_height="56dp"
android:background="@drawable/background" android:orientation="vertical"
app:srcCompat="@drawable/ic_switch" /> android:gravity="center"
android:background="@drawable/background" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_video"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="10sp"
android:text="SNAP"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
<!-- Edit --> <!-- Edit -->

Loading…
Cancel
Save