RCTCameraView.java
6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Created by Fabrice Armisen (farmisen@gmail.com) on 1/3/16.
*/
package com.lwansbrough.RCTCamera;
import android.content.Context;
import android.hardware.SensorManager;
import android.view.OrientationEventListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View;
import java.util.List;
public class RCTCameraView extends ViewGroup {
private final OrientationEventListener _orientationListener;
private final Context _context;
private RCTCameraViewFinder _viewFinder = null;
private int _actualDeviceOrientation = -1;
private int _aspect = RCTCameraModule.RCT_CAMERA_ASPECT_FIT;
private String _captureQuality = "high";
private int _torchMode = -1;
private int _flashMode = -1;
public RCTCameraView(Context context) {
super(context);
this._context = context;
RCTCamera.createInstance(getDeviceOrientation(context));
_orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
if (setActualDeviceOrientation(_context)) {
layoutViewFinder();
}
}
};
if (_orientationListener.canDetectOrientation()) {
_orientationListener.enable();
} else {
_orientationListener.disable();
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
layoutViewFinder(left, top, right, bottom);
}
@Override
public void onViewAdded(View child) {
if (this._viewFinder == child) return;
// remove and readd view to make sure it is in the back.
// @TODO figure out why there was a z order issue in the first place and fix accordingly.
this.removeView(this._viewFinder);
this.addView(this._viewFinder, 0);
}
public void setAspect(int aspect) {
this._aspect = aspect;
layoutViewFinder();
}
public void setCameraType(final int type) {
if (null != this._viewFinder) {
this._viewFinder.setCameraType(type);
RCTCamera.getInstance().adjustPreviewLayout(type);
} else {
_viewFinder = new RCTCameraViewFinder(_context, type);
if (-1 != this._flashMode) {
_viewFinder.setFlashMode(this._flashMode);
}
if (-1 != this._torchMode) {
_viewFinder.setFlashMode(this._torchMode);
}
addView(_viewFinder);
}
}
public void setCaptureQuality(String captureQuality) {
this._captureQuality = captureQuality;
if (this._viewFinder != null) {
this._viewFinder.setCaptureQuality(captureQuality);
}
}
public void setTorchMode(int torchMode) {
this._torchMode = torchMode;
if (this._viewFinder != null) {
this._viewFinder.setTorchMode(torchMode);
}
}
public void setFlashMode(int flashMode) {
this._flashMode = flashMode;
if (this._viewFinder != null) {
this._viewFinder.setFlashMode(flashMode);
}
}
public void setOrientation(int orientation) {
RCTCamera.getInstance().setOrientation(orientation);
if (this._viewFinder != null) {
layoutViewFinder();
}
}
public void setBarcodeScannerEnabled(boolean barcodeScannerEnabled) {
RCTCamera.getInstance().setBarcodeScannerEnabled(barcodeScannerEnabled);
}
public void setBarCodeTypes(List<String> types) {
RCTCamera.getInstance().setBarCodeTypes(types);
}
private boolean setActualDeviceOrientation(Context context) {
int actualDeviceOrientation = getDeviceOrientation(context);
if (_actualDeviceOrientation != actualDeviceOrientation) {
_actualDeviceOrientation = actualDeviceOrientation;
RCTCamera.getInstance().setActualDeviceOrientation(_actualDeviceOrientation);
return true;
} else {
return false;
}
}
private int getDeviceOrientation(Context context) {
return ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
}
private void layoutViewFinder() {
layoutViewFinder(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
}
private void layoutViewFinder(int left, int top, int right, int bottom) {
if (null == _viewFinder) {
return;
}
float width = right - left;
float height = bottom - top;
int viewfinderWidth;
int viewfinderHeight;
double ratio;
switch (this._aspect) {
case RCTCameraModule.RCT_CAMERA_ASPECT_FIT:
ratio = this._viewFinder.getRatio();
if (ratio * height > width) {
viewfinderHeight = (int) (width / ratio);
viewfinderWidth = (int) width;
} else {
viewfinderWidth = (int) (ratio * height);
viewfinderHeight = (int) height;
}
break;
case RCTCameraModule.RCT_CAMERA_ASPECT_FILL:
ratio = this._viewFinder.getRatio();
if (ratio * height < width) {
viewfinderHeight = (int) (width / ratio);
viewfinderWidth = (int) width;
} else {
viewfinderWidth = (int) (ratio * height);
viewfinderHeight = (int) height;
}
break;
default:
viewfinderWidth = (int) width;
viewfinderHeight = (int) height;
}
int viewFinderPaddingX = (int) ((width - viewfinderWidth) / 2);
int viewFinderPaddingY = (int) ((height - viewfinderHeight) / 2);
this._viewFinder.layout(viewFinderPaddingX, viewFinderPaddingY, viewFinderPaddingX + viewfinderWidth, viewFinderPaddingY + viewfinderHeight);
this.postInvalidate(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
}
}