Refactor Previews

v2
Mattia Iavarone 7 years ago
parent 6e8b3a0420
commit a7255ba35f
  1. 5
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java
  2. 66
      cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java
  3. 11
      cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java
  4. 47
      cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java

@ -30,11 +30,6 @@ public class MockCameraPreview extends CameraPreview<View, Void> {
return new View(context);
}
@Override
Surface getSurface() {
return null;
}
@Override
Class<Void> getOutputClass() {
return null;

@ -8,12 +8,12 @@ import android.view.ViewGroup;
abstract class CameraPreview<T extends View, Output> {
private final static CameraLogger LOG = CameraLogger.create(CameraPreview.class.getSimpleName());
protected final static CameraLogger LOG = CameraLogger.create(CameraPreview.class.getSimpleName());
// Used for testing.
Task<Void> mCropTask = new Task<>();
// This is used to notify CameraImpl to recompute its camera Preview size.
// This is used to notify CameraController to recompute its camera Preview size.
// After that, CameraView will need a new layout pass to adapt to the Preview size.
interface SurfaceCallback {
void onSurfaceAvailable();
@ -22,16 +22,15 @@ abstract class CameraPreview<T extends View, Output> {
private SurfaceCallback mSurfaceCallback;
private T mView;
private boolean mCropping;
protected boolean mCropping;
// As far as I can see, these are the view/surface dimensions.
// This live in the 'View' orientation.
private int mSurfaceWidth;
private int mSurfaceHeight;
// These are the surface dimensions in REF_VIEW.
protected int mSurfaceWidth;
protected int mSurfaceHeight;
// As far as I can see, these are the actual preview dimensions, as set in CameraParameters.
private int mDesiredWidth;
private int mDesiredHeight;
// These are the preview stream dimensions, in REF_VIEW.
protected int mDesiredWidth;
protected int mDesiredHeight;
CameraPreview(Context context, ViewGroup parent, SurfaceCallback callback) {
mView = onCreateView(context, parent);
@ -41,8 +40,6 @@ abstract class CameraPreview<T extends View, Output> {
@NonNull
protected abstract T onCreateView(Context context, ViewGroup parent);
abstract Surface getSurface();
@NonNull
final T getView() {
return mView;
@ -57,8 +54,8 @@ abstract class CameraPreview<T extends View, Output> {
// These must be alredy rotated, if needed, to be consistent with surface/view sizes.
void setDesiredSize(int width, int height) {
LOG.i("setDesiredSize:", "desiredW=", width, "desiredH=", height);
this.mDesiredWidth = width;
this.mDesiredHeight = height;
mDesiredWidth = width;
mDesiredHeight = height;
crop();
}
@ -117,49 +114,14 @@ abstract class CameraPreview<T extends View, Output> {
* There might still be some absolute difference (e.g. same ratio but bigger / smaller).
* However that should be already managed by the framework.
*/
private final void crop() {
protected void crop() {
// The base implementation does not support cropping.
mCropTask.start();
if (!supportsCropping()) {
mCropTask.end(null);
return;
};
getView().post(new Runnable() {
@Override
public void run() {
if (mDesiredHeight == 0 || mDesiredWidth == 0 ||
mSurfaceHeight == 0 || mSurfaceWidth == 0) {
mCropTask.end(null);
return;
}
float scaleX = 1f, scaleY = 1f;
AspectRatio current = AspectRatio.of(mSurfaceWidth, mSurfaceHeight);
AspectRatio target = AspectRatio.of(mDesiredWidth, mDesiredHeight);
if (current.toFloat() >= target.toFloat()) {
// We are too short. Must increase height.
scaleY = current.toFloat() / target.toFloat();
} else {
// We must increase width.
scaleX = target.toFloat() / current.toFloat();
}
applyCrop(scaleX, scaleY);
mCropping = scaleX > 1.02f || scaleY > 1.02f;
LOG.i("crop:", "applied scaleX=", scaleX);
LOG.i("crop:", "applied scaleY=", scaleY);
mCropTask.end(null);
}
});
}
protected void applyCrop(float scaleX, float scaleY) {
getView().setScaleX(scaleX);
getView().setScaleY(scaleY);
}
boolean supportsCropping() {
return true;
return false;
}
/**

@ -63,11 +63,6 @@ class SurfaceCameraPreview extends CameraPreview<View, SurfaceHolder> {
return root.findViewById(R.id.surface_view_root);
}
@Override
Surface getSurface() {
return getOutput().getSurface();
}
@Override
SurfaceHolder getOutput() {
return mSurfaceView.getHolder();
@ -78,12 +73,6 @@ class SurfaceCameraPreview extends CameraPreview<View, SurfaceHolder> {
return SurfaceHolder.class;
}
@Override
boolean supportsCropping() {
return false;
}
@Override
protected void applyCrop(float scaleX, float scaleY) {
/* float currWidth = getView().getWidth();
float currHeight = getView().getHeight();

@ -12,8 +12,6 @@ import android.view.ViewGroup;
class TextureCameraPreview extends CameraPreview<TextureView, SurfaceTexture> {
private Surface mSurface;
TextureCameraPreview(Context context, ViewGroup parent, SurfaceCallback callback) {
super(context, parent, callback);
}
@ -49,14 +47,6 @@ class TextureCameraPreview extends CameraPreview<TextureView, SurfaceTexture> {
return texture;
}
@Override
Surface getSurface() {
if (mSurface == null) { // Check if valid?
mSurface = new Surface(getOutput());
}
return mSurface;
}
@Override
Class<SurfaceTexture> getOutputClass() {
return SurfaceTexture.class;
@ -76,4 +66,41 @@ class TextureCameraPreview extends CameraPreview<TextureView, SurfaceTexture> {
}
}
@Override
boolean supportsCropping() {
return true;
}
@Override
protected void crop() {
mCropTask.start();
getView().post(new Runnable() {
@Override
public void run() {
if (mDesiredHeight == 0 || mDesiredWidth == 0 ||
mSurfaceHeight == 0 || mSurfaceWidth == 0) {
mCropTask.end(null);
return;
}
float scaleX = 1f, scaleY = 1f;
AspectRatio current = AspectRatio.of(mSurfaceWidth, mSurfaceHeight);
AspectRatio target = AspectRatio.of(mDesiredWidth, mDesiredHeight);
if (current.toFloat() >= target.toFloat()) {
// We are too short. Must increase height.
scaleY = current.toFloat() / target.toFloat();
} else {
// We must increase width.
scaleX = target.toFloat() / current.toFloat();
}
getView().setScaleX(scaleX);
getView().setScaleY(scaleY);
mCropping = scaleX > 1.02f || scaleY > 1.02f;
LOG.i("crop:", "applied scaleX=", scaleX);
LOG.i("crop:", "applied scaleY=", scaleY);
mCropTask.end(null);
}
});
}
}

Loading…
Cancel
Save