5
5
import android .view .View ;
6
6
import android .widget .CompoundButton ;
7
7
import android .widget .ImageView ;
8
- import android .widget .SeekBar ;
8
+ import android .widget .TextView ;
9
9
import android .widget .Toast ;
10
10
11
11
import org .jetbrains .annotations .NotNull ;
12
12
import org .jetbrains .annotations .Nullable ;
13
13
14
14
import java .io .File ;
15
+ import java .util .Locale ;
15
16
16
17
import androidx .annotation .NonNull ;
17
18
import androidx .appcompat .app .AppCompatActivity ;
18
19
import androidx .appcompat .widget .SwitchCompat ;
19
20
import io .fotoapparat .Fotoapparat ;
21
+ import io .fotoapparat .capability .Capabilities ;
20
22
import io .fotoapparat .configuration .CameraConfiguration ;
21
23
import io .fotoapparat .configuration .UpdateConfiguration ;
22
24
import io .fotoapparat .error .CameraErrorListener ;
23
25
import io .fotoapparat .exception .camera .CameraException ;
24
26
import io .fotoapparat .parameter .ScaleType ;
27
+ import io .fotoapparat .parameter .Zoom ;
25
28
import io .fotoapparat .preview .Frame ;
26
29
import io .fotoapparat .preview .FrameProcessor ;
27
30
import io .fotoapparat .result .BitmapPhoto ;
28
31
import io .fotoapparat .result .PhotoResult ;
29
32
import io .fotoapparat .result .WhenDoneListener ;
30
33
import io .fotoapparat .view .CameraView ;
31
34
import io .fotoapparat .view .FocusView ;
35
+ import kotlin .Unit ;
36
+ import kotlin .jvm .functions .Function1 ;
32
37
33
38
import static io .fotoapparat .log .LoggersKt .fileLogger ;
34
39
import static io .fotoapparat .log .LoggersKt .logcat ;
@@ -57,9 +62,13 @@ public class ActivityJava extends AppCompatActivity {
57
62
private boolean hasCameraPermission ;
58
63
private CameraView cameraView ;
59
64
private FocusView focusView ;
65
+ private TextView zoomLvl ;
66
+ private ImageView switchCamera ;
60
67
private View capture ;
61
68
62
69
private Fotoapparat fotoapparat ;
70
+ private Zoom .VariableZoom cameraZoom ;
71
+ private float curZoom = 0f ;
63
72
64
73
boolean activeCameraBack = true ;
65
74
@@ -92,6 +101,8 @@ protected void onCreate(Bundle savedInstanceState) {
92
101
cameraView = findViewById (R .id .cameraView );
93
102
focusView = findViewById (R .id .focusView );
94
103
capture = findViewById (R .id .capture );
104
+ zoomLvl = findViewById (R .id .zoomLvl );
105
+ switchCamera = findViewById (R .id .switchCamera );
95
106
hasCameraPermission = permissionsDelegate .hasCameraPermission ();
96
107
97
108
if (hasCameraPermission ) {
@@ -105,7 +116,6 @@ protected void onCreate(Bundle savedInstanceState) {
105
116
takePictureOnClick ();
106
117
switchCameraOnClick ();
107
118
toggleTorchOnSwitch ();
108
- zoomSeekBar ();
109
119
}
110
120
111
121
private Fotoapparat createFotoapparat () {
@@ -129,15 +139,66 @@ public void onError(@NotNull CameraException e) {
129
139
.build ();
130
140
}
131
141
132
- private void zoomSeekBar () {
133
- SeekBar seekBar = findViewById (R .id .zoomSeekBar );
134
-
135
- seekBar .setOnSeekBarChangeListener (new OnProgressChanged () {
142
+ private void adjustViewsVisibility () {
143
+ fotoapparat .getCapabilities ().whenAvailable (new Function1 <Capabilities , Unit >() {
136
144
@ Override
137
- public void onProgressChanged (SeekBar seekBar , int progress , boolean fromUser ) {
138
- fotoapparat .setZoom (progress / (float ) seekBar .getMax ());
145
+ public Unit invoke (Capabilities capabilities ) {
146
+ Zoom zoom = capabilities != null ? capabilities .getZoom () : null ;
147
+ if (zoom instanceof Zoom .VariableZoom ){
148
+ cameraZoom = (Zoom .VariableZoom ) zoom ;
149
+ focusView .setScaleListener (new Function1 <Float , Unit >() {
150
+ @ Override
151
+ public Unit invoke (Float aFloat ) {
152
+ scaleZoom (aFloat );
153
+ return null ;
154
+ }
155
+ });
156
+ focusView .setPtrListener (new Function1 <Integer , Unit >() {
157
+ @ Override
158
+ public Unit invoke (Integer integer ) {
159
+ pointerChanged (integer );
160
+ return null ;
161
+ }
162
+ });
163
+ } else {
164
+ zoomLvl .setVisibility (View .GONE );
165
+ focusView .setScaleListener (null );
166
+ focusView .setPtrListener (null );
167
+ }
168
+ return null ;
139
169
}
140
170
});
171
+ if (fotoapparat .isAvailable (front ())){
172
+ switchCamera .setVisibility (View .VISIBLE );
173
+ } else {
174
+ switchCamera .setVisibility (View .GONE );
175
+ }
176
+ }
177
+
178
+ private void scaleZoom (float scaleFactor ){
179
+ float plusZoom = 0 ;
180
+ if (scaleFactor < 1 ) plusZoom = -1 * (1 - scaleFactor );
181
+ else plusZoom = scaleFactor - 1 ;
182
+
183
+ float newZoom = curZoom + plusZoom ;
184
+ if (newZoom < 0 || newZoom > 1 ) return ;
185
+
186
+ curZoom = newZoom ;
187
+ fotoapparat .setZoom (curZoom );
188
+
189
+ int progress = Math .round (cameraZoom .getMaxZoom () * curZoom );
190
+ int value = cameraZoom .getZoomRatios ().get (progress );
191
+
192
+ float roundedValue = (float )(Math .round (((float )value ) / 10f )) / 10f ;
193
+
194
+ zoomLvl .setVisibility (View .VISIBLE );
195
+ zoomLvl .setText (String .format (Locale .getDefault (), "%.1f×" , roundedValue ));
196
+ }
197
+
198
+ private void pointerChanged (int fingerCount ){
199
+ if (fingerCount == 0 ) {
200
+ zoomLvl .setVisibility (View .GONE );
201
+ }
141
202
}
142
203
143
204
private void switchCameraOnClick () {
@@ -180,6 +241,7 @@ public void onClick(View v) {
180
241
activeCameraBack ? back () : front (),
181
242
cameraConfiguration
182
243
);
244
+ adjustViewsVisibility ();
183
245
}
184
246
});
185
247
}
@@ -223,6 +285,7 @@ protected void onStart() {
223
285
super .onStart ();
224
286
if (hasCameraPermission ) {
225
287
fotoapparat .start ();
288
+ adjustViewsVisibility ();
226
289
}
227
290
}
228
291
@@ -242,6 +305,7 @@ public void onRequestPermissionsResult(int requestCode,
242
305
if (permissionsDelegate .resultGranted (requestCode , permissions , grantResults )) {
243
306
hasCameraPermission = true ;
244
307
fotoapparat .start ();
308
+ adjustViewsVisibility ();
245
309
cameraView .setVisibility (View .VISIBLE );
246
310
}
247
311
}
0 commit comments