Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
feat(preview): add android code
Browse files Browse the repository at this point in the history
  • Loading branch information
jgfidelis committed May 31, 2018
1 parent 9bf9a2e commit 497a703
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 21 deletions.
54 changes: 49 additions & 5 deletions android/src/main/java/com/google/android/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ class Camera1 extends CameraViewImpl implements MediaRecorder.OnInfoListener,
private boolean mIsRecording;

private final SizeMap mPreviewSizes = new SizeMap();

private boolean mIsPreviewActive = false;

private final SizeMap mPictureSizes = new SizeMap();

private Size mPictureSize;

private AspectRatio mAspectRatio;

Expand Down Expand Up @@ -107,6 +111,7 @@ class Camera1 extends CameraViewImpl implements MediaRecorder.OnInfoListener,
public void onSurfaceChanged() {
if (mCamera != null) {
setUpPreview();
mIsPreviewActive = false;
adjustCameraParameters();
}
}
Expand Down Expand Up @@ -164,6 +169,7 @@ void setUpPreview() {
final boolean needsToStopPreview = mShowingPreview && Build.VERSION.SDK_INT < 14;
if (needsToStopPreview) {
mCamera.stopPreview();
mIsPreviewActive = false;
}
mCamera.setPreviewDisplay(mPreview.getSurfaceHolder());
if (needsToStopPreview) {
Expand All @@ -179,10 +185,22 @@ void setUpPreview() {

private void startCameraPreview() {
mCamera.startPreview();
mIsPreviewActive = true;
if (mIsScanning) {
mCamera.setPreviewCallback(this);
}
}

@Override
public void resumePreview() {
startCameraPreview();
}

@Override
public void pausePreview() {
mCamera.stopPreview();
mIsPreviewActive = false;
}

@Override
boolean isCameraOpened() {
Expand Down Expand Up @@ -216,6 +234,25 @@ Set<AspectRatio> getSupportedAspectRatios() {
}
return idealAspectRatios.ratios();
}

@Override
SortedSet<Size> getAvailablePictureSizes(AspectRatio ratio) {
return mPictureSizes.sizes(ratio);
}

@Override
void setPictureSize(Size size) {
mPictureSize = size;
if (mCameraParameters != null && mCamera != null) {
mCameraParameters.setPictureSize(size.getWidth(), size.getHeight());
mCamera.setParameters(mCameraParameters);
}
}

@Override
Size getPictureSize() {
return mPictureSize;
}

@Override
boolean setAspectRatio(AspectRatio ratio) {
Expand Down Expand Up @@ -334,6 +371,9 @@ void takePicture() {
throw new IllegalStateException(
"Camera is not ready. Call start() before takePicture().");
}
if (!mIsPreviewActive) {
throw new IllegalStateException("Preview is paused - resume it before taking a picture.");
}
if (getAutoFocus()) {
mCamera.cancelAutoFocus();
mCamera.autoFocus(new Camera.AutoFocusCallback() {
Expand All @@ -355,6 +395,7 @@ public void onPictureTaken(byte[] data, Camera camera) {
isPictureCaptureInProgress.set(false);
camera.cancelAutoFocus();
camera.startPreview();
mIsPreviewActive = true;
if (mIsScanning) {
camera.setPreviewCallback(Camera1.this);
}
Expand Down Expand Up @@ -403,6 +444,7 @@ void setDisplayOrientation(int displayOrientation) {
final boolean needsToStopPreview = mShowingPreview && Build.VERSION.SDK_INT < 14;
if (needsToStopPreview) {
mCamera.stopPreview();
mIsPreviewActive = false;
}
mCamera.setDisplayOrientation(calcDisplayOrientation(displayOrientation));
if (needsToStopPreview) {
Expand All @@ -420,6 +462,7 @@ public void setPreviewTexture(SurfaceTexture surfaceTexture) {
}

mCamera.stopPreview();
mIsPreviewActive = false;

if (surfaceTexture == null) {
mCamera.setPreviewTexture((SurfaceTexture) mPreview.getSurfaceTexture());
Expand Down Expand Up @@ -504,13 +547,15 @@ void adjustCameraParameters() {
Size size = chooseOptimalSize(sizes);

// Always re-apply camera parameters
// Largest picture size in this ratio
final Size pictureSize = mPictureSizes.sizes(mAspectRatio).last();
if (mPictureSize == null) {
mPictureSize = mPictureSizes.sizes(mAspectRatio).last();
}
if (mShowingPreview) {
mCamera.stopPreview();
mIsPreviewActive = false;
}
mCameraParameters.setPreviewSize(size.getWidth(), size.getHeight());
mCameraParameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
mCameraParameters.setPictureSize(mPictureSize.getWidth(), mPictureSize.getHeight());
mCameraParameters.setRotation(calcCameraRotation(mDisplayOrientation));
setAutoFocusInternal(mAutoFocus);
setFlashInternal(mFlash);
Expand Down Expand Up @@ -555,6 +600,7 @@ private void releaseCamera() {
if (mCamera != null) {
mCamera.release();
mCamera = null;
mPictureSize = null;
mCallback.onCameraClosed();
}
}
Expand Down Expand Up @@ -646,7 +692,6 @@ private boolean setFlashInternal(int flash) {
String currentMode = FLASH_MODES.get(mFlash);
if (modes == null || !modes.contains(currentMode)) {
mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mFlash = Constants.FLASH_OFF;
return true;
}
return false;
Expand Down Expand Up @@ -687,7 +732,6 @@ private boolean setWhiteBalanceInternal(int whiteBalance) {
String currentMode = WB_MODES.get(mWhiteBalance);
if (modes == null || !modes.contains(currentMode)) {
mCameraParameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
mWhiteBalance = Constants.WB_AUTO;
return true;
}
return false;
Expand Down
51 changes: 49 additions & 2 deletions android/src/main/java/com/google/android/cameraview/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public void onImageAvailable(ImageReader reader) {

private final SizeMap mPictureSizes = new SizeMap();

private Size mPictureSize;

private int mFacing;

private AspectRatio mAspectRatio = Constants.DEFAULT_ASPECT_RATIO;
Expand Down Expand Up @@ -345,6 +347,35 @@ Set<AspectRatio> getSupportedAspectRatios() {
return mPreviewSizes.ratios();
}

@Override
SortedSet<Size> getAvailablePictureSizes(AspectRatio ratio) {
return mPictureSizes.sizes(ratio);
}

@Override
void setPictureSize(Size size) {
if (mCaptureSession != null) {
try {
mCaptureSession.stopRepeating();
} catch (CameraAccessException e) {
e.printStackTrace();
}
mCaptureSession.close();
mCaptureSession = null;
}
if (mStillImageReader != null) {
mStillImageReader.close();
}
mPictureSize = size;
prepareStillImageReader();
startCaptureSession();
}

@Override
Size getPictureSize() {
return mPictureSize;
}

@Override
boolean setAspectRatio(AspectRatio ratio) {
if (ratio != null && mPreviewSizes.isEmpty()) {
Expand Down Expand Up @@ -653,6 +684,9 @@ private void collectCameraInfo() {
}
mPictureSizes.clear();
collectPictureSizes(mPictureSizes, map);
if (mPictureSize == null) {
mPictureSize = mPictureSizes.sizes(mAspectRatio).last();
}
for (AspectRatio ratio : mPreviewSizes.ratios()) {
if (!mPictureSizes.ratios().contains(ratio)) {
mPreviewSizes.remove(ratio);
Expand All @@ -674,8 +708,7 @@ private void prepareStillImageReader() {
if (mStillImageReader != null) {
mStillImageReader.close();
}
Size largest = mPictureSizes.sizes(mAspectRatio).last();
mStillImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
mStillImageReader = ImageReader.newInstance(mPictureSize.getWidth(), mPictureSize.getHeight(),
ImageFormat.JPEG, 1);
mStillImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null);
}
Expand Down Expand Up @@ -728,6 +761,20 @@ void startCaptureSession() {
}
}

@Override
public void resumePreview() {
startCaptureSession();
}

@Override
public void pausePreview() {
try {
mCaptureSession.stopRepeating();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

public Surface getPreviewSurface() {
if (mPreviewSurface != null) {
return mPreviewSurface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Set;
import java.util.SortedSet;

public class CameraView extends FrameLayout {

Expand Down Expand Up @@ -219,6 +220,7 @@ protected Parcelable onSaveInstanceState() {
state.zoom = getZoom();
state.whiteBalance = getWhiteBalance();
state.scanning = getScanning();
state.pictureSize = getPictureSize();
return state;
}

Expand All @@ -238,6 +240,7 @@ protected void onRestoreInstanceState(Parcelable state) {
setZoom(ss.zoom);
setWhiteBalance(ss.whiteBalance);
setScanning(ss.scanning);
setPictureSize(ss.pictureSize);
}

public void setUsingCamera2Api(boolean useCamera2) {
Expand Down Expand Up @@ -402,6 +405,31 @@ public void setAspectRatio(@NonNull AspectRatio ratio) {
public AspectRatio getAspectRatio() {
return mImpl.getAspectRatio();
}

/**
* Gets all the picture sizes for particular ratio supported by the current camera.
*
* @param ratio {@link AspectRatio} for which the available image sizes will be returned.
*/
public SortedSet<Size> getAvailablePictureSizes(@NonNull AspectRatio ratio) {
return mImpl.getAvailablePictureSizes(ratio);
}

/**
* Sets the size of taken pictures.
*
* @param size The {@link Size} to be set.
*/
public void setPictureSize(@NonNull Size size) {
mImpl.setPictureSize(size);
}

/**
* Gets the size of pictures that will be taken.
*/
public Size getPictureSize() {
return mImpl.getPictureSize();
}

/**
* Enables or disables the continuous auto-focus mode. When the current camera doesn't support
Expand Down Expand Up @@ -494,6 +522,14 @@ public boolean record(String path, int maxDuration, int maxFileSize,
public void stopRecording() {
mImpl.stopRecording();
}

public void resumePreview() {
mImpl.resumePreview();
}

public void pausePreview() {
mImpl.pausePreview();
}

public void setPreviewTexture(SurfaceTexture surfaceTexture) {
mImpl.setPreviewTexture(surfaceTexture);
Expand Down Expand Up @@ -590,6 +626,8 @@ protected static class SavedState extends BaseSavedState {
int whiteBalance;

boolean scanning;

Size pictureSize;

@SuppressWarnings("WrongConstant")
public SavedState(Parcel source, ClassLoader loader) {
Expand All @@ -602,6 +640,7 @@ public SavedState(Parcel source, ClassLoader loader) {
zoom = source.readFloat();
whiteBalance = source.readInt();
scanning = source.readByte() != 0;
pictureSize = source.readParcelable(loader);
}

public SavedState(Parcelable superState) {
Expand All @@ -619,6 +658,7 @@ public void writeToParcel(Parcel out, int flags) {
out.writeFloat(zoom);
out.writeInt(whiteBalance);
out.writeByte((byte) (scanning ? 1 : 0));
out.writeParcelable(pictureSize, flags);
}

public static final Creator<SavedState> CREATOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.graphics.SurfaceTexture;

import java.util.Set;
import java.util.SortedSet;

abstract class CameraViewImpl {

Expand Down Expand Up @@ -51,6 +52,12 @@ View getView() {
abstract int getFacing();

abstract Set<AspectRatio> getSupportedAspectRatios();

abstract SortedSet<Size> getAvailablePictureSizes(AspectRatio ratio);

abstract void setPictureSize(Size size);

abstract Size getPictureSize();

/**
* @return {@code true} if the aspect ratio was changed.
Expand Down Expand Up @@ -91,6 +98,10 @@ abstract boolean record(String path, int maxDuration, int maxFileSize,
abstract void setScanning(boolean isScanning);

abstract boolean getScanning();

abstract public void resumePreview();

abstract public void pausePreview();

abstract public void setPreviewTexture(SurfaceTexture surfaceTexture);

Expand Down
Loading

1 comment on commit 497a703

@DennisJu007
Copy link

@DennisJu007 DennisJu007 commented on 497a703 Jun 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please help me to reslove the issues #451 @jgfidelis

Please sign in to comment.