This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[camera] android-rework part 4: Android flash and zoom features #3798
Merged
mvanbeusekom
merged 12 commits into
flutter:master
from
Baseflow:camera-android/flash_zoom_features
May 25, 2021
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2b7aa9b
Base classes to support Android camera features
mvanbeusekom f780742
Fixed formatting
mvanbeusekom 76bc5bd
Applied feedback from PR
mvanbeusekom 547f58a
Added Android Flash and Zoom features
mvanbeusekom 215fe53
Use mockito-inline
mvanbeusekom 3402d95
Fix formatting issue
mvanbeusekom 6fcaa5f
Merge remote-tracking branch 'upstream/master' into camera-android/fl…
mvanbeusekom 37d06c8
Merge remote-tracking branch 'upstream/master' into camera-android/fl…
mvanbeusekom fab71a5
Processed feedback on pull request.
mvanbeusekom 788bbf9
Fixed formatting
mvanbeusekom a87045a
Fixed formatting
mvanbeusekom 64e2e98
Swap docs to match correct methods
mvanbeusekom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...a/camera/android/src/main/java/io/flutter/plugins/camera/features/flash/FlashFeature.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camera.features.flash; | ||
|
||
import android.hardware.camera2.CaptureRequest; | ||
import io.flutter.plugins.camera.CameraProperties; | ||
import io.flutter.plugins.camera.features.CameraFeature; | ||
|
||
/** Controls the flash configuration on the {@link android.hardware.camera2} API. */ | ||
public class FlashFeature extends CameraFeature<FlashMode> { | ||
private FlashMode currentSetting = FlashMode.auto; | ||
|
||
/** | ||
* Creates a new instance of the {@link FlashFeature}. | ||
* | ||
* @param cameraProperties Collection of characteristics for the current camera device. | ||
*/ | ||
public FlashFeature(CameraProperties cameraProperties) { | ||
super(cameraProperties); | ||
} | ||
|
||
@Override | ||
public String getDebugName() { | ||
return "FlashFeature"; | ||
} | ||
|
||
@Override | ||
public FlashMode getValue() { | ||
return currentSetting; | ||
} | ||
|
||
@Override | ||
public void setValue(FlashMode value) { | ||
this.currentSetting = value; | ||
} | ||
|
||
@Override | ||
public boolean checkIsSupported() { | ||
Boolean available = cameraProperties.getFlashInfoAvailable(); | ||
return available != null && available; | ||
} | ||
|
||
@Override | ||
public void updateBuilder(CaptureRequest.Builder requestBuilder) { | ||
if (!checkIsSupported()) { | ||
return; | ||
} | ||
|
||
switch (currentSetting) { | ||
case off: | ||
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); | ||
requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); | ||
break; | ||
|
||
case always: | ||
requestBuilder.set( | ||
CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH); | ||
requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); | ||
break; | ||
|
||
case torch: | ||
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); | ||
requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH); | ||
break; | ||
|
||
case auto: | ||
requestBuilder.set( | ||
CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); | ||
requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); | ||
break; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...mera/camera/android/src/main/java/io/flutter/plugins/camera/features/flash/FlashMode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camera.features.flash; | ||
|
||
// Mirrors flash_mode.dart | ||
public enum FlashMode { | ||
off("off"), | ||
auto("auto"), | ||
always("always"), | ||
torch("torch"); | ||
|
||
private final String strValue; | ||
|
||
FlashMode(String strValue) { | ||
this.strValue = strValue; | ||
} | ||
|
||
public static FlashMode getValueForString(String modeStr) { | ||
for (FlashMode value : values()) { | ||
if (value.strValue.equals(modeStr)) return value; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return strValue; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
.../android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeature.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camera.features.zoomlevel; | ||
|
||
import android.graphics.Rect; | ||
import android.hardware.camera2.CaptureRequest; | ||
import io.flutter.plugins.camera.CameraProperties; | ||
import io.flutter.plugins.camera.features.CameraFeature; | ||
|
||
/** Controls the zoom configuration on the {@link android.hardware.camera2} API. */ | ||
public class ZoomLevelFeature extends CameraFeature<Float> { | ||
private static final float MINIMUM_ZOOM_LEVEL = 1.0f; | ||
private final boolean hasSupport; | ||
private final Rect sensorArraySize; | ||
private Float currentSetting = MINIMUM_ZOOM_LEVEL; | ||
private Float maximumZoomLevel = MINIMUM_ZOOM_LEVEL; | ||
|
||
/** | ||
* Creates a new instance of the {@link ZoomLevelFeature}. | ||
* | ||
* @param cameraProperties Collection of characteristics for the current camera device. | ||
*/ | ||
public ZoomLevelFeature(CameraProperties cameraProperties) { | ||
super(cameraProperties); | ||
|
||
sensorArraySize = cameraProperties.getSensorInfoActiveArraySize(); | ||
|
||
if (sensorArraySize == null) { | ||
maximumZoomLevel = MINIMUM_ZOOM_LEVEL; | ||
hasSupport = false; | ||
return; | ||
} | ||
|
||
Float maxDigitalZoom = cameraProperties.getScalerAvailableMaxDigitalZoom(); | ||
maximumZoomLevel = | ||
((maxDigitalZoom == null) || (maxDigitalZoom < MINIMUM_ZOOM_LEVEL)) | ||
? MINIMUM_ZOOM_LEVEL | ||
: maxDigitalZoom; | ||
|
||
hasSupport = (Float.compare(maximumZoomLevel, MINIMUM_ZOOM_LEVEL) > 0); | ||
} | ||
|
||
@Override | ||
public String getDebugName() { | ||
return "ZoomLevelFeature"; | ||
} | ||
|
||
@Override | ||
public Float getValue() { | ||
return currentSetting; | ||
} | ||
|
||
@Override | ||
public void setValue(Float value) { | ||
currentSetting = value; | ||
} | ||
|
||
@Override | ||
public boolean checkIsSupported() { | ||
return hasSupport; | ||
} | ||
|
||
@Override | ||
public void updateBuilder(CaptureRequest.Builder requestBuilder) { | ||
if (!checkIsSupported()) { | ||
return; | ||
} | ||
|
||
final Rect computedZoom = | ||
ZoomUtils.computeZoom( | ||
currentSetting, sensorArraySize, MINIMUM_ZOOM_LEVEL, maximumZoomLevel); | ||
requestBuilder.set(CaptureRequest.SCALER_CROP_REGION, computedZoom); | ||
} | ||
|
||
/** | ||
* Gets the minimum supported zoom level. | ||
* | ||
* @return The minimum zoom level. | ||
*/ | ||
public float getMinimumZoomLevel() { | ||
return MINIMUM_ZOOM_LEVEL; | ||
} | ||
|
||
/** | ||
* Gets the maximum supported zoom level. | ||
* | ||
* @return The maximum zoom level. | ||
*/ | ||
public float getMaximumZoomLevel() { | ||
return maximumZoomLevel; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/ZoomUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camera.features.zoomlevel; | ||
|
||
import android.graphics.Rect; | ||
import androidx.annotation.NonNull; | ||
import androidx.core.math.MathUtils; | ||
|
||
/** | ||
* Utility class containing methods that assist with zoom features in the {@link | ||
* android.hardware.camera2} API. | ||
*/ | ||
final class ZoomUtils { | ||
|
||
/** | ||
* Computes an image sensor area based on the supplied zoom settings. | ||
* | ||
* <p>The returned image sensor area can be applied to the {@link android.hardware.camera2} API in | ||
* order to control zoom levels. | ||
* | ||
* @param zoom The desired zoom level. | ||
* @param sensorArraySize The current area of the image sensor. | ||
* @param minimumZoomLevel The minimum supported zoom level. | ||
* @param maximumZoomLevel The maximim supported zoom level. | ||
* @return An image sensor area based on the supplied zoom settings | ||
*/ | ||
static Rect computeZoom( | ||
float zoom, @NonNull Rect sensorArraySize, float minimumZoomLevel, float maximumZoomLevel) { | ||
final float newZoom = MathUtils.clamp(zoom, minimumZoomLevel, maximumZoomLevel); | ||
|
||
final int centerX = sensorArraySize.width() / 2; | ||
final int centerY = sensorArraySize.height() / 2; | ||
final int deltaX = (int) ((0.5f * sensorArraySize.width()) / newZoom); | ||
final int deltaY = (int) ((0.5f * sensorArraySize.height()) / newZoom); | ||
|
||
return new Rect(centerX - deltaX, centerY - deltaY, centerX + deltaX, centerY + deltaY); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.