Reorder to overlay package

pull/502/head
Mattia Iavarone 6 years ago
parent a577fbe653
commit 3a6f3426b0
  1. 114
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  2. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java
  3. 30
      cameraview/src/main/java/com/otaliastudios/cameraview/overlay/OverlayLayout.java
  4. 8
      cameraview/src/main/java/com/otaliastudios/cameraview/overlay/OverlayLayoutInner.java
  5. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/overlay/SurfaceDrawer.java
  6. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/picture/SnapshotGlPictureRecorder.java
  7. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/video/SnapshotVideoRecorder.java

@ -63,6 +63,7 @@ import com.otaliastudios.cameraview.internal.utils.CropHelper;
import com.otaliastudios.cameraview.internal.utils.OrientationHelper; import com.otaliastudios.cameraview.internal.utils.OrientationHelper;
import com.otaliastudios.cameraview.internal.utils.WorkerHandler; import com.otaliastudios.cameraview.internal.utils.WorkerHandler;
import com.otaliastudios.cameraview.markers.MarkerParser; import com.otaliastudios.cameraview.markers.MarkerParser;
import com.otaliastudios.cameraview.overlay.OverlayLayout;
import com.otaliastudios.cameraview.preview.CameraPreview; import com.otaliastudios.cameraview.preview.CameraPreview;
import com.otaliastudios.cameraview.preview.GlCameraPreview; import com.otaliastudios.cameraview.preview.GlCameraPreview;
import com.otaliastudios.cameraview.preview.SurfaceCameraPreview; import com.otaliastudios.cameraview.preview.SurfaceCameraPreview;
@ -133,8 +134,8 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
private boolean mExperimental; private boolean mExperimental;
// Overlays // Overlays
private OverlayLayoutManager mOverlayLayoutManager; // see OverlayLayoutManager for why having two of them private OverlayLayout mOverlayLayoutManager; // see OverlayLayoutManager for why having two of them
private OverlayLayoutManager mOverlayLayoutManagerBelow; private OverlayLayout mOverlayLayoutManagerBelow;
// Threading // Threading
private Handler mUiHandler; private Handler mUiHandler;
@ -192,8 +193,8 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
// Views // Views
mGridLinesLayout = new GridLinesLayout(context); mGridLinesLayout = new GridLinesLayout(context);
mOverlayLayoutManager = new OverlayLayoutManager(context); mOverlayLayoutManager = new OverlayLayout(context);
mOverlayLayoutManagerBelow = new OverlayLayoutManager(context); mOverlayLayoutManagerBelow = new OverlayLayout(context);
mMarkerLayout = new MarkerLayout(context); mMarkerLayout = new MarkerLayout(context);
addView(mGridLinesLayout); addView(mGridLinesLayout);
addView(mMarkerLayout); addView(mMarkerLayout);
@ -246,7 +247,17 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
* {@link #setEngine(Engine)} is called. * {@link #setEngine(Engine)} is called.
*/ */
private void doInstantiateEngine() { private void doInstantiateEngine() {
if (mCameraEngine != null) {
mCameraEngine.removePictureSurfaceDrawer(mOverlayLayoutManager);
mCameraEngine.removePictureSurfaceDrawer(mOverlayLayoutManagerBelow);
mCameraEngine.removeVideoSurfaceDrawer(mOverlayLayoutManager);
mCameraEngine.removeVideoSurfaceDrawer(mOverlayLayoutManagerBelow);
}
mCameraEngine = instantiateCameraEngine(mEngine, mCameraCallbacks); mCameraEngine = instantiateCameraEngine(mEngine, mCameraCallbacks);
mCameraEngine.addPictureSurfaceDrawer(mOverlayLayoutManager);
mCameraEngine.addPictureSurfaceDrawer(mOverlayLayoutManagerBelow);
mCameraEngine.addVideoSurfaceDrawer(mOverlayLayoutManager);
mCameraEngine.addVideoSurfaceDrawer(mOverlayLayoutManagerBelow);
} }
/** /**
@ -313,10 +324,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
// isHardwareAccelerated will return the real value only after we are // isHardwareAccelerated will return the real value only after we are
// attached. That's why we instantiate the preview here. // attached. That's why we instantiate the preview here.
doInstantiatePreview(); doInstantiatePreview();
mCameraEngine.addPictureSurfaceDrawer(mOverlayLayoutManager);
mCameraEngine.addPictureSurfaceDrawer(mOverlayLayoutManagerBelow);
mCameraEngine.addVideoSurfaceDrawer(mOverlayLayoutManager);
mCameraEngine.addVideoSurfaceDrawer(mOverlayLayoutManagerBelow);
} }
if (!isInEditMode()) { if (!isInEditMode()) {
mOrientationHelper.enable(getContext()); mOrientationHelper.enable(getContext());
@ -331,49 +338,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
super.onDetachedFromWindow(); super.onDetachedFromWindow();
} }
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
if (params instanceof OverlayLayoutParams) {
if (((OverlayLayoutParams) params).drawInPreview) {
mOverlayLayoutManager.addView(child, params);
} else {
mOverlayLayoutManagerBelow.addView(child, params);
}
} else {
super.addView(child, params);
}
}
@Override
public void removeView(View child) {
if (child.getLayoutParams() instanceof OverlayLayoutParams) {
if (((OverlayLayoutParams) child.getLayoutParams()).drawInPreview) {
mOverlayLayoutManager.removeView(child);
} else {
mOverlayLayoutManagerBelow.removeView(child);
}
} else {
super.removeView(child);
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
OverlayLayoutParams toBeChecked = new OverlayLayoutParams(this.getContext(), attributeSet);
if (toBeChecked.isOverlay()) {
return toBeChecked;
}
return super.generateLayoutParams(attributeSet);
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
if (p instanceof OverlayLayoutParams) {
return p;
}
return super.generateLayoutParams(p);
}
//endregion //endregion
//region Measuring behavior //region Measuring behavior
@ -2135,6 +2099,54 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
} }
} }
//endregion
//region Overlays
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
if (params instanceof OverlayLayoutParams) {
if (((OverlayLayoutParams) params).drawInPreview) {
mOverlayLayoutManager.addView(child, params);
} else {
mOverlayLayoutManagerBelow.addView(child, params);
}
} else {
super.addView(child, params);
}
}
@Override
public void removeView(View child) {
if (child.getLayoutParams() instanceof OverlayLayoutParams) {
if (((OverlayLayoutParams) child.getLayoutParams()).drawInPreview) {
mOverlayLayoutManager.removeView(child);
} else {
mOverlayLayoutManagerBelow.removeView(child);
}
} else {
super.removeView(child);
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
OverlayLayoutParams toBeChecked = new OverlayLayoutParams(this.getContext(), attributeSet);
if (toBeChecked.isOverlay()) {
return toBeChecked;
}
return super.generateLayoutParams(attributeSet);
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
if (p instanceof OverlayLayoutParams) {
return p;
}
return super.generateLayoutParams(p);
}
public static class OverlayLayoutParams extends FrameLayout.LayoutParams { public static class OverlayLayoutParams extends FrameLayout.LayoutParams {
private boolean drawInPreview = false; private boolean drawInPreview = false;

@ -18,7 +18,7 @@ import com.otaliastudios.cameraview.CameraException;
import com.otaliastudios.cameraview.CameraLogger; import com.otaliastudios.cameraview.CameraLogger;
import com.otaliastudios.cameraview.CameraOptions; import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.PictureResult; import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.SurfaceDrawer; import com.otaliastudios.cameraview.overlay.SurfaceDrawer;
import com.otaliastudios.cameraview.VideoResult; import com.otaliastudios.cameraview.VideoResult;
import com.otaliastudios.cameraview.engine.offset.Angles; import com.otaliastudios.cameraview.engine.offset.Angles;
import com.otaliastudios.cameraview.engine.offset.Reference; import com.otaliastudios.cameraview.engine.offset.Reference;

@ -1,4 +1,4 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview.overlay;
import android.content.Context; import android.content.Context;
@ -11,11 +11,13 @@ import android.widget.FrameLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.otaliastudios.cameraview.CameraView;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* This class manages {@link OverlayLayout}s. * This class manages {@link OverlayLayoutInner}s.
* The necessity for this class comes from two features of {@link View}s: * The necessity for this class comes from two features of {@link View}s:
* - a {@link View} can only have one parent * - a {@link View} can only have one parent
* - the View framework does not provide a straightforward way for a {@link ViewGroup} to draw * - the View framework does not provide a straightforward way for a {@link ViewGroup} to draw
@ -25,26 +27,26 @@ import java.util.Map;
* - picture snapshot * - picture snapshot
* - video snapshot * - video snapshot
* Given the two constraints above in order to draw exclusively on a subset of targets we need a * Given the two constraints above in order to draw exclusively on a subset of targets we need a
* different {@link OverlayLayout} for each subset of targets. This class manages those different * different {@link OverlayLayoutInner} for each subset of targets. This class manages those different
* {@link OverlayLayout}s. * {@link OverlayLayoutInner}s.
* *
* A problem remains: the views are drawn on preview when {@link #draw(Canvas)} is called on this * A problem remains: the views are drawn on preview when {@link #draw(Canvas)} is called on this
* class, for not drawing on the preview but drawing on picture snapshot, for instance, we cannot * class, for not drawing on the preview but drawing on picture snapshot, for instance, we cannot
* change the child's visibility. * change the child's visibility.
* One way to solve this problem is to have two instances of {@link OverlayLayoutManager} and layer * One way to solve this problem is to have two instances of {@link OverlayLayout} and layer
* them so that the one below is covered and hidden by the camera preview. This way only the top * them so that the one below is covered and hidden by the camera preview. This way only the top
* {@link OverlayLayoutManager} is shown on top of the camera preview and we can still access the * {@link OverlayLayout} is shown on top of the camera preview and we can still access the
* bottom one's {@link OverlayLayout#draw(Canvas)} for drawing on picture snapshots. * bottom one's {@link OverlayLayoutInner#draw(Canvas)} for drawing on picture snapshots.
*/ */
class OverlayLayoutManager extends FrameLayout implements SurfaceDrawer { public class OverlayLayout extends FrameLayout implements SurfaceDrawer {
private Map<OverlayType, OverlayLayout> mLayouts = new HashMap<>(); private Map<OverlayType, OverlayLayoutInner> mLayouts = new HashMap<>();
public OverlayLayoutManager(@NonNull Context context) { public OverlayLayout(@NonNull Context context) {
super(context); super(context);
} }
public OverlayLayoutManager(@NonNull Context context, @Nullable AttributeSet attrs) { public OverlayLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
} }
@ -59,7 +61,7 @@ class OverlayLayoutManager extends FrameLayout implements SurfaceDrawer {
if (mLayouts.containsKey(viewOverlayType)) { if (mLayouts.containsKey(viewOverlayType)) {
mLayouts.get(viewOverlayType).addView(child, params); mLayouts.get(viewOverlayType).addView(child, params);
} else { } else {
OverlayLayout newLayout = new OverlayLayout(getContext()); OverlayLayoutInner newLayout = new OverlayLayoutInner(getContext());
newLayout.addView(child, params); newLayout.addView(child, params);
super.addView(newLayout); super.addView(newLayout);
mLayouts.put(viewOverlayType, newLayout); mLayouts.put(viewOverlayType, newLayout);
@ -87,7 +89,7 @@ class OverlayLayoutManager extends FrameLayout implements SurfaceDrawer {
// scale factor between canvas height and this View's height // scale factor between canvas height and this View's height
float heightScale = surfaceCanvas.getHeight() / (float) getHeight(); float heightScale = surfaceCanvas.getHeight() / (float) getHeight();
surfaceCanvas.scale(widthScale, heightScale); surfaceCanvas.scale(widthScale, heightScale);
for (Map.Entry<OverlayType, OverlayLayout> entry : mLayouts.entrySet()) { for (Map.Entry<OverlayType, OverlayLayoutInner> entry : mLayouts.entrySet()) {
if (entry.getKey().pictureSnapshot) { if (entry.getKey().pictureSnapshot) {
entry.getValue().drawOverlay(surfaceCanvas); entry.getValue().drawOverlay(surfaceCanvas);
} }
@ -103,7 +105,7 @@ class OverlayLayoutManager extends FrameLayout implements SurfaceDrawer {
// scale factor between canvas height and this View's height // scale factor between canvas height and this View's height
float heightScale = surfaceCanvas.getHeight() / (float) getHeight(); float heightScale = surfaceCanvas.getHeight() / (float) getHeight();
surfaceCanvas.scale(widthScale, heightScale); surfaceCanvas.scale(widthScale, heightScale);
for (Map.Entry<OverlayType, OverlayLayout> entry : mLayouts.entrySet()) { for (Map.Entry<OverlayType, OverlayLayoutInner> entry : mLayouts.entrySet()) {
if (entry.getKey().videoSnapshot) { if (entry.getKey().videoSnapshot) {
entry.getValue().drawOverlay(surfaceCanvas); entry.getValue().drawOverlay(surfaceCanvas);
} }

@ -1,4 +1,4 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview.overlay;
import android.content.Context; import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
@ -8,14 +8,14 @@ import android.widget.FrameLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
class OverlayLayout extends FrameLayout { public class OverlayLayoutInner extends FrameLayout {
public OverlayLayout(@NonNull Context context) { public OverlayLayoutInner(@NonNull Context context) {
super(context); super(context);
setWillNotDraw(false); setWillNotDraw(false);
} }
public OverlayLayout(@NonNull Context context, @Nullable AttributeSet attrs) { public OverlayLayoutInner(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
setWillNotDraw(false); setWillNotDraw(false);
} }

@ -1,4 +1,4 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview.overlay;
import android.graphics.Canvas; import android.graphics.Canvas;

@ -14,7 +14,7 @@ import android.os.Build;
import com.otaliastudios.cameraview.CameraLogger; import com.otaliastudios.cameraview.CameraLogger;
import com.otaliastudios.cameraview.PictureResult; import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.SurfaceDrawer; import com.otaliastudios.cameraview.overlay.SurfaceDrawer;
import com.otaliastudios.cameraview.controls.Facing; import com.otaliastudios.cameraview.controls.Facing;
import com.otaliastudios.cameraview.engine.CameraEngine; import com.otaliastudios.cameraview.engine.CameraEngine;
import com.otaliastudios.cameraview.engine.offset.Axis; import com.otaliastudios.cameraview.engine.offset.Axis;

@ -6,13 +6,12 @@ import android.graphics.PorterDuff;
import android.graphics.SurfaceTexture; import android.graphics.SurfaceTexture;
import android.opengl.EGL14; import android.opengl.EGL14;
import android.os.Build; import android.os.Build;
import android.util.Log;
import android.view.Surface; import android.view.Surface;
import java.util.List; import java.util.List;
import com.otaliastudios.cameraview.CameraLogger; import com.otaliastudios.cameraview.CameraLogger;
import com.otaliastudios.cameraview.SurfaceDrawer; import com.otaliastudios.cameraview.overlay.SurfaceDrawer;
import com.otaliastudios.cameraview.VideoResult; import com.otaliastudios.cameraview.VideoResult;
import com.otaliastudios.cameraview.controls.Audio; import com.otaliastudios.cameraview.controls.Audio;
import com.otaliastudios.cameraview.engine.CameraEngine; import com.otaliastudios.cameraview.engine.CameraEngine;

Loading…
Cancel
Save