|
|
@ -8,11 +8,10 @@ import android.media.MediaRecorder; |
|
|
|
import android.os.Build; |
|
|
|
import android.os.Build; |
|
|
|
import android.os.Handler; |
|
|
|
import android.os.Handler; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
import android.support.annotation.Nullable; |
|
|
|
import android.support.annotation.Nullable; |
|
|
|
|
|
|
|
import android.util.Log; |
|
|
|
import android.view.MotionEvent; |
|
|
|
import android.view.MotionEvent; |
|
|
|
import android.view.SurfaceHolder; |
|
|
|
import android.view.SurfaceHolder; |
|
|
|
import android.view.View; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
@ -38,9 +37,7 @@ class Camera1 extends CameraImpl { |
|
|
|
|
|
|
|
|
|
|
|
private int mCameraId; |
|
|
|
private int mCameraId; |
|
|
|
private Camera mCamera; |
|
|
|
private Camera mCamera; |
|
|
|
private Camera.Parameters mCameraParameters; |
|
|
|
|
|
|
|
private ExtraProperties mExtraProperties; |
|
|
|
private ExtraProperties mExtraProperties; |
|
|
|
private Camera.CameraInfo mCameraInfo; |
|
|
|
|
|
|
|
private Size mPreviewSize; |
|
|
|
private Size mPreviewSize; |
|
|
|
private Size mCaptureSize; |
|
|
|
private Size mCaptureSize; |
|
|
|
private MediaRecorder mMediaRecorder; |
|
|
|
private MediaRecorder mMediaRecorder; |
|
|
@ -63,48 +60,60 @@ class Camera1 extends CameraImpl { |
|
|
|
private double mLongitude; |
|
|
|
private double mLongitude; |
|
|
|
private boolean mFocusOnTap; |
|
|
|
private boolean mFocusOnTap; |
|
|
|
|
|
|
|
|
|
|
|
private Handler mHandler = new Handler(); |
|
|
|
private Handler mFocusHandler = new Handler(); |
|
|
|
private ConstantMapper.MapperImpl mMapper = new ConstantMapper.Mapper1(); |
|
|
|
private ConstantMapper.MapperImpl mMapper = new ConstantMapper.Mapper1(); |
|
|
|
|
|
|
|
private boolean mIsSetup = false; |
|
|
|
|
|
|
|
private final Object mLock = new Object(); |
|
|
|
|
|
|
|
|
|
|
|
Camera1(CameraView.CameraListenerWrapper callback, PreviewImpl preview) { |
|
|
|
|
|
|
|
|
|
|
|
Camera1(CameraView.CameraListenerWrapper callback, final PreviewImpl preview) { |
|
|
|
super(callback, preview); |
|
|
|
super(callback, preview); |
|
|
|
preview.setCallback(new PreviewImpl.OnPreviewSurfaceChangedCallback() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onPreviewSurfaceChanged() { |
|
|
|
|
|
|
|
if (mCamera != null) { |
|
|
|
|
|
|
|
setupPreview(); |
|
|
|
|
|
|
|
computeCameraSizes(); |
|
|
|
|
|
|
|
adjustCameraParameters(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mCameraInfo = new Camera.CameraInfo(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Preview surface is now available. If camera is open, set up. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onSurfaceAvailable() { |
|
|
|
|
|
|
|
Log.e(TAG, "onSurfaceAvailable, size is "+mPreview.getSurfaceSize()); |
|
|
|
|
|
|
|
if (shouldSetup()) setup(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// CameraImpl:
|
|
|
|
/** |
|
|
|
|
|
|
|
* Preview surface did change its size. Compute a new preview size. |
|
|
|
|
|
|
|
* This requires stopping and restarting the preview. |
|
|
|
|
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void start() { |
|
|
|
public void onSurfaceChanged() { |
|
|
|
setFacing(mFacing); |
|
|
|
Log.e(TAG, "onSurfaceChanged, size is "+mPreview.getSurfaceSize()); |
|
|
|
openCamera(); |
|
|
|
if (mIsSetup) { |
|
|
|
if (mPreview.isReady()) setupPreview(); |
|
|
|
// Compute a new camera preview size.
|
|
|
|
|
|
|
|
Size newSize = computePreviewSize(); |
|
|
|
|
|
|
|
if (!newSize.equals(mPreviewSize)) { |
|
|
|
|
|
|
|
mPreviewSize = newSize; |
|
|
|
|
|
|
|
synchronized (mLock) { |
|
|
|
|
|
|
|
mCamera.stopPreview(); |
|
|
|
|
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
|
|
|
|
params.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); |
|
|
|
|
|
|
|
mCamera.setParameters(params); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
boolean invertPreviewSizes = shouldFlipSizes(); |
|
|
|
|
|
|
|
mPreview.setDesiredSize( |
|
|
|
|
|
|
|
invertPreviewSizes ? mPreviewSize.getHeight() : mPreviewSize.getWidth(), |
|
|
|
|
|
|
|
invertPreviewSizes ? mPreviewSize.getWidth() : mPreviewSize.getHeight() |
|
|
|
|
|
|
|
); |
|
|
|
mCamera.startPreview(); |
|
|
|
mCamera.startPreview(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
private boolean shouldSetup() { |
|
|
|
void stop() { |
|
|
|
return isCameraOpened() && mPreview.isReady() && !mIsSetup; |
|
|
|
if (mCamera != null) mCamera.stopPreview(); |
|
|
|
|
|
|
|
mHandler.removeCallbacksAndMessages(null); |
|
|
|
|
|
|
|
releaseCamera(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
// The act of binding an "open" camera to a "ready" preview.
|
|
|
|
* Sets the output stream for our preview. |
|
|
|
// These can happen at different times but we want to end up here.
|
|
|
|
* To be called when preview is ready. |
|
|
|
private void setup() { |
|
|
|
*/ |
|
|
|
|
|
|
|
private void setupPreview() { |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
if (mPreview.getOutputClass() == SurfaceHolder.class) { |
|
|
|
if (mPreview.getOutputClass() == SurfaceHolder.class) { |
|
|
|
mCamera.setPreviewDisplay(mPreview.getSurfaceHolder()); |
|
|
|
mCamera.setPreviewDisplay(mPreview.getSurfaceHolder()); |
|
|
@ -114,9 +123,79 @@ class Camera1 extends CameraImpl { |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean invertPreviewSizes = shouldFlipSizes(); // mDisplayOffset % 180 != 0;
|
|
|
|
|
|
|
|
mCaptureSize = computeCaptureSize(); |
|
|
|
|
|
|
|
mPreviewSize = computePreviewSize(); |
|
|
|
|
|
|
|
mPreview.setDesiredSize( |
|
|
|
|
|
|
|
invertPreviewSizes ? mPreviewSize.getHeight() : mPreviewSize.getWidth(), |
|
|
|
|
|
|
|
invertPreviewSizes ? mPreviewSize.getWidth() : mPreviewSize.getHeight() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
synchronized (mLock) { |
|
|
|
|
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
|
|
|
|
params.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); |
|
|
|
|
|
|
|
params.setPictureSize(mCaptureSize.getWidth(), mCaptureSize.getHeight()); // <- not allowed during preview
|
|
|
|
|
|
|
|
mCamera.setParameters(params); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mCamera.startPreview(); |
|
|
|
|
|
|
|
mIsSetup = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
void start() { |
|
|
|
|
|
|
|
if (isCameraOpened()) stop(); |
|
|
|
|
|
|
|
if (collectCameraId()) { |
|
|
|
|
|
|
|
mCamera = Camera.open(mCameraId); |
|
|
|
|
|
|
|
mCameraListener.onCameraOpened(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set parameters that might have been set before the camera was opened.
|
|
|
|
|
|
|
|
synchronized (mLock) { |
|
|
|
|
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
|
|
|
|
mergeFocus(params, mFocus); |
|
|
|
|
|
|
|
mergeFlash(params, mFlash); |
|
|
|
|
|
|
|
mergeLocation(params, mLatitude, mLongitude); |
|
|
|
|
|
|
|
mergeWhiteBalance(params, mWhiteBalance); |
|
|
|
|
|
|
|
params.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO); |
|
|
|
|
|
|
|
mCamera.setParameters(params); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Try starting preview.
|
|
|
|
|
|
|
|
mCamera.setDisplayOrientation(computeCameraToDisplayOffset()); // <- not allowed during preview
|
|
|
|
|
|
|
|
if (shouldSetup()) setup(); |
|
|
|
|
|
|
|
collectExtraProperties(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
void stop() { |
|
|
|
|
|
|
|
mFocusHandler.removeCallbacksAndMessages(null); |
|
|
|
|
|
|
|
if (isCameraOpened()) { |
|
|
|
|
|
|
|
mCamera.stopPreview(); |
|
|
|
|
|
|
|
mCamera.release(); |
|
|
|
|
|
|
|
mCameraListener.onCameraClosed(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mCamera = null; |
|
|
|
|
|
|
|
mPreviewSize = null; |
|
|
|
|
|
|
|
mCaptureSize = null; |
|
|
|
|
|
|
|
mIsSetup = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean collectCameraId() { |
|
|
|
|
|
|
|
int internalFacing = mMapper.mapFacing(mFacing); |
|
|
|
|
|
|
|
Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); |
|
|
|
|
|
|
|
for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { |
|
|
|
|
|
|
|
Camera.getCameraInfo(i, cameraInfo); |
|
|
|
|
|
|
|
if (cameraInfo.facing == internalFacing) { |
|
|
|
|
|
|
|
mSensorOffset = cameraInfo.orientation; |
|
|
|
|
|
|
|
mCameraId = i; |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void onDisplayOffset(int displayOrientation) { |
|
|
|
void onDisplayOffset(int displayOrientation) { |
|
|
|
// I doubt this will ever change.
|
|
|
|
// I doubt this will ever change.
|
|
|
@ -134,7 +213,6 @@ class Camera1 extends CameraImpl { |
|
|
|
if (sessionType != mSessionType) { |
|
|
|
if (sessionType != mSessionType) { |
|
|
|
mSessionType = sessionType; |
|
|
|
mSessionType = sessionType; |
|
|
|
if (isCameraOpened()) { |
|
|
|
if (isCameraOpened()) { |
|
|
|
stop(); |
|
|
|
|
|
|
|
start(); |
|
|
|
start(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -142,75 +220,83 @@ class Camera1 extends CameraImpl { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void setLocation(double latitude, double longitude) { |
|
|
|
void setLocation(double latitude, double longitude) { |
|
|
|
|
|
|
|
double oldLat = mLatitude; |
|
|
|
|
|
|
|
double oldLong = mLongitude; |
|
|
|
mLatitude = latitude; |
|
|
|
mLatitude = latitude; |
|
|
|
mLongitude = longitude; |
|
|
|
mLongitude = longitude; |
|
|
|
if (mCameraParameters != null) { |
|
|
|
if (isCameraOpened()) { |
|
|
|
// Sometimes this will fail... I have no idea why.
|
|
|
|
synchronized (mLock) { |
|
|
|
// Since native_setParameters is quite a black box, there's nothing we can do about it.
|
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
try { |
|
|
|
if (mergeLocation(params, oldLat, oldLong)) mCamera.setParameters(params); |
|
|
|
mCameraParameters.setGpsLatitude(latitude); |
|
|
|
|
|
|
|
mCameraParameters.setGpsLongitude(longitude); |
|
|
|
|
|
|
|
// mCameraParameters.setGpsAltitude(0);
|
|
|
|
|
|
|
|
// mCameraParameters.setGpsTimestamp(System.currentTimeMillis());
|
|
|
|
|
|
|
|
// mCameraParameters.setGpsProcessingMethod("GPS");
|
|
|
|
|
|
|
|
mCamera.setParameters(mCameraParameters); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
// Reset or everything after will throw as well.
|
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
mCameraParameters = mCamera.getParameters(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
private boolean mergeLocation(Camera.Parameters params, double oldLatitude, double oldLongitude) { |
|
|
|
void setFacing(@Facing int facing) { |
|
|
|
if (mLatitude != 0 && mLongitude != 0) { |
|
|
|
int internalFacing = mMapper.mapFacing(facing); |
|
|
|
params.setGpsLatitude(mLatitude); |
|
|
|
for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { |
|
|
|
params.setGpsLongitude(mLongitude); |
|
|
|
Camera.getCameraInfo(i, mCameraInfo); |
|
|
|
params.setGpsTimestamp(System.currentTimeMillis()); |
|
|
|
if (mCameraInfo.facing == internalFacing) { |
|
|
|
params.setGpsProcessingMethod("Unknown"); |
|
|
|
mSensorOffset = mCameraInfo.orientation; |
|
|
|
|
|
|
|
mCameraId = i; |
|
|
|
|
|
|
|
mFacing = facing; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mFacing == facing && isCameraOpened()) { |
|
|
|
@Override |
|
|
|
stop(); |
|
|
|
void setFacing(@Facing int facing) { |
|
|
|
|
|
|
|
if (facing != mFacing) { |
|
|
|
|
|
|
|
mFacing = facing; |
|
|
|
|
|
|
|
if (collectCameraId() && isCameraOpened()) { |
|
|
|
start(); |
|
|
|
start(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void setWhiteBalance(@WhiteBalance int whiteBalance) { |
|
|
|
void setWhiteBalance(@WhiteBalance int whiteBalance) { |
|
|
|
int old = mWhiteBalance; |
|
|
|
int old = mWhiteBalance; |
|
|
|
mWhiteBalance = whiteBalance; |
|
|
|
mWhiteBalance = whiteBalance; |
|
|
|
if (mCameraParameters != null) { |
|
|
|
if (isCameraOpened()) { |
|
|
|
List<String> supported = mCameraParameters.getSupportedWhiteBalance(); |
|
|
|
synchronized (mLock) { |
|
|
|
String internal = mMapper.mapWhiteBalance(whiteBalance); |
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
if (supported != null && supported.contains(internal)) { |
|
|
|
if (mergeWhiteBalance(params, old)) mCamera.setParameters(params); |
|
|
|
mCameraParameters.setWhiteBalance(internal); |
|
|
|
} |
|
|
|
mCamera.setParameters(mCameraParameters); |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
mWhiteBalance = old; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mergeWhiteBalance(Camera.Parameters params, @WhiteBalance int oldWhiteBalance) { |
|
|
|
|
|
|
|
List<String> supported = params.getSupportedWhiteBalance(); |
|
|
|
|
|
|
|
String internal = mMapper.mapWhiteBalance(mWhiteBalance); |
|
|
|
|
|
|
|
if (supported != null && supported.contains(internal)) { |
|
|
|
|
|
|
|
params.setWhiteBalance(internal); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
mWhiteBalance = oldWhiteBalance; |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void setFlash(@Flash int flash) { |
|
|
|
void setFlash(@Flash int flash) { |
|
|
|
int old = mFlash; |
|
|
|
int old = mFlash; |
|
|
|
mFlash = flash; |
|
|
|
mFlash = flash; |
|
|
|
if (mCameraParameters != null) { |
|
|
|
if (isCameraOpened()) { |
|
|
|
List<String> flashes = mCameraParameters.getSupportedFlashModes(); |
|
|
|
synchronized (mLock) { |
|
|
|
String internalFlash = mMapper.mapFlash(flash); |
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
if (flashes != null && flashes.contains(internalFlash)) { |
|
|
|
if (mergeFlash(params, old)) mCamera.setParameters(params); |
|
|
|
mCameraParameters.setFlashMode(internalFlash); |
|
|
|
} |
|
|
|
mCamera.setParameters(mCameraParameters); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
mFlash = old; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mergeFlash(Camera.Parameters params, @Flash int oldFlash) { |
|
|
|
|
|
|
|
List<String> flashes = params.getSupportedFlashModes(); |
|
|
|
|
|
|
|
String internalFlash = mMapper.mapFlash(mFlash); |
|
|
|
|
|
|
|
if (flashes != null && flashes.contains(internalFlash)) { |
|
|
|
|
|
|
|
params.setFlashMode(internalFlash); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mFlash = oldFlash; |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -218,43 +304,45 @@ class Camera1 extends CameraImpl { |
|
|
|
void setFocus(@Focus int focus) { |
|
|
|
void setFocus(@Focus int focus) { |
|
|
|
int old = mFocus; |
|
|
|
int old = mFocus; |
|
|
|
mFocus = focus; |
|
|
|
mFocus = focus; |
|
|
|
if (mCameraParameters == null) return; |
|
|
|
if (isCameraOpened()) { |
|
|
|
switch (focus) { |
|
|
|
synchronized (mLock) { |
|
|
|
case FOCUS_CONTINUOUS: |
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
mFocusOnTap = false; |
|
|
|
if (mergeFocus(params, old)) mCamera.setParameters(params); |
|
|
|
final List<String> modes = mCameraParameters.getSupportedFocusModes(); |
|
|
|
|
|
|
|
if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
|
|
|
|
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
mFocus = old; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
mFocusOnTap = mFocus == FOCUS_TAP || mFocus == FOCUS_TAP_WITH_MARKER; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mergeFocus(Camera.Parameters params, @Focus int oldFocus) { |
|
|
|
|
|
|
|
final List<String> modes = params.getSupportedFocusModes(); |
|
|
|
|
|
|
|
switch (mFocus) { |
|
|
|
|
|
|
|
case FOCUS_CONTINUOUS: |
|
|
|
case FOCUS_TAP: |
|
|
|
case FOCUS_TAP: |
|
|
|
case FOCUS_TAP_WITH_MARKER: |
|
|
|
case FOCUS_TAP_WITH_MARKER: |
|
|
|
mFocusOnTap = true; |
|
|
|
if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
|
|
|
final List<String> modes1 = mCameraParameters.getSupportedFocusModes(); |
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
|
if (modes1.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
|
|
|
return true; |
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
mFocus = old; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
mFocus = oldFocus; |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
case FOCUS_OFF: |
|
|
|
case FOCUS_OFF: |
|
|
|
mFocusOnTap = false; |
|
|
|
if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { |
|
|
|
final List<String> modes2 = mCameraParameters.getSupportedFocusModes(); |
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); |
|
|
|
if (modes2.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { |
|
|
|
return true; |
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); |
|
|
|
} else if (modes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) { |
|
|
|
} else if (modes2.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) { |
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); |
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
} else if (modes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { |
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); |
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void setZoom(@ZoomMode int zoom) { |
|
|
|
void setZoom(@ZoomMode int zoom) { |
|
|
|
this.mZoom = zoom; |
|
|
|
this.mZoom = zoom; |
|
|
@ -263,26 +351,29 @@ class Camera1 extends CameraImpl { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void setVideoQuality(int videoQuality) { |
|
|
|
void setVideoQuality(int videoQuality) { |
|
|
|
this.mVideoQuality = videoQuality; |
|
|
|
this.mVideoQuality = videoQuality; |
|
|
|
|
|
|
|
// TODO: restore preview size if needed.
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
void captureImage() { |
|
|
|
void captureImage() { |
|
|
|
if (isCapturingImage) return; |
|
|
|
if (isCapturingImage) return; |
|
|
|
if (mCamera == null) return; |
|
|
|
if (!isCameraOpened()) return; |
|
|
|
switch (mSessionType) { |
|
|
|
switch (mSessionType) { |
|
|
|
case SESSION_TYPE_PICTURE: |
|
|
|
case SESSION_TYPE_PICTURE: |
|
|
|
// Set boolean to wait for image callback
|
|
|
|
// Set boolean to wait for image callback
|
|
|
|
isCapturingImage = true; |
|
|
|
isCapturingImage = true; |
|
|
|
|
|
|
|
synchronized (mLock) { |
|
|
|
Camera.Parameters parameters = mCamera.getParameters(); |
|
|
|
Camera.Parameters parameters = mCamera.getParameters(); |
|
|
|
parameters.setRotation(computeExifOrientation()); |
|
|
|
parameters.setRotation(computeExifOrientation()); |
|
|
|
mCamera.setParameters(parameters); |
|
|
|
mCamera.setParameters(parameters); |
|
|
|
|
|
|
|
} |
|
|
|
mCamera.takePicture(null, null, null, |
|
|
|
mCamera.takePicture(null, null, null, |
|
|
|
new Camera.PictureCallback() { |
|
|
|
new Camera.PictureCallback() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onPictureTaken(byte[] data, Camera camera) { |
|
|
|
public void onPictureTaken(byte[] data, Camera camera) { |
|
|
|
mCameraListener.onPictureTaken(data); |
|
|
|
mCameraListener.onPictureTaken(data); |
|
|
|
isCapturingImage = false; |
|
|
|
isCapturingImage = false; |
|
|
|
camera.startPreview(); // TODO: is this needed? why?
|
|
|
|
camera.startPreview(); // This is needed, read somewhere in the docs.
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
break; |
|
|
|
break; |
|
|
@ -297,13 +388,14 @@ class Camera1 extends CameraImpl { |
|
|
|
public void onPreviewFrame(final byte[] data, Camera camera) { |
|
|
|
public void onPreviewFrame(final byte[] data, Camera camera) { |
|
|
|
// Got to rotate the preview frame, since byte[] data here does not include
|
|
|
|
// Got to rotate the preview frame, since byte[] data here does not include
|
|
|
|
// EXIF tags automatically set by camera. So either we add EXIF, or we rotate.
|
|
|
|
// EXIF tags automatically set by camera. So either we add EXIF, or we rotate.
|
|
|
|
|
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
final int rotation = computeExifOrientation(); |
|
|
|
final int rotation = computeExifOrientation(); |
|
|
|
final boolean flip = rotation % 180 != 0; |
|
|
|
final boolean flip = rotation % 180 != 0; |
|
|
|
final int preWidth = mPreviewSize.getWidth(); |
|
|
|
final int preWidth = mPreviewSize.getWidth(); |
|
|
|
final int preHeight = mPreviewSize.getHeight(); |
|
|
|
final int preHeight = mPreviewSize.getHeight(); |
|
|
|
final int postWidth = flip ? preHeight : preWidth; |
|
|
|
final int postWidth = flip ? preHeight : preWidth; |
|
|
|
final int postHeight = flip ? preWidth : preHeight; |
|
|
|
final int postHeight = flip ? preWidth : preHeight; |
|
|
|
final int format = mCameraParameters.getPreviewFormat(); |
|
|
|
final int format = params.getPreviewFormat(); |
|
|
|
new Thread(new Runnable() { |
|
|
|
new Thread(new Runnable() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
@ -347,38 +439,13 @@ class Camera1 extends CameraImpl { |
|
|
|
|
|
|
|
|
|
|
|
// Internal:
|
|
|
|
// Internal:
|
|
|
|
|
|
|
|
|
|
|
|
private void openCamera() { |
|
|
|
|
|
|
|
if (mCamera != null) { |
|
|
|
|
|
|
|
releaseCamera(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mCamera = Camera.open(mCameraId); |
|
|
|
|
|
|
|
mCameraParameters = mCamera.getParameters(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
collectCameraProperties(); |
|
|
|
|
|
|
|
computeCameraSizes(); |
|
|
|
|
|
|
|
adjustCameraParameters(); |
|
|
|
|
|
|
|
mCamera.setDisplayOrientation(computeCameraToDisplayOffset()); |
|
|
|
|
|
|
|
mCameraListener.onCameraOpened(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void releaseCamera() { |
|
|
|
|
|
|
|
if (mCamera != null) { |
|
|
|
|
|
|
|
mCamera.release(); |
|
|
|
|
|
|
|
mCamera = null; |
|
|
|
|
|
|
|
mCameraParameters = null; |
|
|
|
|
|
|
|
mPreviewSize = null; |
|
|
|
|
|
|
|
mCaptureSize = null; |
|
|
|
|
|
|
|
mCameraListener.onCameraClosed(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns how much should the sensor image be rotated before being shown. |
|
|
|
* Returns how much should the sensor image be rotated before being shown. |
|
|
|
* It is meant to be fed to Camera.setDisplayOrientation(). |
|
|
|
* It is meant to be fed to Camera.setDisplayOrientation(). |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private int computeCameraToDisplayOffset() { |
|
|
|
private int computeCameraToDisplayOffset() { |
|
|
|
if (mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { |
|
|
|
if (mFacing == CameraKit.Constants.FACING_FRONT) { |
|
|
|
// or: (360 - ((info.orientation + displayOrientation) % 360)) % 360;
|
|
|
|
// or: (360 - ((info.orientation + displayOrientation) % 360)) % 360;
|
|
|
|
return ((mSensorOffset - mDisplayOffset) + 360 + 180) % 360; |
|
|
|
return ((mSensorOffset - mDisplayOffset) + 360 + 180) % 360; |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -399,45 +466,35 @@ class Camera1 extends CameraImpl { |
|
|
|
* This is called either on cameraView.start(), or when the underlying surface changes. |
|
|
|
* This is called either on cameraView.start(), or when the underlying surface changes. |
|
|
|
* It is possible that in the first call the preview surface has not already computed its |
|
|
|
* It is possible that in the first call the preview surface has not already computed its |
|
|
|
* dimensions. |
|
|
|
* dimensions. |
|
|
|
* But when it does, the {@link PreviewImpl.OnPreviewSurfaceChangedCallback} should be called, |
|
|
|
* But when it does, the {@link PreviewImpl.SurfaceCallback} should be called, |
|
|
|
* and this should be refreshed. |
|
|
|
* and this should be refreshed. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void computeCameraSizes() { |
|
|
|
private Size computeCaptureSize() { |
|
|
|
mCameraParameters.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO); |
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
List<Size> previewSizes = sizesFromList(mCameraParameters.getSupportedPreviewSizes()); |
|
|
|
|
|
|
|
if (mSessionType == SESSION_TYPE_PICTURE) { |
|
|
|
if (mSessionType == SESSION_TYPE_PICTURE) { |
|
|
|
// Choose the max size.
|
|
|
|
// Choose the max size.
|
|
|
|
List<Size> captureSizes = sizesFromList(mCameraParameters.getSupportedPictureSizes()); |
|
|
|
List<Size> captureSizes = sizesFromList(params.getSupportedPictureSizes()); |
|
|
|
mCaptureSize = Collections.max(captureSizes); |
|
|
|
return Collections.max(captureSizes); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// Choose according to developer choice in setVideoQuality.
|
|
|
|
// Choose according to developer choice in setVideoQuality.
|
|
|
|
// The Camcorder internally checks for cameraParameters.getSupportedVideoSizes() etc.
|
|
|
|
// The Camcorder internally checks for cameraParameters.getSupportedVideoSizes() etc.
|
|
|
|
// So its output is our output.
|
|
|
|
// So its output is our output.
|
|
|
|
CamcorderProfile camcorderProfile = getCamcorderProfile(mVideoQuality); |
|
|
|
CamcorderProfile camcorderProfile = getCamcorderProfile(mVideoQuality); |
|
|
|
mCaptureSize = new Size(camcorderProfile.videoFrameWidth, camcorderProfile.videoFrameHeight); |
|
|
|
return new Size(camcorderProfile.videoFrameWidth, camcorderProfile.videoFrameHeight); |
|
|
|
} |
|
|
|
} |
|
|
|
mPreviewSize = computePreviewSize(previewSizes, mCaptureSize, mPreview.getSurfaceSize()); |
|
|
|
|
|
|
|
// Log.e("Camera1", "CaptureSize is "+mCaptureSize.toString());
|
|
|
|
|
|
|
|
// Log.e("Camera1", "PreviewSize is "+mPreviewSize.toString());
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void adjustCameraParameters() { |
|
|
|
private Size computePreviewSize() { |
|
|
|
boolean invertPreviewSizes = shouldFlipSizes(); // mDisplayOffset % 180 != 0;
|
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
mPreview.setDesiredSize( |
|
|
|
List<Size> previewSizes = sizesFromList(params.getSupportedPreviewSizes()); |
|
|
|
invertPreviewSizes ? getPreviewSize().getHeight() : getPreviewSize().getWidth(), |
|
|
|
return computePreviewSize(previewSizes, mCaptureSize, mPreview.getSurfaceSize()); |
|
|
|
invertPreviewSizes ? getPreviewSize().getWidth() : getPreviewSize().getHeight() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
mCameraParameters.setPreviewSize(getPreviewSize().getWidth(), getPreviewSize().getHeight()); |
|
|
|
|
|
|
|
mCameraParameters.setPictureSize(getCaptureSize().getWidth(), getCaptureSize().getHeight()); |
|
|
|
|
|
|
|
setFocus(mFocus); |
|
|
|
|
|
|
|
setFlash(mFlash); |
|
|
|
|
|
|
|
setLocation(mLatitude, mLongitude); |
|
|
|
|
|
|
|
mCamera.setParameters(mCameraParameters); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void collectCameraProperties() { |
|
|
|
|
|
|
|
mExtraProperties = new ExtraProperties(mCameraParameters.getVerticalViewAngle(), |
|
|
|
private void collectExtraProperties() { |
|
|
|
mCameraParameters.getHorizontalViewAngle()); |
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
|
|
|
|
mExtraProperties = new ExtraProperties(params.getVerticalViewAngle(), |
|
|
|
|
|
|
|
params.getHorizontalViewAngle()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -546,15 +603,17 @@ class Camera1 extends CameraImpl { |
|
|
|
if (!mFocusOnTap) return; |
|
|
|
if (!mFocusOnTap) return; |
|
|
|
if (mCamera == null) return; |
|
|
|
if (mCamera == null) return; |
|
|
|
if (event.getAction() != MotionEvent.ACTION_UP) return; |
|
|
|
if (event.getAction() != MotionEvent.ACTION_UP) return; |
|
|
|
Camera.Parameters parameters = mCamera.getParameters(); |
|
|
|
|
|
|
|
Rect rect = calculateFocusArea(event.getX(), event.getY()); |
|
|
|
Rect rect = calculateFocusArea(event.getX(), event.getY()); |
|
|
|
List<Camera.Area> meteringAreas = new ArrayList<>(); |
|
|
|
List<Camera.Area> meteringAreas = new ArrayList<>(); |
|
|
|
meteringAreas.add(new Camera.Area(rect, getFocusMeteringAreaWeight())); |
|
|
|
meteringAreas.add(new Camera.Area(rect, getFocusMeteringAreaWeight())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (mLock) { |
|
|
|
|
|
|
|
Camera.Parameters parameters = mCamera.getParameters(); |
|
|
|
boolean autofocusSupported = parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO); |
|
|
|
boolean autofocusSupported = parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO); |
|
|
|
if (autofocusSupported) { |
|
|
|
if (autofocusSupported) { |
|
|
|
if (parameters.getMaxNumFocusAreas() > 0) parameters.setFocusAreas(meteringAreas); |
|
|
|
if (parameters.getMaxNumFocusAreas() > 0) parameters.setFocusAreas(meteringAreas); |
|
|
|
if (parameters.getMaxNumMeteringAreas() > 0) parameters.setMeteringAreas(meteringAreas); |
|
|
|
if (parameters.getMaxNumMeteringAreas() > 0) |
|
|
|
|
|
|
|
parameters.setMeteringAreas(meteringAreas); |
|
|
|
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); |
|
|
|
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); |
|
|
|
mCamera.setParameters(parameters); |
|
|
|
mCamera.setParameters(parameters); |
|
|
|
mCamera.autoFocus(new Camera.AutoFocusCallback() { |
|
|
|
mCamera.autoFocus(new Camera.AutoFocusCallback() { |
|
|
@ -565,6 +624,7 @@ class Camera1 extends CameraImpl { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int getFocusAreaSize() { |
|
|
|
private int getFocusAreaSize() { |
|
|
@ -578,18 +638,20 @@ class Camera1 extends CameraImpl { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void resetFocus(final boolean success, final Camera camera) { |
|
|
|
private void resetFocus(final boolean success, final Camera camera) { |
|
|
|
mHandler.removeCallbacksAndMessages(null); |
|
|
|
mFocusHandler.removeCallbacksAndMessages(null); |
|
|
|
mHandler.postDelayed(new Runnable() { |
|
|
|
mFocusHandler.postDelayed(new Runnable() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
if (camera != null) { |
|
|
|
if (camera != null) { |
|
|
|
camera.cancelAutoFocus(); |
|
|
|
camera.cancelAutoFocus(); |
|
|
|
|
|
|
|
synchronized (mLock) { |
|
|
|
Camera.Parameters params = camera.getParameters(); |
|
|
|
Camera.Parameters params = camera.getParameters(); |
|
|
|
if (!params.getFocusMode().equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
|
|
|
if (!params.getFocusMode().equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
|
params.setFocusAreas(null); |
|
|
|
params.setFocusAreas(null); |
|
|
|
params.setMeteringAreas(null); |
|
|
|
params.setMeteringAreas(null); |
|
|
|
camera.setParameters(params); |
|
|
|
mCamera.setParameters(params); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mAutofocusCallback != null) mAutofocusCallback.onAutoFocus(success, camera); |
|
|
|
if (mAutofocusCallback != null) mAutofocusCallback.onAutoFocus(success, camera); |
|
|
|