|
|
@ -1,13 +1,17 @@ |
|
|
|
package com.flurgle.camerakit; |
|
|
|
package com.flurgle.camerakit; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.graphics.Rect; |
|
|
|
import android.graphics.YuvImage; |
|
|
|
import android.graphics.YuvImage; |
|
|
|
import android.hardware.Camera; |
|
|
|
import android.hardware.Camera; |
|
|
|
import android.media.CamcorderProfile; |
|
|
|
import android.media.CamcorderProfile; |
|
|
|
import android.media.MediaRecorder; |
|
|
|
import android.media.MediaRecorder; |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
@ -24,6 +28,9 @@ import static com.flurgle.camerakit.CameraKit.Constants.METHOD_STILL; |
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
public class Camera1 extends CameraImpl { |
|
|
|
public class Camera1 extends CameraImpl { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final int FOCUS_AREA_SIZE_DEFAULT = 300; |
|
|
|
|
|
|
|
private static final int FOCUS_METERING_AREA_WEIGHT_DEFAULT = 1000; |
|
|
|
|
|
|
|
|
|
|
|
private int mCameraId; |
|
|
|
private int mCameraId; |
|
|
|
private Camera mCamera; |
|
|
|
private Camera mCamera; |
|
|
|
private Camera.Parameters mCameraParameters; |
|
|
|
private Camera.Parameters mCameraParameters; |
|
|
@ -32,6 +39,7 @@ public class Camera1 extends CameraImpl { |
|
|
|
private Size mCaptureSize; |
|
|
|
private Size mCaptureSize; |
|
|
|
private MediaRecorder mMediaRecorder; |
|
|
|
private MediaRecorder mMediaRecorder; |
|
|
|
private File mVideoFile; |
|
|
|
private File mVideoFile; |
|
|
|
|
|
|
|
private Camera.AutoFocusCallback mAutofocusCallback; |
|
|
|
|
|
|
|
|
|
|
|
private int mDisplayOrientation; |
|
|
|
private int mDisplayOrientation; |
|
|
|
|
|
|
|
|
|
|
@ -137,6 +145,7 @@ public class Camera1 extends CameraImpl { |
|
|
|
switch (focus) { |
|
|
|
switch (focus) { |
|
|
|
case FOCUS_CONTINUOUS: |
|
|
|
case FOCUS_CONTINUOUS: |
|
|
|
if (mCameraParameters != null) { |
|
|
|
if (mCameraParameters != null) { |
|
|
|
|
|
|
|
detachFocusTapListener(); |
|
|
|
final List<String> modes = mCameraParameters.getSupportedFocusModes(); |
|
|
|
final List<String> modes = mCameraParameters.getSupportedFocusModes(); |
|
|
|
if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
|
|
|
if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
@ -147,11 +156,18 @@ public class Camera1 extends CameraImpl { |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case FOCUS_TAP: |
|
|
|
case FOCUS_TAP: |
|
|
|
setFocus(FOCUS_CONTINUOUS); |
|
|
|
if (mCameraParameters != null) { |
|
|
|
|
|
|
|
attachFocusTapListener(); |
|
|
|
|
|
|
|
final List<String> modes = mCameraParameters.getSupportedFocusModes(); |
|
|
|
|
|
|
|
if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
|
|
|
|
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case FOCUS_OFF: |
|
|
|
case FOCUS_OFF: |
|
|
|
if (mCameraParameters != null) { |
|
|
|
if (mCameraParameters != null) { |
|
|
|
|
|
|
|
detachFocusTapListener(); |
|
|
|
final List<String> modes = mCameraParameters.getSupportedFocusModes(); |
|
|
|
final List<String> modes = mCameraParameters.getSupportedFocusModes(); |
|
|
|
if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { |
|
|
|
if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { |
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); |
|
|
|
mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); |
|
|
@ -403,4 +419,101 @@ public class Camera1 extends CameraImpl { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setTapToAutofocusListener(Camera.AutoFocusCallback callback) { |
|
|
|
|
|
|
|
if (this.mFocus != FOCUS_TAP) { |
|
|
|
|
|
|
|
throw new IllegalArgumentException("Please set the camera to FOCUS_TAP."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.mAutofocusCallback = callback; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int getFocusAreaSize() { |
|
|
|
|
|
|
|
return FOCUS_AREA_SIZE_DEFAULT; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int getFocusMeteringAreaWeight() { |
|
|
|
|
|
|
|
return FOCUS_METERING_AREA_WEIGHT_DEFAULT; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void detachFocusTapListener() { |
|
|
|
|
|
|
|
mPreview.getView().setOnTouchListener(null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void attachFocusTapListener() { |
|
|
|
|
|
|
|
mPreview.getView().setOnTouchListener(new View.OnTouchListener() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean onTouch(View v, MotionEvent event) { |
|
|
|
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_UP) { |
|
|
|
|
|
|
|
if (mCamera != null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Camera.Parameters parameters = mCamera.getParameters(); |
|
|
|
|
|
|
|
if (parameters.getMaxNumMeteringAreas() > 0) { |
|
|
|
|
|
|
|
Rect rect = calculateFocusArea(event.getX(), event.getY()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); |
|
|
|
|
|
|
|
List<Camera.Area> meteringAreas = new ArrayList<>(); |
|
|
|
|
|
|
|
meteringAreas.add(new Camera.Area(rect, getFocusMeteringAreaWeight())); |
|
|
|
|
|
|
|
parameters.setFocusAreas(meteringAreas); |
|
|
|
|
|
|
|
parameters.setMeteringAreas(meteringAreas); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mCamera.setParameters(parameters); |
|
|
|
|
|
|
|
mCamera.autoFocus(new Camera.AutoFocusCallback() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onAutoFocus(boolean success, Camera camera) { |
|
|
|
|
|
|
|
camera.cancelAutoFocus(); |
|
|
|
|
|
|
|
Camera.Parameters params = camera.getParameters(); |
|
|
|
|
|
|
|
if (params.getFocusMode() != Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { |
|
|
|
|
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
|
|
|
|
|
|
|
params.setFocusAreas(null); |
|
|
|
|
|
|
|
params.setMeteringAreas(null); |
|
|
|
|
|
|
|
camera.setParameters(params); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mAutofocusCallback != null) { |
|
|
|
|
|
|
|
mAutofocusCallback.onAutoFocus(success, camera); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
mCamera.autoFocus(new Camera.AutoFocusCallback() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onAutoFocus(boolean success, Camera camera) { |
|
|
|
|
|
|
|
if (mAutofocusCallback != null) { |
|
|
|
|
|
|
|
mAutofocusCallback.onAutoFocus(success, camera); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Rect calculateFocusArea(float x, float y) { |
|
|
|
|
|
|
|
int centerX = clamp(Float.valueOf((x / mPreview.getView().getWidth()) * 2000 - 1000).intValue(), getFocusAreaSize()); |
|
|
|
|
|
|
|
int centerY = clamp(Float.valueOf((y / mPreview.getView().getHeight()) * 2000 - 1000).intValue(), getFocusAreaSize()); |
|
|
|
|
|
|
|
return new Rect( |
|
|
|
|
|
|
|
centerX - getFocusAreaSize() / 2, |
|
|
|
|
|
|
|
centerY - getFocusAreaSize() / 2, |
|
|
|
|
|
|
|
centerX + getFocusAreaSize() / 2, |
|
|
|
|
|
|
|
centerY + getFocusAreaSize() / 2 |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int clamp(int touchCoordinateInCameraReper, int focusAreaSize) { |
|
|
|
|
|
|
|
int result; |
|
|
|
|
|
|
|
if (Math.abs(touchCoordinateInCameraReper) + focusAreaSize / 2 > 1000) { |
|
|
|
|
|
|
|
if (touchCoordinateInCameraReper > 0) { |
|
|
|
|
|
|
|
result = 1000 - focusAreaSize / 2; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
result = -1000 + focusAreaSize / 2; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
result = touchCoordinateInCameraReper - focusAreaSize / 2; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|