-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[camera] Add availableCameras() to CameraX plugin #6307
Changes from all commits
2c04e86
5a561b9
9b4bf45
bcd03ff
d66030e
cda8281
d505dbb
35d4fd0
b399d6c
a963c56
8eec39c
70c10f3
921fb0d
7b1d9b0
b0496c9
3cc271e
4afc191
fc94964
f61ec4d
6b3cdcb
867671a
f174031
ef2fe0e
d5373c6
3c24e80
d211970
365ba79
bd3ced4
f091371
cae72e4
e9faee8
eab08f8
e124f65
2aa7f1c
1e11fe1
01c271a
fbd7e58
ebe7eaf
b5b0738
783d779
c33f25d
153edff
75d320c
47d2784
97f0c54
daae8d2
23e7577
945e280
5d3ac51
4299817
3b62574
2c4b614
481758c
fc434f1
2466213
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,39 +4,80 @@ | |
|
|
||
| package io.flutter.plugins.camerax; | ||
|
|
||
| import android.content.Context; | ||
| import androidx.annotation.NonNull; | ||
| import io.flutter.embedding.engine.plugins.FlutterPlugin; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | ||
| import io.flutter.plugin.common.MethodChannel.Result; | ||
| import io.flutter.embedding.engine.plugins.activity.ActivityAware; | ||
| import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
|
|
||
| /** CameraAndroidCameraxPlugin */ | ||
| public class CameraAndroidCameraxPlugin implements FlutterPlugin, MethodCallHandler { | ||
| /// The MethodChannel that will the communication between Flutter and native Android | ||
| /// | ||
| /// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
| /// when the Flutter Engine is detached from the Activity | ||
| private MethodChannel channel; | ||
| public final class CameraAndroidCameraxPlugin implements FlutterPlugin, ActivityAware { | ||
| private InstanceManager instanceManager; | ||
| private FlutterPluginBinding pluginBinding; | ||
| private ProcessCameraProviderHostApiImpl processCameraProviderHostApi; | ||
|
|
||
| /** | ||
| * Initialize this within the {@code #configureFlutterEngine} of a Flutter activity or fragment. | ||
| * | ||
| * <p>See {@code io.flutter.plugins.camera.MainActivity} for an example. | ||
| */ | ||
| public CameraAndroidCameraxPlugin() {} | ||
|
|
||
| void setUp(BinaryMessenger binaryMessenger, Context context) { | ||
| // Set up instance manager. | ||
| instanceManager = InstanceManager.open(identifier -> {}); | ||
|
|
||
| // Set up Host APIs. | ||
| processCameraProviderHostApi = | ||
| new ProcessCameraProviderHostApiImpl(binaryMessenger, instanceManager, context); | ||
| GeneratedCameraXLibrary.ProcessCameraProviderHostApi.setup( | ||
| binaryMessenger, processCameraProviderHostApi); | ||
| GeneratedCameraXLibrary.CameraInfoHostApi.setup( | ||
| binaryMessenger, new CameraInfoHostApiImpl(instanceManager)); | ||
| GeneratedCameraXLibrary.CameraSelectorHostApi.setup( | ||
| binaryMessenger, new CameraSelectorHostApiImpl(binaryMessenger, instanceManager)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { | ||
| channel = | ||
| new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "camera_android_camerax"); | ||
| channel.setMethodCallHandler(this); | ||
| pluginBinding = flutterPluginBinding; | ||
| (new CameraAndroidCameraxPlugin()) | ||
| .setUp( | ||
| flutterPluginBinding.getBinaryMessenger(), | ||
| flutterPluginBinding.getApplicationContext()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add a So just like the WebView plugin, you want to update the context as they are made available to you. For an example, see where the webview plugin calls |
||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { | ||
| if (call.method.equals("getPlatformVersion")) { | ||
| result.success("Android " + android.os.Build.VERSION.RELEASE); | ||
| } else { | ||
| result.notImplemented(); | ||
| } | ||
| public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { | ||
| instanceManager.close(); | ||
| } | ||
|
|
||
| // Activity Lifecycle methods: | ||
|
|
||
| @Override | ||
| public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { | ||
| channel.setMethodCallHandler(null); | ||
| public void onAttachedToActivity(@NonNull ActivityPluginBinding activityPluginBinding) { | ||
| updateContext(activityPluginBinding.getActivity()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onDetachedFromActivityForConfigChanges() { | ||
| updateContext(pluginBinding.getApplicationContext()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onReattachedToActivityForConfigChanges( | ||
| @NonNull ActivityPluginBinding activityPluginBinding) { | ||
| updateContext(activityPluginBinding.getActivity()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onDetachedFromActivity() { | ||
| updateContext(pluginBinding.getApplicationContext()); | ||
| } | ||
|
|
||
| /** Updates context that ProcessCameraProvider will use to bind UseCases to. */ | ||
| private void updateContext(Context context) { | ||
| processCameraProviderHostApi.setContext(context); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // 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.camerax; | ||
|
|
||
| import androidx.camera.core.CameraInfo; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugins.camerax.GeneratedCameraXLibrary.CameraInfoFlutterApi; | ||
|
|
||
| public class CameraInfoFlutterApiImpl extends CameraInfoFlutterApi { | ||
| private final InstanceManager instanceManager; | ||
|
|
||
| public CameraInfoFlutterApiImpl( | ||
| BinaryMessenger binaryMessenger, InstanceManager instanceManager) { | ||
| super(binaryMessenger); | ||
| this.instanceManager = instanceManager; | ||
| } | ||
|
|
||
| void create(CameraInfo cameraInfo, Reply<Void> reply) { | ||
| create(instanceManager.getIdentifierForStrongReference(cameraInfo), reply); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // 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.camerax; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.camera.core.CameraInfo; | ||
| import io.flutter.plugins.camerax.GeneratedCameraXLibrary.CameraInfoHostApi; | ||
|
|
||
| public class CameraInfoHostApiImpl implements CameraInfoHostApi { | ||
| private final InstanceManager instanceManager; | ||
|
|
||
| public CameraInfoHostApiImpl(InstanceManager instanceManager) { | ||
| this.instanceManager = instanceManager; | ||
| } | ||
|
|
||
| @Override | ||
| public Long getSensorRotationDegrees(@NonNull Long instanceId) { | ||
| CameraInfo cameraInfo = | ||
| (CameraInfo) instanceManager.getInstance(instanceId); // may return null? | ||
| return Long.valueOf(cameraInfo.getSensorRotationDegrees()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // 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.camerax; | ||
|
|
||
| import androidx.camera.core.CameraSelector; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugins.camerax.GeneratedCameraXLibrary.CameraSelectorFlutterApi; | ||
|
|
||
| public class CameraSelectorFlutterApiImpl extends CameraSelectorFlutterApi { | ||
| private final InstanceManager instanceManager; | ||
|
|
||
| public CameraSelectorFlutterApiImpl( | ||
| BinaryMessenger binaryMessenger, InstanceManager instanceManager) { | ||
| super(binaryMessenger); | ||
| this.instanceManager = instanceManager; | ||
| } | ||
|
|
||
| void create(CameraSelector cameraSelector, Long lensDirection, Reply<Void> reply) { | ||
| create(instanceManager.getIdentifierForStrongReference(cameraSelector), lensDirection, reply); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // 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.camerax; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.camera.core.CameraInfo; | ||
| import androidx.camera.core.CameraSelector; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugins.camerax.GeneratedCameraXLibrary.CameraSelectorHostApi; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class CameraSelectorHostApiImpl implements CameraSelectorHostApi { | ||
| private final BinaryMessenger binaryMessenger; | ||
| private final InstanceManager instanceManager; | ||
|
|
||
| public CameraSelectorHostApiImpl( | ||
| BinaryMessenger binaryMessenger, InstanceManager instanceManager) { | ||
| this.binaryMessenger = binaryMessenger; | ||
| this.instanceManager = instanceManager; | ||
| } | ||
|
|
||
| @Override | ||
| public Long requireLensFacing(@NonNull Long lensDirection) { | ||
| CameraSelector cameraSelectorWithLensSpecified = | ||
| (new CameraSelector.Builder()).requireLensFacing(Math.toIntExact(lensDirection)).build(); | ||
|
|
||
| final CameraSelectorFlutterApiImpl cameraInfoFlutterApi = | ||
| new CameraSelectorFlutterApiImpl(binaryMessenger, instanceManager); | ||
| long cameraSelectorWithLensSpecifiedId = | ||
| instanceManager.addHostCreatedInstance(cameraSelectorWithLensSpecified); | ||
| cameraInfoFlutterApi.create(cameraSelectorWithLensSpecified, lensDirection, result -> {}); | ||
|
|
||
| return cameraSelectorWithLensSpecifiedId; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Long> filter(@NonNull Long instanceId, @NonNull List<Long> cameraInfoIds) { | ||
| CameraSelector cameraSelector = (CameraSelector) instanceManager.getInstance(instanceId); | ||
| List<CameraInfo> cameraInfosForFilter = new ArrayList<CameraInfo>(); | ||
|
|
||
| for (Number cameraInfoId1 : cameraInfoIds) { | ||
| Long cameraInfoId = cameraInfoId1.longValue(); | ||
|
|
||
| CameraInfo cameraInfo = (CameraInfo) instanceManager.getInstance(cameraInfoId); | ||
| cameraInfosForFilter.add(cameraInfo); | ||
| } | ||
|
|
||
| List<CameraInfo> filteredCameraInfos = cameraSelector.filter(cameraInfosForFilter); | ||
| final CameraInfoFlutterApiImpl cameraInfoFlutterApiImpl = | ||
| new CameraInfoFlutterApiImpl(binaryMessenger, instanceManager); | ||
| List<Long> filteredCameraInfosIds = new ArrayList<Long>(); | ||
|
|
||
| for (CameraInfo cameraInfo : filteredCameraInfos) { | ||
| long cameraInfoId = instanceManager.addHostCreatedInstance(cameraInfo); | ||
| cameraInfoFlutterApiImpl.create(cameraInfo, result -> {}); | ||
| filteredCameraInfosIds.add(cameraInfoId); | ||
| } | ||
|
|
||
| return filteredCameraInfosIds; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.