Fix location bug, add setLocation(Location) (#10)

pull/11/head^2
Mattia Iavarone 7 years ago committed by GitHub
parent 3c564d0c1d
commit 4e5c772902
  1. 1
      README.md
  2. 37
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  3. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java
  4. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  5. 18
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  6. 18
      demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java

@ -405,6 +405,7 @@ Other APIs not mentioned above are provided, and are well documented and comment
|`setExposureCorrection(float)`, `getExposureCorrection()`|Sets exposure compensation EV value, in camera stops. No-op if this is not supported. Should be between the bounds returned by CameraOptions.|
|`toggleFacing()`|Toggles the facing value between `Facing.FRONT` and `Facing.BACK`.|
|`toggleFlash()`|Toggles the flash value between `Flash.OFF`, `Flash.ON`, and `Flash.AUTO`.|
|`setLocation(Location)`|Sets location data to be appended to picture/video metadata.|
|`setLocation(double, double)`|Sets latitude and longitude to be appended to picture/video metadata.|
|`startAutoFocus(float, float)`|Starts an autofocus process at the given coordinates, with respect to the view dimensions.|
|`getPreviewSize()`|Returns the size of the preview surface. If CameraView was not constrained in its layout phase (e.g. it was `wrap_content`), this will return the same aspect ratio of CameraView.|

@ -4,6 +4,7 @@ import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.location.Location;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Build;
@ -40,8 +41,7 @@ class Camera1 extends CameraController {
private int mDeviceOrientation;
private int mSensorOffset;
private double mLatitude;
private double mLongitude;
private Location mLocation;
private Handler mFocusHandler = new Handler();
private Mapper mMapper = new Mapper.Mapper1();
@ -142,7 +142,7 @@ class Camera1 extends CameraController {
mOptions = new CameraOptions(params);
applyDefaultFocus(params);
mergeFlash(params, Flash.DEFAULT);
mergeLocation(params, 0d, 0d);
mergeLocation(params, null);
mergeWhiteBalance(params, WhiteBalance.DEFAULT);
params.setRecordingHint(mSessionType == SessionType.VIDEO);
mCamera.setParameters(params);
@ -210,28 +210,28 @@ class Camera1 extends CameraController {
}
@Override
void setLocation(double latitude, double longitude) {
double oldLat = mLatitude;
double oldLong = mLongitude;
mLatitude = latitude;
mLongitude = longitude;
void setLocation(Location location) {
Location oldLocation = mLocation;
mLocation = location;
if (isCameraOpened()) {
synchronized (mLock) {
Camera.Parameters params = mCamera.getParameters();
if (mergeLocation(params, oldLat, oldLong)) mCamera.setParameters(params);
if (mergeLocation(params, oldLocation)) mCamera.setParameters(params);
}
}
}
private boolean mergeLocation(Camera.Parameters params, double oldLatitude, double oldLongitude) {
if (mLatitude != 0 && mLongitude != 0) {
params.setGpsLatitude(mLatitude);
params.setGpsLongitude(mLongitude);
params.setGpsTimestamp(System.currentTimeMillis());
params.setGpsProcessingMethod("Unknown");
private boolean mergeLocation(Camera.Parameters params, Location oldLocation) {
if (mLocation != null) {
params.setGpsLatitude(mLocation.getLatitude());
params.setGpsLongitude(mLocation.getLongitude());
params.setGpsAltitude(mLocation.getAltitude());
params.setGpsTimestamp(mLocation.getTime());
params.setGpsProcessingMethod(mLocation.getProvider());
if (mIsCapturingVideo && mMediaRecorder != null) {
mMediaRecorder.setLocation((float) mLatitude, (float) mLongitude);
mMediaRecorder.setLocation((float) mLocation.getLatitude(),
(float) mLocation.getLongitude());
}
}
return true;
@ -577,8 +577,9 @@ class Camera1 extends CameraController {
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
if (mLatitude != 0 && mLongitude != 0) {
mMediaRecorder.setLocation((float) mLatitude, (float) mLongitude);
if (mLocation != null) {
mMediaRecorder.setLocation((float) mLocation.getLatitude(),
(float) mLocation.getLongitude());
}
mMediaRecorder.setProfile(getCamcorderProfile(mVideoQuality));

@ -9,6 +9,7 @@ import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
@ -147,7 +148,7 @@ class Camera2 extends CameraController {
}
@Override
void setLocation(double latitude, double longitude) {
void setLocation(Location location) {
}

@ -1,6 +1,7 @@
package com.otaliastudios.cameraview;
import android.graphics.PointF;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -36,7 +37,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
abstract void setWhiteBalance(WhiteBalance whiteBalance);
abstract void setVideoQuality(VideoQuality videoQuality);
abstract void setSessionType(SessionType sessionType);
abstract void setLocation(double latitude, double longitude);
abstract void setLocation(Location location);
abstract boolean capturePicture();
abstract boolean captureSnapshot();

@ -12,6 +12,7 @@ import android.content.res.TypedArray;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.location.Location;
import android.media.MediaActionSound;
import android.os.Build;
import android.os.Handler;
@ -712,7 +713,22 @@ public class CameraView extends FrameLayout {
* @param longitude current longitude
*/
public void setLocation(double latitude, double longitude) {
mCameraController.setLocation(latitude, longitude);
Location location = new Location("Unknown");
location.setTime(System.currentTimeMillis());
location.setAltitude(0);
location.setLatitude(latitude);
location.setLongitude(longitude);
mCameraController.setLocation(location);
}
/**
* Set location values to be found later in the jpeg EXIF header
*
* @param location current location
*/
public void setLocation(Location location) {
mCameraController.setLocation(location);
}

@ -1,6 +1,7 @@
package com.otaliastudios.cameraview.demo;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
@ -126,6 +127,15 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
startActivity(intent);
}
});
// Debug location.
/* camera.postDelayed(new Runnable() {
@Override
public void run() {
message("set Location", false);
camera.setLocation(-20, 40.12345);
}
}, 4500); */
}
private void message(String content, boolean important) {
@ -171,13 +181,7 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
if (mCapturingPicture || mCapturingVideo) return;
mCapturingVideo = true;
message("Recording for 8 seconds...", true);
camera.startCapturingVideo(null);
camera.postDelayed(new Runnable() {
@Override
public void run() {
camera.stopCapturingVideo();
}
}, 8000);
camera.startCapturingVideo(null, 8000);
}
@OnClick(R.id.toggleCamera)

Loading…
Cancel
Save