From 8154e129f47cafebe06fc5c3cb8ee0c1f3ddd58b Mon Sep 17 00:00:00 2001 From: Dylan McIntyre Date: Wed, 15 Feb 2017 23:55:39 -0500 Subject: [PATCH] PreviewActivity improvements --- .../java/com/flurgle/camerakit/Camera1.java | 23 ++- .../com/flurgle/camerakit/CameraImpl.java | 5 + .../com/flurgle/camerakit/CameraView.java | 9 + .../flurgle/camerakit/demo/MainActivity.java | 37 +--- .../flurgle/camerakit/demo/MediaHolder.java | 37 ---- .../camerakit/demo/PreviewActivity.java | 59 +++--- .../flurgle/camerakit/demo/ResultHolder.java | 49 +++++ demo/src/main/res/layout/activity_preview.xml | 188 +++++++++++++++++- 8 files changed, 303 insertions(+), 104 deletions(-) delete mode 100644 demo/src/main/java/com/flurgle/camerakit/demo/MediaHolder.java create mode 100644 demo/src/main/java/com/flurgle/camerakit/demo/ResultHolder.java diff --git a/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java b/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java index 4c0dc630..c414b9ff 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java @@ -41,6 +41,8 @@ public class Camera1 extends CameraImpl { private Camera mCamera; private Camera.Parameters mCameraParameters; private final Camera.CameraInfo mCameraInfo = new Camera.CameraInfo(); + private Size mPreviewSize; + private Size mCaptureSize; private boolean mShowingPreview; private boolean mAutoFocus; @@ -395,13 +397,13 @@ public class Camera1 extends CameraImpl { private void adjustCameraParameters() { SortedSet sizes = mPreviewSizes; - Size previewSize = chooseOptimalSize(sizes); + mPreviewSize = chooseOptimalSize(sizes); final Camera.Size currentSize = mCameraParameters.getPictureSize(); - if (currentSize.width != previewSize.getWidth() || currentSize.height != previewSize.getHeight()) { + if (currentSize.width != mPreviewSize.getWidth() || currentSize.height != mPreviewSize.getHeight()) { Iterator iterator = mCaptureSizes.descendingIterator(); Size pictureSize; while ((pictureSize = iterator.next()) != null) { - if (AspectRatio.of(previewSize.getWidth(), previewSize.getHeight()).matches(pictureSize)) { + if (AspectRatio.of(mPreviewSize.getWidth(), mPreviewSize.getHeight()).matches(pictureSize)) { break; } } @@ -410,12 +412,14 @@ public class Camera1 extends CameraImpl { pictureSize = mCaptureSizes.last(); } + mCaptureSize = pictureSize; + if (mShowingPreview) { mCamera.stopPreview(); } - mCameraParameters.setPreviewSize(previewSize.getWidth(), previewSize.getHeight()); - mPreview.setTruePreviewSize(previewSize.getWidth(), previewSize.getHeight()); + mCameraParameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); + mPreview.setTruePreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); mCameraParameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight()); mCameraParameters.setRotation(calcCameraRotation(mDisplayOrientation) + (mFacing == CameraKit.Constants.FACING_FRONT ? 180 : 0)); @@ -488,4 +492,13 @@ public class Camera1 extends CameraImpl { } } + @Override + Size getCaptureSize() { + return mCaptureSize; + } + + @Override + Size getPreviewSize() { + return mPreviewSize; + } } diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraImpl.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraImpl.java index 4edbfb1b..1ee663cd 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraImpl.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraImpl.java @@ -4,6 +4,7 @@ import android.view.View; import com.flurgle.camerakit.types.Facing; import com.flurgle.camerakit.types.Flash; +import com.flurgle.camerakit.utils.Size; public abstract class CameraImpl { @@ -45,4 +46,8 @@ public abstract class CameraImpl { abstract void setDisplayOrientation(int displayOrientation); + abstract Size getCaptureSize(); + + abstract Size getPreviewSize(); + } diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index dc59ae80..834f4b96 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -31,6 +31,7 @@ import com.flurgle.camerakit.types.TapToFocus; import com.flurgle.camerakit.utils.AspectRatio; import com.flurgle.camerakit.utils.CenterCrop; import com.flurgle.camerakit.utils.DisplayOrientationDetector; +import com.flurgle.camerakit.utils.Size; import java.io.ByteArrayOutputStream; import java.io.File; @@ -295,6 +296,14 @@ public class CameraView extends FrameLayout { mCameraImpl.endVideo(); } + public Size getPreviewSize() { + return mCameraImpl != null ? mCameraImpl.getPreviewSize() : null; + } + + public Size getCaptureSize() { + return mCameraImpl != null ? mCameraImpl.getCaptureSize() : null; + } + private void requestCameraPermission() { Activity activity = null; Context context = getContext(); diff --git a/demo/src/main/java/com/flurgle/camerakit/demo/MainActivity.java b/demo/src/main/java/com/flurgle/camerakit/demo/MainActivity.java index 19b3af35..a586b1e7 100644 --- a/demo/src/main/java/com/flurgle/camerakit/demo/MainActivity.java +++ b/demo/src/main/java/com/flurgle/camerakit/demo/MainActivity.java @@ -23,7 +23,6 @@ import java.io.File; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; -import butterknife.OnTextChanged; public class MainActivity extends AppCompatActivity { @@ -119,13 +118,20 @@ public class MainActivity extends AppCompatActivity { @OnClick(R.id.capturePhoto) void capturePhoto() { + final long startTime = System.currentTimeMillis(); camera.setCameraListener(new CameraListener() { @Override public void onPictureTaken(byte[] jpeg) { super.onPictureTaken(jpeg); + long callbackTime = System.currentTimeMillis(); Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length); - MediaHolder.dispose(); - MediaHolder.setImage(bitmap); + ResultHolder.dispose(); + ResultHolder.setImage(bitmap); + ResultHolder.setNativeCaptureSize( + captureModeRadioGroup.getCheckedRadioButtonId() == R.id.modeCaptureQuality ? + camera.getCaptureSize() : camera.getPreviewSize() + ); + ResultHolder.setTimeToCallback(callbackTime - startTime); Intent intent = new Intent(MainActivity.this, PreviewActivity.class); startActivity(intent); } @@ -139,10 +145,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onVideoTaken(File video) { super.onVideoTaken(video); - MediaHolder.dispose(); - MediaHolder.setVideo(video); - Intent intent = new Intent(MainActivity.this, PreviewActivity.class); - startActivity(intent); + } }); @@ -224,11 +227,6 @@ public class MainActivity extends AppCompatActivity { } }; - @OnTextChanged(value = R.id.width, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) - void widthChanged() { - - } - @OnClick(R.id.widthUpdate) void widthUpdateClicked() { if (widthUpdate.getAlpha() >= 1) { @@ -249,11 +247,6 @@ public class MainActivity extends AppCompatActivity { } }; - @OnTextChanged(value = R.id.height) - void heightChanged() { - - } - @OnClick(R.id.heightUpdate) void heightUpdateClicked() { if (heightUpdate.getAlpha() >= 1) { @@ -329,16 +322,6 @@ public class MainActivity extends AppCompatActivity { cameraLayoutParams.width = width; cameraLayoutParams.height = height; - camera.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { - @Override - public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { - mCameraWidth = right - left; - mCameraHeight = bottom - top; - camera.removeOnLayoutChangeListener(this); - widthChanged(); - heightChanged(); - } - }); camera.setLayoutParams(cameraLayoutParams); Toast.makeText(this, (updateWidth && updateHeight ? "Width and height" : updateWidth ? "Width" : "Height") + " updated!", Toast.LENGTH_SHORT).show(); diff --git a/demo/src/main/java/com/flurgle/camerakit/demo/MediaHolder.java b/demo/src/main/java/com/flurgle/camerakit/demo/MediaHolder.java deleted file mode 100644 index 5c2119fc..00000000 --- a/demo/src/main/java/com/flurgle/camerakit/demo/MediaHolder.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.flurgle.camerakit.demo; - -import android.graphics.Bitmap; -import android.support.annotation.Nullable; - -import java.io.File; -import java.lang.ref.WeakReference; - -public class MediaHolder { - - private static WeakReference video; - private static WeakReference image; - - public static void setVideo(@Nullable File video) { - MediaHolder.video = video != null ? new WeakReference<>(video) : null; - } - - @Nullable - public static File getVideo() { - return video != null ? video.get() : null; - } - - public static void setImage(@Nullable Bitmap image) { - MediaHolder.image = image != null ? new WeakReference<>(image) : null; - } - - @Nullable - public static Bitmap getImage() { - return image != null ? image.get() : null; - } - - public static void dispose() { - setVideo(null); - setImage(null); - } - -} diff --git a/demo/src/main/java/com/flurgle/camerakit/demo/PreviewActivity.java b/demo/src/main/java/com/flurgle/camerakit/demo/PreviewActivity.java index 00a8894f..f61368d3 100644 --- a/demo/src/main/java/com/flurgle/camerakit/demo/PreviewActivity.java +++ b/demo/src/main/java/com/flurgle/camerakit/demo/PreviewActivity.java @@ -2,27 +2,33 @@ package com.flurgle.camerakit.demo; import android.app.Activity; import android.graphics.Bitmap; -import android.media.MediaPlayer; import android.os.Bundle; import android.support.annotation.Nullable; -import android.view.View; import android.widget.ImageView; -import android.widget.VideoView; +import android.widget.TextView; -import java.io.File; +import com.flurgle.camerakit.utils.AspectRatio; +import com.flurgle.camerakit.utils.Size; import butterknife.BindView; import butterknife.ButterKnife; -import static com.flurgle.camerakit.demo.R.id.video; - public class PreviewActivity extends Activity { @BindView(R.id.image) ImageView imageView; - @BindView(video) - VideoView videoView; + @BindView(R.id.nativeCaptureResolution) + TextView nativeCaptureResolution; + + @BindView(R.id.actualResolution) + TextView actualResolution; + + @BindView(R.id.approxUncompressedSize) + TextView approxUncompressedSize; + + @BindView(R.id.captureLatency) + TextView captureLatency; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -30,23 +36,28 @@ public class PreviewActivity extends Activity { setContentView(R.layout.activity_preview); ButterKnife.bind(this); - Bitmap bitmap = MediaHolder.getImage(); - File video = MediaHolder.getVideo(); - - if (bitmap != null) { - imageView.setImageBitmap(bitmap); - } else if (video != null) { - imageView.setVisibility(View.GONE); - videoView.setVideoPath(video.getAbsolutePath()); - videoView.setMediaController(null); - videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { - @Override - public void onPrepared(MediaPlayer mediaPlayer) { - mediaPlayer.setLooping(true); - } - }); - videoView.start(); + Bitmap bitmap = ResultHolder.getImage(); + if (bitmap == null) { + finish(); + return; + } + + imageView.setImageBitmap(bitmap); + + Size captureSize = ResultHolder.getNativeCaptureSize(); + if (captureSize != null) { + // Native sizes are landscape, hardcode flip because demo app forced to portrait. + AspectRatio aspectRatio = AspectRatio.of(captureSize.getHeight(), captureSize.getWidth()); + nativeCaptureResolution.setText(captureSize.getHeight() + " x " + captureSize.getWidth() + " (" + aspectRatio.toString() + ")"); } + + actualResolution.setText(bitmap.getWidth() + " x " + bitmap.getHeight()); + approxUncompressedSize.setText(getApproximateFileMegabytes(bitmap) + "MB"); + captureLatency.setText(ResultHolder.getTimeToCallback() + " milliseconds"); + } + + private static float getApproximateFileMegabytes(Bitmap bitmap) { + return (bitmap.getRowBytes() * bitmap.getHeight()) / 1024 / 1024; } } diff --git a/demo/src/main/java/com/flurgle/camerakit/demo/ResultHolder.java b/demo/src/main/java/com/flurgle/camerakit/demo/ResultHolder.java new file mode 100644 index 00000000..46be0ad9 --- /dev/null +++ b/demo/src/main/java/com/flurgle/camerakit/demo/ResultHolder.java @@ -0,0 +1,49 @@ +package com.flurgle.camerakit.demo; + +import android.graphics.Bitmap; +import android.support.annotation.Nullable; + +import com.flurgle.camerakit.utils.Size; + +import java.lang.ref.WeakReference; + +public class ResultHolder { + + private static WeakReference image; + private static Size nativeCaptureSize; + private static long timeToCallback; + + + public static void setImage(@Nullable Bitmap image) { + ResultHolder.image = image != null ? new WeakReference<>(image) : null; + } + + @Nullable + public static Bitmap getImage() { + return image != null ? image.get() : null; + } + + public static void setNativeCaptureSize(@Nullable Size nativeCaptureSize) { + ResultHolder.nativeCaptureSize = nativeCaptureSize; + } + + @Nullable + public static Size getNativeCaptureSize() { + return nativeCaptureSize; + } + + public static void setTimeToCallback(long timeToCallback) { + ResultHolder.timeToCallback = timeToCallback; + } + + public static long getTimeToCallback() { + return timeToCallback; + } + + public static void dispose() { + setImage(null); + setNativeCaptureSize(null); + setTimeToCallback(0); + } + +} diff --git a/demo/src/main/res/layout/activity_preview.xml b/demo/src/main/res/layout/activity_preview.xml index b2b7d26c..221e29fd 100644 --- a/demo/src/main/res/layout/activity_preview.xml +++ b/demo/src/main/res/layout/activity_preview.xml @@ -1,17 +1,183 @@ - + android:layout_height="match_parent" + android:background="@android:color/white"> - + android:layout_height="wrap_content" + android:orientation="vertical"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file + \ No newline at end of file