@capacitor-community/camera-preview 3.0.0 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -2
- package/android/build.gradle +14 -7
- package/android/src/androidTest/java/com/getcapacitor/android/ExampleInstrumentedTest.java +3 -3
- package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraActivity.java +869 -798
- package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java +184 -173
- package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomSurfaceView.java +12 -14
- package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomTextureView.java +16 -18
- package/android/src/main/java/com/ahm/capacitor/camera/preview/Preview.java +327 -323
- package/android/src/main/java/com/ahm/capacitor/camera/preview/TapGestureDetector.java +15 -14
- package/android/src/test/java/com/getcapacitor/ExampleUnitTest.java +4 -3
- package/dist/esm/definitions.d.ts +10 -8
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +3 -3
- package/dist/esm/web.js +36 -25
- package/dist/esm/web.js.map +1 -1
- package/ios/Plugin/CameraController.swift +107 -129
- package/ios/Plugin/Plugin.swift +76 -90
- package/ios/PluginTests/PluginTests.swift +8 -8
- package/package.json +27 -2
|
@@ -2,11 +2,11 @@ package com.ahm.capacitor.camera.preview;
|
|
|
2
2
|
|
|
3
3
|
import android.app.Activity;
|
|
4
4
|
import android.app.Fragment;
|
|
5
|
-
import android.content.pm.PackageManager;
|
|
6
5
|
import android.content.Context;
|
|
6
|
+
import android.content.pm.PackageManager;
|
|
7
|
+
import android.content.res.Configuration;
|
|
7
8
|
import android.graphics.Bitmap;
|
|
8
9
|
import android.graphics.Bitmap.CompressFormat;
|
|
9
|
-
import android.media.AudioManager;
|
|
10
10
|
import android.graphics.BitmapFactory;
|
|
11
11
|
import android.graphics.Canvas;
|
|
12
12
|
import android.graphics.ImageFormat;
|
|
@@ -16,19 +16,19 @@ import android.graphics.YuvImage;
|
|
|
16
16
|
import android.hardware.Camera;
|
|
17
17
|
import android.hardware.Camera.PictureCallback;
|
|
18
18
|
import android.hardware.Camera.ShutterCallback;
|
|
19
|
+
import android.media.AudioManager;
|
|
19
20
|
import android.media.CamcorderProfile;
|
|
20
21
|
import android.media.MediaRecorder;
|
|
21
|
-
import androidx.exifinterface.media.ExifInterface;
|
|
22
|
-
import android.view.Surface;
|
|
23
22
|
import android.os.Bundle;
|
|
24
23
|
import android.util.Base64;
|
|
25
|
-
import android.util.Log;
|
|
26
24
|
import android.util.DisplayMetrics;
|
|
25
|
+
import android.util.Log;
|
|
27
26
|
import android.view.GestureDetector;
|
|
28
27
|
import android.view.Gravity;
|
|
29
28
|
import android.view.LayoutInflater;
|
|
30
29
|
import android.view.MotionEvent;
|
|
31
30
|
import android.view.Surface;
|
|
31
|
+
import android.view.Surface;
|
|
32
32
|
import android.view.SurfaceHolder;
|
|
33
33
|
import android.view.SurfaceView;
|
|
34
34
|
import android.view.View;
|
|
@@ -36,7 +36,7 @@ import android.view.ViewGroup;
|
|
|
36
36
|
import android.view.ViewTreeObserver;
|
|
37
37
|
import android.widget.FrameLayout;
|
|
38
38
|
import android.widget.RelativeLayout;
|
|
39
|
-
|
|
39
|
+
import androidx.exifinterface.media.ExifInterface;
|
|
40
40
|
import java.io.ByteArrayInputStream;
|
|
41
41
|
import java.io.ByteArrayOutputStream;
|
|
42
42
|
import java.io.File;
|
|
@@ -48,884 +48,955 @@ import java.util.UUID;
|
|
|
48
48
|
|
|
49
49
|
public class CameraActivity extends Fragment {
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
private CameraPreviewListener eventListener;
|
|
67
|
-
private static final String TAG = "CameraActivity";
|
|
68
|
-
public FrameLayout mainLayout;
|
|
69
|
-
public FrameLayout frameContainerLayout;
|
|
70
|
-
|
|
71
|
-
private Preview mPreview;
|
|
72
|
-
private boolean canTakePicture = true;
|
|
73
|
-
|
|
74
|
-
private View view;
|
|
75
|
-
private Camera.Parameters cameraParameters;
|
|
76
|
-
private Camera mCamera;
|
|
77
|
-
private int numberOfCameras;
|
|
78
|
-
private int cameraCurrentlyLocked;
|
|
79
|
-
private int currentQuality;
|
|
80
|
-
private enum RecordingState {INITIALIZING, STARTED, STOPPED}
|
|
81
|
-
private RecordingState mRecordingState = RecordingState.INITIALIZING;
|
|
82
|
-
private MediaRecorder mRecorder = null;
|
|
83
|
-
private String recordFilePath;
|
|
84
|
-
private float opacity;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// The first rear facing camera
|
|
88
|
-
private int defaultCameraId;
|
|
89
|
-
public String defaultCamera;
|
|
90
|
-
public boolean tapToTakePicture;
|
|
91
|
-
public boolean dragEnabled;
|
|
92
|
-
public boolean tapToFocus;
|
|
93
|
-
public boolean disableExifHeaderStripping;
|
|
94
|
-
public boolean storeToFile;
|
|
95
|
-
public boolean toBack;
|
|
96
|
-
public boolean enableOpacity = false;
|
|
97
|
-
public boolean enableZoom = false;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
public int width;
|
|
101
|
-
public int height;
|
|
102
|
-
public int x;
|
|
103
|
-
public int y;
|
|
104
|
-
|
|
105
|
-
public void setEventListener(CameraPreviewListener listener){
|
|
106
|
-
eventListener = listener;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
private String appResourcesPackage;
|
|
110
|
-
|
|
111
|
-
@Override
|
|
112
|
-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
113
|
-
appResourcesPackage = getActivity().getPackageName();
|
|
114
|
-
|
|
115
|
-
// Inflate the layout for this fragment
|
|
116
|
-
view = inflater.inflate(getResources().getIdentifier("camera_activity", "layout", appResourcesPackage), container, false);
|
|
117
|
-
createCameraPreview();
|
|
118
|
-
return view;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public void setRect(int x, int y, int width, int height){
|
|
122
|
-
this.x = x;
|
|
123
|
-
this.y = y;
|
|
124
|
-
this.width = width;
|
|
125
|
-
this.height = height;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
private void createCameraPreview(){
|
|
129
|
-
if(mPreview == null) {
|
|
130
|
-
setDefaultCameraId();
|
|
131
|
-
|
|
132
|
-
//set box position and size
|
|
133
|
-
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, height);
|
|
134
|
-
layoutParams.setMargins(x, y, 0, 0);
|
|
135
|
-
frameContainerLayout = (FrameLayout) view.findViewById(getResources().getIdentifier("frame_container", "id", appResourcesPackage));
|
|
136
|
-
frameContainerLayout.setLayoutParams(layoutParams);
|
|
137
|
-
|
|
138
|
-
//video view
|
|
139
|
-
mPreview = new Preview(getActivity(), enableOpacity);
|
|
140
|
-
mainLayout = (FrameLayout) view.findViewById(getResources().getIdentifier("video_view", "id", appResourcesPackage));
|
|
141
|
-
mainLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
|
|
142
|
-
mainLayout.addView(mPreview);
|
|
143
|
-
mainLayout.setEnabled(false);
|
|
144
|
-
|
|
145
|
-
if (enableZoom) {
|
|
146
|
-
this.setupTouchAndBackButton();
|
|
147
|
-
}
|
|
51
|
+
public interface CameraPreviewListener {
|
|
52
|
+
void onPictureTaken(String originalPicture);
|
|
53
|
+
void onPictureTakenError(String message);
|
|
54
|
+
void onSnapshotTaken(String originalPicture);
|
|
55
|
+
void onSnapshotTakenError(String message);
|
|
56
|
+
void onFocusSet(int pointX, int pointY);
|
|
57
|
+
void onFocusSetError(String message);
|
|
58
|
+
void onBackButton();
|
|
59
|
+
void onCameraStarted();
|
|
60
|
+
void onStartRecordVideo();
|
|
61
|
+
void onStartRecordVideoError(String message);
|
|
62
|
+
void onStopRecordVideo(String file);
|
|
63
|
+
void onStopRecordVideoError(String error);
|
|
64
|
+
}
|
|
148
65
|
|
|
66
|
+
private CameraPreviewListener eventListener;
|
|
67
|
+
private static final String TAG = "CameraActivity";
|
|
68
|
+
public FrameLayout mainLayout;
|
|
69
|
+
public FrameLayout frameContainerLayout;
|
|
70
|
+
|
|
71
|
+
private Preview mPreview;
|
|
72
|
+
private boolean canTakePicture = true;
|
|
73
|
+
|
|
74
|
+
private View view;
|
|
75
|
+
private Camera.Parameters cameraParameters;
|
|
76
|
+
private Camera mCamera;
|
|
77
|
+
private int numberOfCameras;
|
|
78
|
+
private int cameraCurrentlyLocked;
|
|
79
|
+
private int currentQuality;
|
|
80
|
+
|
|
81
|
+
private enum RecordingState {
|
|
82
|
+
INITIALIZING,
|
|
83
|
+
STARTED,
|
|
84
|
+
STOPPED
|
|
149
85
|
}
|
|
150
|
-
}
|
|
151
|
-
private void setupTouchAndBackButton() {
|
|
152
|
-
|
|
153
|
-
final GestureDetector gestureDetector = new GestureDetector(getActivity().getApplicationContext(), new TapGestureDetector());
|
|
154
|
-
|
|
155
|
-
getActivity().runOnUiThread(new Runnable() {
|
|
156
|
-
@Override
|
|
157
|
-
public void run() {
|
|
158
|
-
frameContainerLayout.setClickable(true);
|
|
159
|
-
frameContainerLayout.setOnTouchListener(new View.OnTouchListener() {
|
|
160
|
-
|
|
161
|
-
private int mLastTouchX;
|
|
162
|
-
private int mLastTouchY;
|
|
163
|
-
private int mPosX = 0;
|
|
164
|
-
private int mPosY = 0;
|
|
165
|
-
|
|
166
|
-
@Override
|
|
167
|
-
public boolean onTouch(View v, MotionEvent event) {
|
|
168
|
-
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) frameContainerLayout.getLayoutParams();
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
boolean isSingleTapTouch = gestureDetector.onTouchEvent(event);
|
|
172
|
-
int action = event.getAction();
|
|
173
|
-
int eventCount = event.getPointerCount();
|
|
174
|
-
Log.d(TAG, "onTouch event, action, count: " + event + ", " + action + ", " + eventCount);
|
|
175
|
-
if (eventCount > 1) {
|
|
176
|
-
// handle multi-touch events
|
|
177
|
-
Camera.Parameters params = mCamera.getParameters();
|
|
178
|
-
if (action == MotionEvent.ACTION_POINTER_DOWN) {
|
|
179
|
-
mDist = getFingerSpacing(event);
|
|
180
|
-
} else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
|
|
181
|
-
handleZoom(event, params);
|
|
182
|
-
}
|
|
183
|
-
} else {
|
|
184
|
-
if (action != MotionEvent.ACTION_MOVE && isSingleTapTouch) {
|
|
185
|
-
if (tapToTakePicture && tapToFocus) {
|
|
186
|
-
setFocusArea((int) event.getX(0), (int) event.getY(0), new Camera.AutoFocusCallback() {
|
|
187
|
-
public void onAutoFocus(boolean success, Camera camera) {
|
|
188
|
-
if (success) {
|
|
189
|
-
takePicture(0, 0, 85);
|
|
190
|
-
} else {
|
|
191
|
-
Log.d(TAG, "onTouch:" + " setFocusArea() did not suceed");
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
} else if (tapToTakePicture) {
|
|
197
|
-
takePicture(0, 0, 85);
|
|
198
|
-
|
|
199
|
-
} else if (tapToFocus) {
|
|
200
|
-
setFocusArea((int) event.getX(0), (int) event.getY(0), new Camera.AutoFocusCallback() {
|
|
201
|
-
public void onAutoFocus(boolean success, Camera camera) {
|
|
202
|
-
if (success) {
|
|
203
|
-
// A callback to JS might make sense here.
|
|
204
|
-
} else {
|
|
205
|
-
Log.d(TAG, "onTouch:" + " setFocusArea() did not suceed");
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
return true;
|
|
211
|
-
} else {
|
|
212
|
-
if (dragEnabled) {
|
|
213
|
-
int x;
|
|
214
|
-
int y;
|
|
215
|
-
|
|
216
|
-
switch (event.getAction()) {
|
|
217
|
-
case MotionEvent.ACTION_DOWN:
|
|
218
|
-
if (mLastTouchX == 0 || mLastTouchY == 0) {
|
|
219
|
-
mLastTouchX = (int) event.getRawX() - layoutParams.leftMargin;
|
|
220
|
-
mLastTouchY = (int) event.getRawY() - layoutParams.topMargin;
|
|
221
|
-
} else {
|
|
222
|
-
mLastTouchX = (int) event.getRawX();
|
|
223
|
-
mLastTouchY = (int) event.getRawY();
|
|
224
|
-
}
|
|
225
|
-
break;
|
|
226
|
-
case MotionEvent.ACTION_MOVE:
|
|
227
|
-
|
|
228
|
-
x = (int) event.getRawX();
|
|
229
|
-
y = (int) event.getRawY();
|
|
230
|
-
|
|
231
|
-
final float dx = x - mLastTouchX;
|
|
232
|
-
final float dy = y - mLastTouchY;
|
|
233
|
-
|
|
234
|
-
mPosX += dx;
|
|
235
|
-
mPosY += dy;
|
|
236
|
-
|
|
237
|
-
layoutParams.leftMargin = mPosX;
|
|
238
|
-
layoutParams.topMargin = mPosY;
|
|
239
|
-
|
|
240
|
-
frameContainerLayout.setLayoutParams(layoutParams);
|
|
241
|
-
|
|
242
|
-
// Remember this touch position for the next move event
|
|
243
|
-
mLastTouchX = x;
|
|
244
|
-
mLastTouchY = y;
|
|
245
|
-
|
|
246
|
-
break;
|
|
247
|
-
default:
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
return true;
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
frameContainerLayout.setFocusableInTouchMode(true);
|
|
257
|
-
frameContainerLayout.requestFocus();
|
|
258
|
-
frameContainerLayout.setOnKeyListener(new View.OnKeyListener() {
|
|
259
|
-
@Override
|
|
260
|
-
public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
|
|
261
|
-
|
|
262
|
-
if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
|
|
263
|
-
eventListener.onBackButton();
|
|
264
|
-
return true;
|
|
265
|
-
}
|
|
266
|
-
return false;
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
86
|
|
|
87
|
+
private RecordingState mRecordingState = RecordingState.INITIALIZING;
|
|
88
|
+
private MediaRecorder mRecorder = null;
|
|
89
|
+
private String recordFilePath;
|
|
90
|
+
private float opacity;
|
|
91
|
+
|
|
92
|
+
// The first rear facing camera
|
|
93
|
+
private int defaultCameraId;
|
|
94
|
+
public String defaultCamera;
|
|
95
|
+
public boolean tapToTakePicture;
|
|
96
|
+
public boolean dragEnabled;
|
|
97
|
+
public boolean tapToFocus;
|
|
98
|
+
public boolean disableExifHeaderStripping;
|
|
99
|
+
public boolean storeToFile;
|
|
100
|
+
public boolean toBack;
|
|
101
|
+
public boolean enableOpacity = false;
|
|
102
|
+
public boolean enableZoom = false;
|
|
103
|
+
|
|
104
|
+
public int width;
|
|
105
|
+
public int height;
|
|
106
|
+
public int x;
|
|
107
|
+
public int y;
|
|
108
|
+
|
|
109
|
+
public void setEventListener(CameraPreviewListener listener) {
|
|
110
|
+
eventListener = listener;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private String appResourcesPackage;
|
|
114
|
+
|
|
115
|
+
@Override
|
|
116
|
+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
117
|
+
appResourcesPackage = getActivity().getPackageName();
|
|
118
|
+
|
|
119
|
+
// Inflate the layout for this fragment
|
|
120
|
+
view = inflater.inflate(getResources().getIdentifier("camera_activity", "layout", appResourcesPackage), container, false);
|
|
121
|
+
createCameraPreview();
|
|
122
|
+
return view;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public void setRect(int x, int y, int width, int height) {
|
|
126
|
+
this.x = x;
|
|
127
|
+
this.y = y;
|
|
128
|
+
this.width = width;
|
|
129
|
+
this.height = height;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private void createCameraPreview() {
|
|
133
|
+
if (mPreview == null) {
|
|
134
|
+
setDefaultCameraId();
|
|
135
|
+
|
|
136
|
+
//set box position and size
|
|
137
|
+
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, height);
|
|
138
|
+
layoutParams.setMargins(x, y, 0, 0);
|
|
139
|
+
frameContainerLayout =
|
|
140
|
+
(FrameLayout) view.findViewById(getResources().getIdentifier("frame_container", "id", appResourcesPackage));
|
|
141
|
+
frameContainerLayout.setLayoutParams(layoutParams);
|
|
142
|
+
|
|
143
|
+
//video view
|
|
144
|
+
mPreview = new Preview(getActivity(), enableOpacity);
|
|
145
|
+
mainLayout = (FrameLayout) view.findViewById(getResources().getIdentifier("video_view", "id", appResourcesPackage));
|
|
146
|
+
mainLayout.setLayoutParams(
|
|
147
|
+
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
|
|
148
|
+
);
|
|
149
|
+
mainLayout.addView(mPreview);
|
|
150
|
+
mainLayout.setEnabled(false);
|
|
151
|
+
|
|
152
|
+
if (enableZoom) {
|
|
153
|
+
this.setupTouchAndBackButton();
|
|
154
|
+
}
|
|
270
155
|
}
|
|
271
|
-
private float mDist = 0F;
|
|
272
|
-
|
|
273
|
-
private void handleZoom(MotionEvent event, Camera.Parameters params) {
|
|
274
|
-
if (mCamera != null) {
|
|
275
|
-
mCamera.cancelAutoFocus();
|
|
276
|
-
int maxZoom = params.getMaxZoom();
|
|
277
|
-
int zoom = params.getZoom();
|
|
278
|
-
float newDist = getFingerSpacing(event);
|
|
279
|
-
if (newDist > mDist) {
|
|
280
|
-
//zoom in
|
|
281
|
-
if (zoom < maxZoom)
|
|
282
|
-
zoom++;
|
|
283
|
-
} else if (newDist < mDist) {
|
|
284
|
-
//zoom out
|
|
285
|
-
if (zoom > 0)
|
|
286
|
-
zoom--;
|
|
287
|
-
}
|
|
288
|
-
mDist = newDist;
|
|
289
|
-
params.setZoom(zoom);
|
|
290
|
-
mCamera.setParameters(params);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
private void setDefaultCameraId(){
|
|
298
|
-
// Find the total number of cameras available
|
|
299
|
-
numberOfCameras = Camera.getNumberOfCameras();
|
|
300
|
-
|
|
301
|
-
int facing = "front".equals(defaultCamera) ? Camera.CameraInfo.CAMERA_FACING_FRONT : Camera.CameraInfo.CAMERA_FACING_BACK;
|
|
302
|
-
|
|
303
|
-
// Find the ID of the default camera
|
|
304
|
-
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
|
|
305
|
-
for (int i = 0; i < numberOfCameras; i++) {
|
|
306
|
-
Camera.getCameraInfo(i, cameraInfo);
|
|
307
|
-
if (cameraInfo.facing == facing) {
|
|
308
|
-
defaultCameraId = i;
|
|
309
|
-
break;
|
|
310
|
-
}
|
|
311
156
|
}
|
|
312
|
-
}
|
|
313
157
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
158
|
+
private void setupTouchAndBackButton() {
|
|
159
|
+
final GestureDetector gestureDetector = new GestureDetector(getActivity().getApplicationContext(), new TapGestureDetector());
|
|
160
|
+
|
|
161
|
+
getActivity()
|
|
162
|
+
.runOnUiThread(
|
|
163
|
+
new Runnable() {
|
|
164
|
+
@Override
|
|
165
|
+
public void run() {
|
|
166
|
+
frameContainerLayout.setClickable(true);
|
|
167
|
+
frameContainerLayout.setOnTouchListener(
|
|
168
|
+
new View.OnTouchListener() {
|
|
169
|
+
private int mLastTouchX;
|
|
170
|
+
private int mLastTouchY;
|
|
171
|
+
private int mPosX = 0;
|
|
172
|
+
private int mPosY = 0;
|
|
173
|
+
|
|
174
|
+
@Override
|
|
175
|
+
public boolean onTouch(View v, MotionEvent event) {
|
|
176
|
+
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) frameContainerLayout.getLayoutParams();
|
|
177
|
+
|
|
178
|
+
boolean isSingleTapTouch = gestureDetector.onTouchEvent(event);
|
|
179
|
+
int action = event.getAction();
|
|
180
|
+
int eventCount = event.getPointerCount();
|
|
181
|
+
Log.d(TAG, "onTouch event, action, count: " + event + ", " + action + ", " + eventCount);
|
|
182
|
+
if (eventCount > 1) {
|
|
183
|
+
// handle multi-touch events
|
|
184
|
+
Camera.Parameters params = mCamera.getParameters();
|
|
185
|
+
if (action == MotionEvent.ACTION_POINTER_DOWN) {
|
|
186
|
+
mDist = getFingerSpacing(event);
|
|
187
|
+
} else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
|
|
188
|
+
handleZoom(event, params);
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
if (action != MotionEvent.ACTION_MOVE && isSingleTapTouch) {
|
|
192
|
+
if (tapToTakePicture && tapToFocus) {
|
|
193
|
+
setFocusArea(
|
|
194
|
+
(int) event.getX(0),
|
|
195
|
+
(int) event.getY(0),
|
|
196
|
+
new Camera.AutoFocusCallback() {
|
|
197
|
+
public void onAutoFocus(boolean success, Camera camera) {
|
|
198
|
+
if (success) {
|
|
199
|
+
takePicture(0, 0, 85);
|
|
200
|
+
} else {
|
|
201
|
+
Log.d(TAG, "onTouch:" + " setFocusArea() did not suceed");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
} else if (tapToTakePicture) {
|
|
207
|
+
takePicture(0, 0, 85);
|
|
208
|
+
} else if (tapToFocus) {
|
|
209
|
+
setFocusArea(
|
|
210
|
+
(int) event.getX(0),
|
|
211
|
+
(int) event.getY(0),
|
|
212
|
+
new Camera.AutoFocusCallback() {
|
|
213
|
+
public void onAutoFocus(boolean success, Camera camera) {
|
|
214
|
+
if (success) {
|
|
215
|
+
// A callback to JS might make sense here.
|
|
216
|
+
} else {
|
|
217
|
+
Log.d(TAG, "onTouch:" + " setFocusArea() did not suceed");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
return true;
|
|
224
|
+
} else {
|
|
225
|
+
if (dragEnabled) {
|
|
226
|
+
int x;
|
|
227
|
+
int y;
|
|
228
|
+
|
|
229
|
+
switch (event.getAction()) {
|
|
230
|
+
case MotionEvent.ACTION_DOWN:
|
|
231
|
+
if (mLastTouchX == 0 || mLastTouchY == 0) {
|
|
232
|
+
mLastTouchX = (int) event.getRawX() - layoutParams.leftMargin;
|
|
233
|
+
mLastTouchY = (int) event.getRawY() - layoutParams.topMargin;
|
|
234
|
+
} else {
|
|
235
|
+
mLastTouchX = (int) event.getRawX();
|
|
236
|
+
mLastTouchY = (int) event.getRawY();
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
case MotionEvent.ACTION_MOVE:
|
|
240
|
+
x = (int) event.getRawX();
|
|
241
|
+
y = (int) event.getRawY();
|
|
242
|
+
|
|
243
|
+
final float dx = x - mLastTouchX;
|
|
244
|
+
final float dy = y - mLastTouchY;
|
|
245
|
+
|
|
246
|
+
mPosX += dx;
|
|
247
|
+
mPosY += dy;
|
|
248
|
+
|
|
249
|
+
layoutParams.leftMargin = mPosX;
|
|
250
|
+
layoutParams.topMargin = mPosY;
|
|
251
|
+
|
|
252
|
+
frameContainerLayout.setLayoutParams(layoutParams);
|
|
253
|
+
|
|
254
|
+
// Remember this touch position for the next move event
|
|
255
|
+
mLastTouchX = x;
|
|
256
|
+
mLastTouchY = y;
|
|
257
|
+
|
|
258
|
+
break;
|
|
259
|
+
default:
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
frameContainerLayout.setFocusableInTouchMode(true);
|
|
270
|
+
frameContainerLayout.requestFocus();
|
|
271
|
+
frameContainerLayout.setOnKeyListener(
|
|
272
|
+
new View.OnKeyListener() {
|
|
273
|
+
@Override
|
|
274
|
+
public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
|
|
275
|
+
if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
|
|
276
|
+
eventListener.onBackButton();
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private float mDist = 0F;
|
|
286
|
+
|
|
287
|
+
private void handleZoom(MotionEvent event, Camera.Parameters params) {
|
|
288
|
+
if (mCamera != null) {
|
|
289
|
+
mCamera.cancelAutoFocus();
|
|
290
|
+
int maxZoom = params.getMaxZoom();
|
|
291
|
+
int zoom = params.getZoom();
|
|
292
|
+
float newDist = getFingerSpacing(event);
|
|
293
|
+
if (newDist > mDist) {
|
|
294
|
+
//zoom in
|
|
295
|
+
if (zoom < maxZoom) zoom++;
|
|
296
|
+
} else if (newDist < mDist) {
|
|
297
|
+
//zoom out
|
|
298
|
+
if (zoom > 0) zoom--;
|
|
299
|
+
}
|
|
300
|
+
mDist = newDist;
|
|
301
|
+
params.setZoom(zoom);
|
|
302
|
+
mCamera.setParameters(params);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private void setDefaultCameraId() {
|
|
310
|
+
// Find the total number of cameras available
|
|
311
|
+
numberOfCameras = Camera.getNumberOfCameras();
|
|
317
312
|
|
|
318
|
-
|
|
313
|
+
int facing = "front".equals(defaultCamera) ? Camera.CameraInfo.CAMERA_FACING_FRONT : Camera.CameraInfo.CAMERA_FACING_BACK;
|
|
319
314
|
|
|
320
|
-
|
|
321
|
-
|
|
315
|
+
// Find the ID of the default camera
|
|
316
|
+
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
|
|
317
|
+
for (int i = 0; i < numberOfCameras; i++) {
|
|
318
|
+
Camera.getCameraInfo(i, cameraInfo);
|
|
319
|
+
if (cameraInfo.facing == facing) {
|
|
320
|
+
defaultCameraId = i;
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
322
324
|
}
|
|
323
325
|
|
|
324
|
-
|
|
326
|
+
@Override
|
|
327
|
+
public void onResume() {
|
|
328
|
+
super.onResume();
|
|
329
|
+
|
|
330
|
+
mCamera = Camera.open(defaultCameraId);
|
|
331
|
+
|
|
332
|
+
if (cameraParameters != null) {
|
|
333
|
+
mCamera.setParameters(cameraParameters);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
cameraCurrentlyLocked = defaultCameraId;
|
|
337
|
+
|
|
338
|
+
if (mPreview.mPreviewSize == null) {
|
|
339
|
+
mPreview.setCamera(mCamera, cameraCurrentlyLocked);
|
|
340
|
+
eventListener.onCameraStarted();
|
|
341
|
+
} else {
|
|
342
|
+
mPreview.switchCamera(mCamera, cameraCurrentlyLocked);
|
|
343
|
+
mCamera.startPreview();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
Log.d(TAG, "cameraCurrentlyLocked:" + cameraCurrentlyLocked);
|
|
347
|
+
|
|
348
|
+
final FrameLayout frameContainerLayout = (FrameLayout) view.findViewById(
|
|
349
|
+
getResources().getIdentifier("frame_container", "id", appResourcesPackage)
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
ViewTreeObserver viewTreeObserver = frameContainerLayout.getViewTreeObserver();
|
|
353
|
+
|
|
354
|
+
if (viewTreeObserver.isAlive()) {
|
|
355
|
+
viewTreeObserver.addOnGlobalLayoutListener(
|
|
356
|
+
new ViewTreeObserver.OnGlobalLayoutListener() {
|
|
357
|
+
@Override
|
|
358
|
+
public void onGlobalLayout() {
|
|
359
|
+
frameContainerLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
|
|
360
|
+
frameContainerLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
|
|
361
|
+
Activity activity = getActivity();
|
|
362
|
+
if (isAdded() && activity != null) {
|
|
363
|
+
final RelativeLayout frameCamContainerLayout = (RelativeLayout) view.findViewById(
|
|
364
|
+
getResources().getIdentifier("frame_camera_cont", "id", appResourcesPackage)
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
FrameLayout.LayoutParams camViewLayout = new FrameLayout.LayoutParams(
|
|
368
|
+
frameContainerLayout.getWidth(),
|
|
369
|
+
frameContainerLayout.getHeight()
|
|
370
|
+
);
|
|
371
|
+
camViewLayout.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
|
|
372
|
+
frameCamContainerLayout.setLayoutParams(camViewLayout);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
325
379
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
380
|
+
@Override
|
|
381
|
+
public void onPause() {
|
|
382
|
+
super.onPause();
|
|
383
|
+
|
|
384
|
+
// Because the Camera object is a shared resource, it's very important to release it when the activity is paused.
|
|
385
|
+
if (mCamera != null) {
|
|
386
|
+
setDefaultCameraId();
|
|
387
|
+
mPreview.setCamera(null, -1);
|
|
388
|
+
mCamera.setPreviewCallback(null);
|
|
389
|
+
mCamera.release();
|
|
390
|
+
mCamera = null;
|
|
391
|
+
}
|
|
332
392
|
}
|
|
333
393
|
|
|
334
|
-
|
|
394
|
+
@Override
|
|
395
|
+
public void onConfigurationChanged(Configuration newConfig) {
|
|
396
|
+
super.onConfigurationChanged(newConfig);
|
|
397
|
+
|
|
398
|
+
final FrameLayout frameContainerLayout = (FrameLayout) view.findViewById(
|
|
399
|
+
getResources().getIdentifier("frame_container", "id", appResourcesPackage)
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
final int previousOrientation = frameContainerLayout.getHeight() > frameContainerLayout.getWidth() ? Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
|
|
403
|
+
// Checks if the orientation of the screen has changed
|
|
404
|
+
if (newConfig.orientation != previousOrientation) {
|
|
335
405
|
|
|
336
|
-
|
|
406
|
+
final RelativeLayout frameCamContainerLayout = (RelativeLayout) view.findViewById(
|
|
407
|
+
getResources().getIdentifier("frame_camera_cont", "id", appResourcesPackage)
|
|
408
|
+
);
|
|
337
409
|
|
|
338
|
-
|
|
410
|
+
frameContainerLayout.getLayoutParams().width = frameCamContainerLayout.getHeight();
|
|
411
|
+
frameContainerLayout.getLayoutParams().height = frameCamContainerLayout.getWidth();
|
|
339
412
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
413
|
+
frameCamContainerLayout.getLayoutParams().width = frameCamContainerLayout.getHeight();
|
|
414
|
+
frameCamContainerLayout.getLayoutParams().height = frameCamContainerLayout.getWidth();
|
|
415
|
+
|
|
416
|
+
frameContainerLayout.invalidate();
|
|
417
|
+
frameContainerLayout.requestLayout();
|
|
418
|
+
|
|
419
|
+
frameCamContainerLayout.forceLayout();
|
|
420
|
+
|
|
421
|
+
mPreview.setCameraDisplayOrientation();
|
|
349
422
|
|
|
350
|
-
FrameLayout.LayoutParams camViewLayout = new FrameLayout.LayoutParams(frameContainerLayout.getWidth(), frameContainerLayout.getHeight());
|
|
351
|
-
camViewLayout.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
|
|
352
|
-
frameCamContainerLayout.setLayoutParams(camViewLayout);
|
|
353
|
-
}
|
|
354
423
|
}
|
|
355
|
-
|
|
424
|
+
|
|
356
425
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
super.onPause();
|
|
362
|
-
|
|
363
|
-
// Because the Camera object is a shared resource, it's very important to release it when the activity is paused.
|
|
364
|
-
if (mCamera != null) {
|
|
365
|
-
setDefaultCameraId();
|
|
366
|
-
mPreview.setCamera(null, -1);
|
|
367
|
-
mCamera.setPreviewCallback(null);
|
|
368
|
-
mCamera.release();
|
|
369
|
-
mCamera = null;
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
public Camera getCamera() {
|
|
429
|
+
return mCamera;
|
|
370
430
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
List<String> supportedFlashModesNewCamera = mCamera.getParameters().getSupportedFlashModes();
|
|
408
|
-
String currentFlashModePreviousCamera = cameraParameters.getFlashMode();
|
|
409
|
-
if (supportedFlashModesNewCamera != null && supportedFlashModesNewCamera.contains(currentFlashModePreviousCamera)) {
|
|
410
|
-
Log.d(TAG, "current flash mode supported on new camera. setting params");
|
|
411
|
-
/* mCamera.setParameters(cameraParameters);
|
|
431
|
+
|
|
432
|
+
public void switchCamera() {
|
|
433
|
+
// check for availability of multiple cameras
|
|
434
|
+
if (numberOfCameras == 1) {
|
|
435
|
+
//There is only one camera available
|
|
436
|
+
} else {
|
|
437
|
+
Log.d(TAG, "numberOfCameras: " + numberOfCameras);
|
|
438
|
+
|
|
439
|
+
// OK, we have multiple cameras. Release this camera -> cameraCurrentlyLocked
|
|
440
|
+
if (mCamera != null) {
|
|
441
|
+
mCamera.stopPreview();
|
|
442
|
+
mPreview.setCamera(null, -1);
|
|
443
|
+
mCamera.release();
|
|
444
|
+
mCamera = null;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
Log.d(TAG, "cameraCurrentlyLocked := " + Integer.toString(cameraCurrentlyLocked));
|
|
448
|
+
try {
|
|
449
|
+
cameraCurrentlyLocked = (cameraCurrentlyLocked + 1) % numberOfCameras;
|
|
450
|
+
Log.d(TAG, "cameraCurrentlyLocked new: " + cameraCurrentlyLocked);
|
|
451
|
+
} catch (Exception exception) {
|
|
452
|
+
Log.d(TAG, exception.getMessage());
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Acquire the next camera and request Preview to reconfigure parameters.
|
|
456
|
+
mCamera = Camera.open(cameraCurrentlyLocked);
|
|
457
|
+
|
|
458
|
+
if (cameraParameters != null) {
|
|
459
|
+
Log.d(TAG, "camera parameter not null");
|
|
460
|
+
|
|
461
|
+
// Check for flashMode as well to prevent error on frontward facing camera.
|
|
462
|
+
List<String> supportedFlashModesNewCamera = mCamera.getParameters().getSupportedFlashModes();
|
|
463
|
+
String currentFlashModePreviousCamera = cameraParameters.getFlashMode();
|
|
464
|
+
if (supportedFlashModesNewCamera != null && supportedFlashModesNewCamera.contains(currentFlashModePreviousCamera)) {
|
|
465
|
+
Log.d(TAG, "current flash mode supported on new camera. setting params");
|
|
466
|
+
/* mCamera.setParameters(cameraParameters);
|
|
412
467
|
The line above is disabled because parameters that can actually be changed are different from one device to another. Makes less sense trying to reconfigure them when changing camera device while those settings gan be changed using plugin methods.
|
|
413
468
|
*/
|
|
414
|
-
|
|
415
|
-
|
|
469
|
+
} else {
|
|
470
|
+
Log.d(TAG, "current flash mode NOT supported on new camera");
|
|
471
|
+
}
|
|
472
|
+
} else {
|
|
473
|
+
Log.d(TAG, "camera parameter NULL");
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
mPreview.switchCamera(mCamera, cameraCurrentlyLocked);
|
|
477
|
+
|
|
478
|
+
mCamera.startPreview();
|
|
416
479
|
}
|
|
480
|
+
}
|
|
417
481
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
}
|
|
482
|
+
public void setCameraParameters(Camera.Parameters params) {
|
|
483
|
+
cameraParameters = params;
|
|
421
484
|
|
|
422
|
-
|
|
485
|
+
if (mCamera != null && cameraParameters != null) {
|
|
486
|
+
mCamera.setParameters(cameraParameters);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
423
489
|
|
|
424
|
-
|
|
490
|
+
public boolean hasFrontCamera() {
|
|
491
|
+
return getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
|
|
425
492
|
}
|
|
426
|
-
}
|
|
427
493
|
|
|
428
|
-
|
|
429
|
-
|
|
494
|
+
public static Bitmap applyMatrix(Bitmap source, Matrix matrix) {
|
|
495
|
+
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
|
|
496
|
+
}
|
|
430
497
|
|
|
431
|
-
|
|
432
|
-
|
|
498
|
+
ShutterCallback shutterCallback = new ShutterCallback() {
|
|
499
|
+
public void onShutter() {
|
|
500
|
+
// do nothing, availabilty of this callback causes default system shutter sound to work
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
private static int exifToDegrees(int exifOrientation) {
|
|
505
|
+
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
|
|
506
|
+
return 90;
|
|
507
|
+
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
|
|
508
|
+
return 180;
|
|
509
|
+
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
|
|
510
|
+
return 270;
|
|
511
|
+
}
|
|
512
|
+
return 0;
|
|
433
513
|
}
|
|
434
|
-
}
|
|
435
514
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
}
|
|
515
|
+
private String getTempDirectoryPath() {
|
|
516
|
+
File cache = null;
|
|
439
517
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
518
|
+
// Use internal storage
|
|
519
|
+
cache = getActivity().getCacheDir();
|
|
443
520
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
521
|
+
// Create the cache directory if it doesn't exist
|
|
522
|
+
cache.mkdirs();
|
|
523
|
+
return cache.getAbsolutePath();
|
|
447
524
|
}
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
private static int exifToDegrees(int exifOrientation) {
|
|
451
|
-
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
|
|
452
|
-
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
|
|
453
|
-
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
|
|
454
|
-
return 0;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
private String getTempDirectoryPath() {
|
|
458
|
-
File cache = null;
|
|
459
|
-
|
|
460
|
-
// Use internal storage
|
|
461
|
-
cache = getActivity().getCacheDir();
|
|
462
|
-
|
|
463
|
-
// Create the cache directory if it doesn't exist
|
|
464
|
-
cache.mkdirs();
|
|
465
|
-
return cache.getAbsolutePath();
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
private String getTempFilePath() {
|
|
469
|
-
return getTempDirectoryPath() + "/cpcp_capture_" + UUID.randomUUID().toString().replace("-", "").substring(0, 8) + ".jpg";
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
PictureCallback jpegPictureCallback = new PictureCallback(){
|
|
473
|
-
public void onPictureTaken(byte[] data, Camera arg1){
|
|
474
|
-
Log.d(TAG, "CameraPreview jpegPictureCallback");
|
|
475
|
-
|
|
476
|
-
try {
|
|
477
|
-
if (!disableExifHeaderStripping) {
|
|
478
|
-
Matrix matrix = new Matrix();
|
|
479
|
-
if (cameraCurrentlyLocked == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
480
|
-
matrix.preScale(1.0f, -1.0f);
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
|
|
484
|
-
int rotation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
|
|
485
|
-
int rotationInDegrees = exifToDegrees(rotation);
|
|
486
|
-
|
|
487
|
-
if (rotation != 0f) {
|
|
488
|
-
matrix.preRotate(rotationInDegrees);
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
// Check if matrix has changed. In that case, apply matrix and override data
|
|
492
|
-
if (!matrix.isIdentity()) {
|
|
493
|
-
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
|
|
494
|
-
bitmap = applyMatrix(bitmap, matrix);
|
|
495
|
-
|
|
496
|
-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
497
|
-
bitmap.compress(CompressFormat.JPEG, currentQuality, outputStream);
|
|
498
|
-
data = outputStream.toByteArray();
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
if (!storeToFile) {
|
|
503
|
-
String encodedImage = Base64.encodeToString(data, Base64.NO_WRAP);
|
|
504
525
|
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
String path = getTempFilePath();
|
|
508
|
-
FileOutputStream out = new FileOutputStream(path);
|
|
509
|
-
out.write(data);
|
|
510
|
-
out.close();
|
|
511
|
-
eventListener.onPictureTaken(path);
|
|
512
|
-
}
|
|
513
|
-
Log.d(TAG, "CameraPreview pictureTakenHandler called back");
|
|
514
|
-
} catch (OutOfMemoryError e) {
|
|
515
|
-
// most likely failed to allocate memory for rotateBitmap
|
|
516
|
-
Log.d(TAG, "CameraPreview OutOfMemoryError");
|
|
517
|
-
// failed to allocate memory
|
|
518
|
-
eventListener.onPictureTakenError("Picture too large (memory)");
|
|
519
|
-
} catch (IOException e) {
|
|
520
|
-
Log.d(TAG, "CameraPreview IOException");
|
|
521
|
-
eventListener.onPictureTakenError("IO Error when extracting exif");
|
|
522
|
-
} catch (Exception e) {
|
|
523
|
-
Log.d(TAG, "CameraPreview onPictureTaken general exception");
|
|
524
|
-
} finally {
|
|
525
|
-
canTakePicture = true;
|
|
526
|
-
mCamera.startPreview();
|
|
527
|
-
}
|
|
526
|
+
private String getTempFilePath() {
|
|
527
|
+
return getTempDirectoryPath() + "/cpcp_capture_" + UUID.randomUUID().toString().replace("-", "").substring(0, 8) + ".jpg";
|
|
528
528
|
}
|
|
529
|
-
};
|
|
530
529
|
|
|
531
|
-
|
|
532
|
-
|
|
530
|
+
PictureCallback jpegPictureCallback = new PictureCallback() {
|
|
531
|
+
public void onPictureTaken(byte[] data, Camera arg1) {
|
|
532
|
+
Log.d(TAG, "CameraPreview jpegPictureCallback");
|
|
533
|
+
|
|
534
|
+
try {
|
|
535
|
+
if (!disableExifHeaderStripping) {
|
|
536
|
+
Matrix matrix = new Matrix();
|
|
537
|
+
if (cameraCurrentlyLocked == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
538
|
+
matrix.preScale(1.0f, -1.0f);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
|
|
542
|
+
int rotation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
|
|
543
|
+
int rotationInDegrees = exifToDegrees(rotation);
|
|
544
|
+
|
|
545
|
+
if (rotation != 0f) {
|
|
546
|
+
matrix.preRotate(rotationInDegrees);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Check if matrix has changed. In that case, apply matrix and override data
|
|
550
|
+
if (!matrix.isIdentity()) {
|
|
551
|
+
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
|
|
552
|
+
bitmap = applyMatrix(bitmap, matrix);
|
|
553
|
+
|
|
554
|
+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
555
|
+
bitmap.compress(CompressFormat.JPEG, currentQuality, outputStream);
|
|
556
|
+
data = outputStream.toByteArray();
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if (!storeToFile) {
|
|
561
|
+
String encodedImage = Base64.encodeToString(data, Base64.NO_WRAP);
|
|
562
|
+
|
|
563
|
+
eventListener.onPictureTaken(encodedImage);
|
|
564
|
+
} else {
|
|
565
|
+
String path = getTempFilePath();
|
|
566
|
+
FileOutputStream out = new FileOutputStream(path);
|
|
567
|
+
out.write(data);
|
|
568
|
+
out.close();
|
|
569
|
+
eventListener.onPictureTaken(path);
|
|
570
|
+
}
|
|
571
|
+
Log.d(TAG, "CameraPreview pictureTakenHandler called back");
|
|
572
|
+
} catch (OutOfMemoryError e) {
|
|
573
|
+
// most likely failed to allocate memory for rotateBitmap
|
|
574
|
+
Log.d(TAG, "CameraPreview OutOfMemoryError");
|
|
575
|
+
// failed to allocate memory
|
|
576
|
+
eventListener.onPictureTakenError("Picture too large (memory)");
|
|
577
|
+
} catch (IOException e) {
|
|
578
|
+
Log.d(TAG, "CameraPreview IOException");
|
|
579
|
+
eventListener.onPictureTakenError("IO Error when extracting exif");
|
|
580
|
+
} catch (Exception e) {
|
|
581
|
+
Log.d(TAG, "CameraPreview onPictureTaken general exception");
|
|
582
|
+
} finally {
|
|
583
|
+
canTakePicture = true;
|
|
584
|
+
mCamera.startPreview();
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
private Camera.Size getOptimalPictureSize(
|
|
590
|
+
final int width,
|
|
591
|
+
final int height,
|
|
592
|
+
final Camera.Size previewSize,
|
|
593
|
+
final List<Camera.Size> supportedSizes
|
|
594
|
+
) {
|
|
595
|
+
/*
|
|
533
596
|
get the supportedPictureSize that:
|
|
534
597
|
- matches exactly width and height
|
|
535
598
|
- has the closest aspect ratio to the preview aspect ratio
|
|
536
599
|
- has picture.width and picture.height closest to width and height
|
|
537
600
|
- has the highest supported picture width and height up to 2 Megapixel if width == 0 || height == 0
|
|
538
601
|
*/
|
|
539
|
-
|
|
602
|
+
Camera.Size size = mCamera.new Size(width, height);
|
|
540
603
|
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
604
|
+
// convert to landscape if necessary
|
|
605
|
+
if (size.width < size.height) {
|
|
606
|
+
int temp = size.width;
|
|
607
|
+
size.width = size.height;
|
|
608
|
+
size.height = temp;
|
|
609
|
+
}
|
|
547
610
|
|
|
548
|
-
|
|
611
|
+
Camera.Size requestedSize = mCamera.new Size(size.width, size.height);
|
|
549
612
|
|
|
550
|
-
|
|
613
|
+
double previewAspectRatio = (double) previewSize.width / (double) previewSize.height;
|
|
551
614
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
615
|
+
if (previewAspectRatio < 1.0) {
|
|
616
|
+
// reset ratio to landscape
|
|
617
|
+
previewAspectRatio = 1.0 / previewAspectRatio;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
Log.d(TAG, "CameraPreview previewAspectRatio " + previewAspectRatio);
|
|
556
621
|
|
|
557
|
-
|
|
622
|
+
double aspectTolerance = 0.1;
|
|
623
|
+
double bestDifference = Double.MAX_VALUE;
|
|
558
624
|
|
|
559
|
-
|
|
560
|
-
|
|
625
|
+
for (int i = 0; i < supportedSizes.size(); i++) {
|
|
626
|
+
Camera.Size supportedSize = supportedSizes.get(i);
|
|
561
627
|
|
|
562
|
-
|
|
563
|
-
|
|
628
|
+
// Perfect match
|
|
629
|
+
if (supportedSize.equals(requestedSize)) {
|
|
630
|
+
Log.d(TAG, "CameraPreview optimalPictureSize " + supportedSize.width + 'x' + supportedSize.height);
|
|
631
|
+
return supportedSize;
|
|
632
|
+
}
|
|
564
633
|
|
|
565
|
-
|
|
566
|
-
if (supportedSize.equals(requestedSize)) {
|
|
567
|
-
Log.d(TAG, "CameraPreview optimalPictureSize " + supportedSize.width + 'x' + supportedSize.height);
|
|
568
|
-
return supportedSize;
|
|
569
|
-
}
|
|
634
|
+
double difference = Math.abs(previewAspectRatio - ((double) supportedSize.width / (double) supportedSize.height));
|
|
570
635
|
|
|
571
|
-
|
|
636
|
+
if (difference < bestDifference - aspectTolerance) {
|
|
637
|
+
// better aspectRatio found
|
|
638
|
+
if ((width != 0 && height != 0) || (supportedSize.width * supportedSize.height < 2048 * 1024)) {
|
|
639
|
+
size.width = supportedSize.width;
|
|
640
|
+
size.height = supportedSize.height;
|
|
641
|
+
bestDifference = difference;
|
|
642
|
+
}
|
|
643
|
+
} else if (difference < bestDifference + aspectTolerance) {
|
|
644
|
+
// same aspectRatio found (within tolerance)
|
|
645
|
+
if (width == 0 || height == 0) {
|
|
646
|
+
// set highest supported resolution below 2 Megapixel
|
|
647
|
+
if ((size.width < supportedSize.width) && (supportedSize.width * supportedSize.height < 2048 * 1024)) {
|
|
648
|
+
size.width = supportedSize.width;
|
|
649
|
+
size.height = supportedSize.height;
|
|
650
|
+
}
|
|
651
|
+
} else {
|
|
652
|
+
// check if this pictureSize closer to requested width and height
|
|
653
|
+
if (
|
|
654
|
+
Math.abs(width * height - supportedSize.width * supportedSize.height) <
|
|
655
|
+
Math.abs(width * height - size.width * size.height)
|
|
656
|
+
) {
|
|
657
|
+
size.width = supportedSize.width;
|
|
658
|
+
size.height = supportedSize.height;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
Log.d(TAG, "CameraPreview optimalPictureSize " + size.width + 'x' + size.height);
|
|
664
|
+
return size;
|
|
665
|
+
}
|
|
572
666
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
if (
|
|
576
|
-
|
|
577
|
-
size.height = supportedSize.height;
|
|
578
|
-
bestDifference = difference;
|
|
667
|
+
static byte[] rotateNV21(final byte[] yuv, final int width, final int height, final int rotation) {
|
|
668
|
+
if (rotation == 0) return yuv;
|
|
669
|
+
if (rotation % 90 != 0 || rotation < 0 || rotation > 270) {
|
|
670
|
+
throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0");
|
|
579
671
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
672
|
+
|
|
673
|
+
final byte[] output = new byte[yuv.length];
|
|
674
|
+
final int frameSize = width * height;
|
|
675
|
+
final boolean swap = rotation % 180 != 0;
|
|
676
|
+
final boolean xflip = rotation % 270 != 0;
|
|
677
|
+
final boolean yflip = rotation >= 180;
|
|
678
|
+
|
|
679
|
+
for (int j = 0; j < height; j++) {
|
|
680
|
+
for (int i = 0; i < width; i++) {
|
|
681
|
+
final int yIn = j * width + i;
|
|
682
|
+
final int uIn = frameSize + (j >> 1) * width + (i & ~1);
|
|
683
|
+
final int vIn = uIn + 1;
|
|
684
|
+
|
|
685
|
+
final int wOut = swap ? height : width;
|
|
686
|
+
final int hOut = swap ? width : height;
|
|
687
|
+
final int iSwapped = swap ? j : i;
|
|
688
|
+
final int jSwapped = swap ? i : j;
|
|
689
|
+
final int iOut = xflip ? wOut - iSwapped - 1 : iSwapped;
|
|
690
|
+
final int jOut = yflip ? hOut - jSwapped - 1 : jSwapped;
|
|
691
|
+
|
|
692
|
+
final int yOut = jOut * wOut + iOut;
|
|
693
|
+
final int uOut = frameSize + (jOut >> 1) * wOut + (iOut & ~1);
|
|
694
|
+
final int vOut = uOut + 1;
|
|
695
|
+
|
|
696
|
+
output[yOut] = (byte) (0xff & yuv[yIn]);
|
|
697
|
+
output[uOut] = (byte) (0xff & yuv[uIn]);
|
|
698
|
+
output[vOut] = (byte) (0xff & yuv[vIn]);
|
|
699
|
+
}
|
|
594
700
|
}
|
|
595
|
-
|
|
701
|
+
return output;
|
|
596
702
|
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
final int height,
|
|
603
|
-
final int rotation)
|
|
604
|
-
{
|
|
605
|
-
if (rotation == 0) return yuv;
|
|
606
|
-
if (rotation % 90 != 0 || rotation < 0 || rotation > 270) {
|
|
607
|
-
throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0");
|
|
703
|
+
|
|
704
|
+
public void setOpacity(final float opacity) {
|
|
705
|
+
Log.d(TAG, "set opacity:" + opacity);
|
|
706
|
+
this.opacity = opacity;
|
|
707
|
+
mPreview.setOpacity(opacity);
|
|
608
708
|
}
|
|
609
709
|
|
|
610
|
-
final
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
710
|
+
public void takeSnapshot(final int quality) {
|
|
711
|
+
mCamera.setPreviewCallback(
|
|
712
|
+
new Camera.PreviewCallback() {
|
|
713
|
+
@Override
|
|
714
|
+
public void onPreviewFrame(byte[] bytes, Camera camera) {
|
|
715
|
+
try {
|
|
716
|
+
Camera.Parameters parameters = camera.getParameters();
|
|
717
|
+
Camera.Size size = parameters.getPreviewSize();
|
|
718
|
+
int orientation = mPreview.getDisplayOrientation();
|
|
719
|
+
if (mPreview.getCameraFacing() == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
720
|
+
bytes = rotateNV21(bytes, size.width, size.height, (360 - orientation) % 360);
|
|
721
|
+
} else {
|
|
722
|
+
bytes = rotateNV21(bytes, size.width, size.height, orientation);
|
|
723
|
+
}
|
|
724
|
+
// switch width/height when rotating 90/270 deg
|
|
725
|
+
Rect rect = orientation == 90 || orientation == 270
|
|
726
|
+
? new Rect(0, 0, size.height, size.width)
|
|
727
|
+
: new Rect(0, 0, size.width, size.height);
|
|
728
|
+
YuvImage yuvImage = new YuvImage(bytes, parameters.getPreviewFormat(), rect.width(), rect.height(), null);
|
|
729
|
+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
730
|
+
yuvImage.compressToJpeg(rect, quality, byteArrayOutputStream);
|
|
731
|
+
byte[] data = byteArrayOutputStream.toByteArray();
|
|
732
|
+
byteArrayOutputStream.close();
|
|
733
|
+
eventListener.onSnapshotTaken(Base64.encodeToString(data, Base64.NO_WRAP));
|
|
734
|
+
} catch (IOException e) {
|
|
735
|
+
Log.d(TAG, "CameraPreview IOException");
|
|
736
|
+
eventListener.onSnapshotTakenError("IO Error");
|
|
737
|
+
} finally {
|
|
738
|
+
mCamera.setPreviewCallback(null);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
);
|
|
637
743
|
}
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
744
|
+
|
|
745
|
+
public void takePicture(final int width, final int height, final int quality) {
|
|
746
|
+
Log.d(TAG, "CameraPreview takePicture width: " + width + ", height: " + height + ", quality: " + quality);
|
|
747
|
+
|
|
748
|
+
if (mPreview != null) {
|
|
749
|
+
if (!canTakePicture) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
canTakePicture = false;
|
|
754
|
+
|
|
755
|
+
new Thread() {
|
|
756
|
+
public void run() {
|
|
757
|
+
Camera.Parameters params = mCamera.getParameters();
|
|
758
|
+
|
|
759
|
+
Camera.Size size = getOptimalPictureSize(width, height, params.getPreviewSize(), params.getSupportedPictureSizes());
|
|
760
|
+
params.setPictureSize(size.width, size.height);
|
|
761
|
+
currentQuality = quality;
|
|
762
|
+
|
|
763
|
+
if (cameraCurrentlyLocked == Camera.CameraInfo.CAMERA_FACING_FRONT && !storeToFile) {
|
|
764
|
+
// The image will be recompressed in the callback
|
|
765
|
+
params.setJpegQuality(99);
|
|
766
|
+
} else {
|
|
767
|
+
params.setJpegQuality(quality);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
if (cameraCurrentlyLocked == Camera.CameraInfo.CAMERA_FACING_FRONT && disableExifHeaderStripping) {
|
|
771
|
+
Activity activity = getActivity();
|
|
772
|
+
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
|
|
773
|
+
int degrees = 0;
|
|
774
|
+
switch (rotation) {
|
|
775
|
+
case Surface.ROTATION_0:
|
|
776
|
+
degrees = 0;
|
|
777
|
+
break;
|
|
778
|
+
case Surface.ROTATION_90:
|
|
779
|
+
degrees = 180;
|
|
780
|
+
break;
|
|
781
|
+
case Surface.ROTATION_180:
|
|
782
|
+
degrees = 270;
|
|
783
|
+
break;
|
|
784
|
+
case Surface.ROTATION_270:
|
|
785
|
+
degrees = 0;
|
|
786
|
+
break;
|
|
787
|
+
}
|
|
788
|
+
int orientation;
|
|
789
|
+
Camera.CameraInfo info = new Camera.CameraInfo();
|
|
790
|
+
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
791
|
+
orientation = (info.orientation + degrees) % 360;
|
|
792
|
+
if (degrees != 0) {
|
|
793
|
+
orientation = (360 - orientation) % 360;
|
|
794
|
+
}
|
|
795
|
+
} else {
|
|
796
|
+
orientation = (info.orientation - degrees + 360) % 360;
|
|
797
|
+
}
|
|
798
|
+
params.setRotation(orientation);
|
|
799
|
+
} else {
|
|
800
|
+
params.setRotation(mPreview.getDisplayOrientation());
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
mCamera.setParameters(params);
|
|
804
|
+
mCamera.takePicture(shutterCallback, null, jpegPictureCallback);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
.start();
|
|
808
|
+
} else {
|
|
809
|
+
canTakePicture = true;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
public void startRecord(
|
|
814
|
+
final String filePath,
|
|
815
|
+
final String camera,
|
|
816
|
+
final int width,
|
|
817
|
+
final int height,
|
|
818
|
+
final int quality,
|
|
819
|
+
final boolean withFlash,
|
|
820
|
+
final int maxDuration
|
|
821
|
+
) {
|
|
822
|
+
Log.d(TAG, "CameraPreview startRecord camera: " + camera + " width: " + width + ", height: " + height + ", quality: " + quality);
|
|
823
|
+
Activity activity = getActivity();
|
|
824
|
+
muteStream(true, activity);
|
|
825
|
+
if (this.mRecordingState == RecordingState.STARTED) {
|
|
826
|
+
Log.d(TAG, "Already Recording");
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
this.recordFilePath = filePath;
|
|
831
|
+
int mOrientationHint = calculateOrientationHint();
|
|
832
|
+
int videoWidth = 0; //set whatever
|
|
833
|
+
int videoHeight = 0; //set whatever
|
|
834
|
+
|
|
835
|
+
Camera.Parameters cameraParams = mCamera.getParameters();
|
|
836
|
+
if (withFlash) {
|
|
837
|
+
cameraParams.setFlashMode(withFlash ? Camera.Parameters.FLASH_MODE_TORCH : Camera.Parameters.FLASH_MODE_OFF);
|
|
838
|
+
mCamera.setParameters(cameraParams);
|
|
839
|
+
mCamera.startPreview();
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
mCamera.unlock();
|
|
843
|
+
mRecorder = new MediaRecorder();
|
|
844
|
+
|
|
650
845
|
try {
|
|
651
|
-
|
|
652
|
-
Camera.Size size = parameters.getPreviewSize();
|
|
653
|
-
int orientation = mPreview.getDisplayOrientation();
|
|
654
|
-
if (mPreview.getCameraFacing() == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
655
|
-
bytes = rotateNV21(bytes, size.width, size.height, (360 - orientation) % 360);
|
|
656
|
-
} else {
|
|
657
|
-
bytes = rotateNV21(bytes, size.width, size.height, orientation);
|
|
658
|
-
}
|
|
659
|
-
// switch width/height when rotating 90/270 deg
|
|
660
|
-
Rect rect = orientation == 90 || orientation == 270 ?
|
|
661
|
-
new Rect(0, 0, size.height, size.width) :
|
|
662
|
-
new Rect(0, 0, size.width, size.height);
|
|
663
|
-
YuvImage yuvImage = new YuvImage(bytes, parameters.getPreviewFormat(), rect.width(), rect.height(), null);
|
|
664
|
-
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
665
|
-
yuvImage.compressToJpeg(rect, quality, byteArrayOutputStream);
|
|
666
|
-
byte[] data = byteArrayOutputStream.toByteArray();
|
|
667
|
-
byteArrayOutputStream.close();
|
|
668
|
-
eventListener.onSnapshotTaken(Base64.encodeToString(data, Base64.NO_WRAP));
|
|
669
|
-
} catch (IOException e) {
|
|
670
|
-
Log.d(TAG, "CameraPreview IOException");
|
|
671
|
-
eventListener.onSnapshotTakenError("IO Error");
|
|
672
|
-
} finally {
|
|
846
|
+
mRecorder.setCamera(mCamera);
|
|
673
847
|
|
|
674
|
-
|
|
848
|
+
CamcorderProfile profile;
|
|
849
|
+
if (CamcorderProfile.hasProfile(defaultCameraId, CamcorderProfile.QUALITY_HIGH)) {
|
|
850
|
+
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_HIGH);
|
|
851
|
+
} else {
|
|
852
|
+
if (CamcorderProfile.hasProfile(defaultCameraId, CamcorderProfile.QUALITY_480P)) {
|
|
853
|
+
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_480P);
|
|
854
|
+
} else {
|
|
855
|
+
if (CamcorderProfile.hasProfile(defaultCameraId, CamcorderProfile.QUALITY_720P)) {
|
|
856
|
+
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_720P);
|
|
857
|
+
} else {
|
|
858
|
+
if (CamcorderProfile.hasProfile(defaultCameraId, CamcorderProfile.QUALITY_1080P)) {
|
|
859
|
+
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_1080P);
|
|
860
|
+
} else {
|
|
861
|
+
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_LOW);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
|
|
868
|
+
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
|
|
869
|
+
mRecorder.setProfile(profile);
|
|
870
|
+
mRecorder.setOutputFile(filePath);
|
|
871
|
+
mRecorder.setOrientationHint(mOrientationHint);
|
|
872
|
+
mRecorder.setMaxDuration(maxDuration);
|
|
873
|
+
|
|
874
|
+
mRecorder.prepare();
|
|
875
|
+
Log.d(TAG, "Starting recording");
|
|
876
|
+
mRecorder.start();
|
|
877
|
+
eventListener.onStartRecordVideo();
|
|
878
|
+
} catch (IOException e) {
|
|
879
|
+
eventListener.onStartRecordVideoError(e.getMessage());
|
|
675
880
|
}
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
public void run() {
|
|
692
|
-
Camera.Parameters params = mCamera.getParameters();
|
|
693
|
-
|
|
694
|
-
Camera.Size size = getOptimalPictureSize(width, height, params.getPreviewSize(), params.getSupportedPictureSizes());
|
|
695
|
-
params.setPictureSize(size.width, size.height);
|
|
696
|
-
currentQuality = quality;
|
|
697
|
-
|
|
698
|
-
if(cameraCurrentlyLocked == Camera.CameraInfo.CAMERA_FACING_FRONT && !storeToFile) {
|
|
699
|
-
// The image will be recompressed in the callback
|
|
700
|
-
params.setJpegQuality(99);
|
|
701
|
-
} else {
|
|
702
|
-
params.setJpegQuality(quality);
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
if(cameraCurrentlyLocked == Camera.CameraInfo.CAMERA_FACING_FRONT && disableExifHeaderStripping) {
|
|
706
|
-
Activity activity = getActivity();
|
|
707
|
-
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
|
|
708
|
-
int degrees = 0;
|
|
709
|
-
switch (rotation) {
|
|
710
|
-
case Surface.ROTATION_0:
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
public int calculateOrientationHint() {
|
|
884
|
+
DisplayMetrics dm = new DisplayMetrics();
|
|
885
|
+
Camera.CameraInfo info = new Camera.CameraInfo();
|
|
886
|
+
Camera.getCameraInfo(defaultCameraId, info);
|
|
887
|
+
int cameraRotationOffset = info.orientation;
|
|
888
|
+
Activity activity = getActivity();
|
|
889
|
+
|
|
890
|
+
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
|
|
891
|
+
int currentScreenRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
|
|
892
|
+
|
|
893
|
+
int degrees = 0;
|
|
894
|
+
switch (currentScreenRotation) {
|
|
895
|
+
case Surface.ROTATION_0:
|
|
711
896
|
degrees = 0;
|
|
712
897
|
break;
|
|
713
|
-
|
|
898
|
+
case Surface.ROTATION_90:
|
|
899
|
+
degrees = 90;
|
|
900
|
+
break;
|
|
901
|
+
case Surface.ROTATION_180:
|
|
714
902
|
degrees = 180;
|
|
715
903
|
break;
|
|
716
|
-
|
|
904
|
+
case Surface.ROTATION_270:
|
|
717
905
|
degrees = 270;
|
|
718
906
|
break;
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
726
|
-
orientation = (info.orientation + degrees) % 360;
|
|
727
|
-
if (degrees != 0) {
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
int orientation;
|
|
910
|
+
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
911
|
+
orientation = (cameraRotationOffset + degrees) % 360;
|
|
912
|
+
if (degrees != 0) {
|
|
728
913
|
orientation = (360 - orientation) % 360;
|
|
729
|
-
}
|
|
730
|
-
} else {
|
|
731
|
-
orientation = (info.orientation - degrees + 360) % 360;
|
|
732
914
|
}
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
params.setRotation(mPreview.getDisplayOrientation());
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
mCamera.setParameters(params);
|
|
739
|
-
mCamera.takePicture(shutterCallback, null, jpegPictureCallback);
|
|
915
|
+
} else {
|
|
916
|
+
orientation = (cameraRotationOffset - degrees + 360) % 360;
|
|
740
917
|
}
|
|
741
|
-
|
|
742
|
-
} else {
|
|
743
|
-
canTakePicture = true;
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
public void startRecord(final String filePath, final String camera, final int width, final int height, final int quality, final boolean withFlash, final int maxDuration){
|
|
748
|
-
Log.d(TAG, "CameraPreview startRecord camera: " + camera + " width: " + width + ", height: " + height + ", quality: " + quality);
|
|
749
|
-
Activity activity = getActivity();
|
|
750
|
-
muteStream(true, activity);
|
|
751
|
-
if (this.mRecordingState == RecordingState.STARTED) {
|
|
752
|
-
Log.d(TAG, "Already Recording");
|
|
753
|
-
return;
|
|
754
|
-
}
|
|
918
|
+
Log.w(TAG, "************orientationHint ***********= " + orientation);
|
|
755
919
|
|
|
756
|
-
|
|
757
|
-
int mOrientationHint = calculateOrientationHint();
|
|
758
|
-
int videoWidth = 0;//set whatever
|
|
759
|
-
int videoHeight = 0;//set whatever
|
|
760
|
-
|
|
761
|
-
Camera.Parameters cameraParams = mCamera.getParameters();
|
|
762
|
-
if (withFlash) {
|
|
763
|
-
cameraParams.setFlashMode(withFlash ? Camera.Parameters.FLASH_MODE_TORCH : Camera.Parameters.FLASH_MODE_OFF);
|
|
764
|
-
mCamera.setParameters(cameraParams);
|
|
765
|
-
mCamera.startPreview();
|
|
920
|
+
return orientation;
|
|
766
921
|
}
|
|
767
922
|
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
try {
|
|
772
|
-
mRecorder.setCamera(mCamera);
|
|
923
|
+
public void stopRecord() {
|
|
924
|
+
Log.d(TAG, "stopRecord");
|
|
773
925
|
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
profile = CamcorderProfile.get(defaultCameraId, CamcorderProfile.QUALITY_LOW);
|
|
788
|
-
}
|
|
789
|
-
}
|
|
926
|
+
try {
|
|
927
|
+
mRecorder.stop();
|
|
928
|
+
mRecorder.reset(); // clear recorder configuration
|
|
929
|
+
mRecorder.release(); // release the recorder object
|
|
930
|
+
mRecorder = null;
|
|
931
|
+
mCamera.lock();
|
|
932
|
+
Camera.Parameters cameraParams = mCamera.getParameters();
|
|
933
|
+
cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
|
934
|
+
mCamera.setParameters(cameraParams);
|
|
935
|
+
mCamera.startPreview();
|
|
936
|
+
eventListener.onStopRecordVideo(this.recordFilePath);
|
|
937
|
+
} catch (Exception e) {
|
|
938
|
+
eventListener.onStopRecordVideoError(e.getMessage());
|
|
790
939
|
}
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
|
|
795
|
-
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
|
|
796
|
-
mRecorder.setProfile(profile);
|
|
797
|
-
mRecorder.setOutputFile(filePath);
|
|
798
|
-
mRecorder.setOrientationHint(mOrientationHint);
|
|
799
|
-
mRecorder.setMaxDuration(maxDuration);
|
|
800
|
-
|
|
801
|
-
mRecorder.prepare();
|
|
802
|
-
Log.d(TAG, "Starting recording");
|
|
803
|
-
mRecorder.start();
|
|
804
|
-
eventListener.onStartRecordVideo();
|
|
805
|
-
} catch (IOException e) {
|
|
806
|
-
eventListener.onStartRecordVideoError(e.getMessage());
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
public int calculateOrientationHint() {
|
|
811
|
-
DisplayMetrics dm = new DisplayMetrics();
|
|
812
|
-
Camera.CameraInfo info = new Camera.CameraInfo();
|
|
813
|
-
Camera.getCameraInfo(defaultCameraId, info);
|
|
814
|
-
int cameraRotationOffset = info.orientation;
|
|
815
|
-
Activity activity = getActivity();
|
|
816
|
-
|
|
817
|
-
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
|
|
818
|
-
int currentScreenRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
|
|
819
|
-
|
|
820
|
-
int degrees = 0;
|
|
821
|
-
switch (currentScreenRotation) {
|
|
822
|
-
case Surface.ROTATION_0:
|
|
823
|
-
degrees = 0;
|
|
824
|
-
break;
|
|
825
|
-
case Surface.ROTATION_90:
|
|
826
|
-
degrees = 90;
|
|
827
|
-
break;
|
|
828
|
-
case Surface.ROTATION_180:
|
|
829
|
-
degrees = 180;
|
|
830
|
-
break;
|
|
831
|
-
case Surface.ROTATION_270:
|
|
832
|
-
degrees = 270;
|
|
833
|
-
break;
|
|
834
940
|
}
|
|
835
941
|
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
if (degrees != 0) {
|
|
840
|
-
orientation = (360 - orientation) % 360;
|
|
841
|
-
}
|
|
842
|
-
} else {
|
|
843
|
-
orientation = (cameraRotationOffset - degrees + 360) % 360;
|
|
942
|
+
public void muteStream(boolean mute, Activity activity) {
|
|
943
|
+
AudioManager audioManager = ((AudioManager) activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE));
|
|
944
|
+
int direction = mute ? audioManager.ADJUST_MUTE : audioManager.ADJUST_UNMUTE;
|
|
844
945
|
}
|
|
845
|
-
Log.w(TAG, "************orientationHint ***********= " + orientation);
|
|
846
|
-
|
|
847
|
-
return orientation;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
public void stopRecord() {
|
|
851
|
-
Log.d(TAG, "stopRecord");
|
|
852
|
-
|
|
853
|
-
try {
|
|
854
|
-
mRecorder.stop();
|
|
855
|
-
mRecorder.reset(); // clear recorder configuration
|
|
856
|
-
mRecorder.release(); // release the recorder object
|
|
857
|
-
mRecorder = null;
|
|
858
|
-
mCamera.lock();
|
|
859
|
-
Camera.Parameters cameraParams = mCamera.getParameters();
|
|
860
|
-
cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
|
861
|
-
mCamera.setParameters(cameraParams);
|
|
862
|
-
mCamera.startPreview();
|
|
863
|
-
eventListener.onStopRecordVideo(this.recordFilePath);
|
|
864
|
-
} catch (Exception e) {
|
|
865
|
-
eventListener.onStopRecordVideoError(e.getMessage());
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
public void muteStream(boolean mute, Activity activity) {
|
|
870
|
-
AudioManager audioManager = ((AudioManager)activity.getApplicationContext().getSystemService(Context.AUDIO_SERVICE));
|
|
871
|
-
int direction = mute ? audioManager.ADJUST_MUTE : audioManager.ADJUST_UNMUTE;
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
public void setFocusArea(final int pointX, final int pointY, final Camera.AutoFocusCallback callback) {
|
|
875
|
-
if (mCamera != null) {
|
|
876
946
|
|
|
877
|
-
|
|
947
|
+
public void setFocusArea(final int pointX, final int pointY, final Camera.AutoFocusCallback callback) {
|
|
948
|
+
if (mCamera != null) {
|
|
949
|
+
mCamera.cancelAutoFocus();
|
|
878
950
|
|
|
879
|
-
|
|
951
|
+
Camera.Parameters parameters = mCamera.getParameters();
|
|
880
952
|
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
953
|
+
Rect focusRect = calculateTapArea(pointX, pointY, 1f);
|
|
954
|
+
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
|
|
955
|
+
parameters.setFocusAreas(Arrays.asList(new Camera.Area(focusRect, 1000)));
|
|
884
956
|
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
957
|
+
if (parameters.getMaxNumMeteringAreas() > 0) {
|
|
958
|
+
Rect meteringRect = calculateTapArea(pointX, pointY, 1.5f);
|
|
959
|
+
parameters.setMeteringAreas(Arrays.asList(new Camera.Area(meteringRect, 1000)));
|
|
960
|
+
}
|
|
889
961
|
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
962
|
+
try {
|
|
963
|
+
setCameraParameters(parameters);
|
|
964
|
+
mCamera.autoFocus(callback);
|
|
965
|
+
} catch (Exception e) {
|
|
966
|
+
Log.d(TAG, e.getMessage());
|
|
967
|
+
callback.onAutoFocus(false, this.mCamera);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
897
970
|
}
|
|
898
|
-
}
|
|
899
971
|
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
972
|
+
private Rect calculateTapArea(float x, float y, float coefficient) {
|
|
973
|
+
if (x < 100) {
|
|
974
|
+
x = 100;
|
|
975
|
+
}
|
|
976
|
+
if (x > width - 100) {
|
|
977
|
+
x = width - 100;
|
|
978
|
+
}
|
|
979
|
+
if (y < 100) {
|
|
980
|
+
y = 100;
|
|
981
|
+
}
|
|
982
|
+
if (y > height - 100) {
|
|
983
|
+
y = height - 100;
|
|
984
|
+
}
|
|
985
|
+
return new Rect(
|
|
986
|
+
Math.round((x - 100) * 2000 / width - 1000),
|
|
987
|
+
Math.round((y - 100) * 2000 / height - 1000),
|
|
988
|
+
Math.round((x + 100) * 2000 / width - 1000),
|
|
989
|
+
Math.round((y + 100) * 2000 / height - 1000)
|
|
990
|
+
);
|
|
912
991
|
}
|
|
913
|
-
return new Rect(
|
|
914
|
-
Math.round((x - 100) * 2000 / width - 1000),
|
|
915
|
-
Math.round((y - 100) * 2000 / height - 1000),
|
|
916
|
-
Math.round((x + 100) * 2000 / width - 1000),
|
|
917
|
-
Math.round((y + 100) * 2000 / height - 1000)
|
|
918
|
-
);
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
/**
|
|
922
|
-
* Determine the space between the first two fingers
|
|
923
|
-
*/
|
|
924
|
-
private static float getFingerSpacing(MotionEvent event) {
|
|
925
|
-
// ...
|
|
926
|
-
float x = event.getX(0) - event.getX(1);
|
|
927
|
-
float y = event.getY(0) - event.getY(1);
|
|
928
|
-
return (float) Math.sqrt(x * x + y * y);
|
|
929
|
-
}
|
|
930
992
|
|
|
993
|
+
/**
|
|
994
|
+
* Determine the space between the first two fingers
|
|
995
|
+
*/
|
|
996
|
+
private static float getFingerSpacing(MotionEvent event) {
|
|
997
|
+
// ...
|
|
998
|
+
float x = event.getX(0) - event.getX(1);
|
|
999
|
+
float y = event.getY(0) - event.getY(1);
|
|
1000
|
+
return (float) Math.sqrt(x * x + y * y);
|
|
1001
|
+
}
|
|
931
1002
|
}
|