Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 9fef1c7

Browse files
authored
[camera] Add camera_platform_interface package (#3253)
* Make sure only camera_platform_interface has updates * add dev dependency to async package * refactored import to package import * Fix formatting issues
1 parent b3e6d1d commit 9fef1c7

21 files changed

+1974
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial open-source release
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright 2017 The Chromium Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without modification,
4+
are permitted provided that the following conditions are met:
5+
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above
9+
copyright notice, this list of conditions and the following
10+
disclaimer in the documentation and/or other materials provided
11+
with the distribution.
12+
* Neither the name of Google Inc. nor the names of its
13+
contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# camera_platform_interface
2+
3+
A common platform interface for the [`camera`][1] plugin.
4+
5+
This interface allows platform-specific implementations of the `camera`
6+
plugin, as well as the plugin itself, to ensure they are supporting the
7+
same interface.
8+
9+
# Usage
10+
11+
To implement a new platform-specific implementation of `camera`, extend
12+
[`CameraPlatform`][2] with an implementation that performs the
13+
platform-specific behavior, and when you register your plugin, set the default
14+
`CameraPlatform` by calling
15+
`CameraPlatform.instance = MyPlatformCamera()`.
16+
17+
# Note on breaking changes
18+
19+
Strongly prefer non-breaking changes (such as adding a method to the interface)
20+
over breaking changes for this package.
21+
22+
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
23+
on why a less-clean interface is preferable to a breaking change.
24+
25+
[1]: ../camera
26+
[2]: lib/camera_platform_interface.dart
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
export 'src/events/camera_event.dart';
6+
export 'src/platform_interface/camera_platform.dart';
7+
export 'src/types/types.dart';
8+
9+
/// Expose XFile
10+
export 'package:cross_file/cross_file.dart';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Generic Event coming from the native side of Camera.
6+
///
7+
/// All [CameraEvent]s contain the `cameraId` that originated the event. This
8+
/// should never be `null`.
9+
///
10+
/// This class is used as a base class for all the events that might be
11+
/// triggered from a Camera, but it is never used directly as an event type.
12+
///
13+
/// Do NOT instantiate new events like `CameraEvent(cameraId)` directly,
14+
/// use a specific class instead:
15+
///
16+
/// Do `class NewEvent extend CameraEvent` when creating your own events.
17+
/// See below for examples: `CameraClosingEvent`, `CameraErrorEvent`...
18+
/// These events are more semantic and more pleasant to use than raw generics.
19+
/// They can be (and in fact, are) filtered by the `instanceof`-operator.
20+
abstract class CameraEvent {
21+
/// The ID of the Camera this event is associated to.
22+
final int cameraId;
23+
24+
/// Build a Camera Event, that relates a `cameraId`.
25+
///
26+
/// The `cameraId` is the ID of the camera that triggered the event.
27+
CameraEvent(this.cameraId) : assert(cameraId != null);
28+
29+
@override
30+
bool operator ==(Object other) =>
31+
identical(this, other) ||
32+
other is CameraEvent &&
33+
runtimeType == other.runtimeType &&
34+
cameraId == other.cameraId;
35+
36+
@override
37+
int get hashCode => cameraId.hashCode;
38+
}
39+
40+
/// An event fired when the camera has finished initializing.
41+
class CameraInitializedEvent extends CameraEvent {
42+
/// The width of the preview in pixels.
43+
final double previewWidth;
44+
45+
/// The height of the preview in pixels.
46+
final double previewHeight;
47+
48+
/// Build a CameraInitialized event triggered from the camera represented by
49+
/// `cameraId`.
50+
///
51+
/// The `previewWidth` represents the width of the generated preview in pixels.
52+
/// The `previewHeight` represents the height of the generated preview in pixels.
53+
CameraInitializedEvent(
54+
int cameraId,
55+
this.previewWidth,
56+
this.previewHeight,
57+
) : super(cameraId);
58+
59+
/// Converts the supplied [Map] to an instance of the [CameraInitializedEvent]
60+
/// class.
61+
CameraInitializedEvent.fromJson(Map<String, dynamic> json)
62+
: previewWidth = json['previewWidth'],
63+
previewHeight = json['previewHeight'],
64+
super(json['cameraId']);
65+
66+
/// Converts the [CameraInitializedEvent] instance into a [Map] instance that
67+
/// can be serialized to JSON.
68+
Map<String, dynamic> toJson() => {
69+
'cameraId': cameraId,
70+
'previewWidth': previewWidth,
71+
'previewHeight': previewHeight,
72+
};
73+
74+
@override
75+
bool operator ==(Object other) =>
76+
identical(this, other) ||
77+
super == other &&
78+
other is CameraInitializedEvent &&
79+
runtimeType == other.runtimeType &&
80+
previewWidth == other.previewWidth &&
81+
previewHeight == other.previewHeight;
82+
83+
@override
84+
int get hashCode =>
85+
super.hashCode ^ previewWidth.hashCode ^ previewHeight.hashCode;
86+
}
87+
88+
/// An event fired when the resolution preset of the camera has changed.
89+
class CameraResolutionChangedEvent extends CameraEvent {
90+
/// The capture width in pixels.
91+
final double captureWidth;
92+
93+
/// The capture height in pixels.
94+
final double captureHeight;
95+
96+
/// Build a CameraResolutionChanged event triggered from the camera
97+
/// represented by `cameraId`.
98+
///
99+
/// The `captureWidth` represents the width of the resulting image in pixels.
100+
/// The `captureHeight` represents the height of the resulting image in pixels.
101+
CameraResolutionChangedEvent(
102+
int cameraId,
103+
this.captureWidth,
104+
this.captureHeight,
105+
) : super(cameraId);
106+
107+
/// Converts the supplied [Map] to an instance of the
108+
/// [CameraResolutionChangedEvent] class.
109+
CameraResolutionChangedEvent.fromJson(Map<String, dynamic> json)
110+
: captureWidth = json['captureWidth'],
111+
captureHeight = json['captureHeight'],
112+
super(json['cameraId']);
113+
114+
/// Converts the [CameraResolutionChangedEvent] instance into a [Map] instance
115+
/// that can be serialized to JSON.
116+
Map<String, dynamic> toJson() => {
117+
'cameraId': cameraId,
118+
'captureWidth': captureWidth,
119+
'captureHeight': captureHeight,
120+
};
121+
122+
@override
123+
bool operator ==(Object other) =>
124+
identical(this, other) ||
125+
other is CameraResolutionChangedEvent &&
126+
super == (other) &&
127+
runtimeType == other.runtimeType &&
128+
captureWidth == other.captureWidth &&
129+
captureHeight == other.captureHeight;
130+
131+
@override
132+
int get hashCode =>
133+
super.hashCode ^ captureWidth.hashCode ^ captureHeight.hashCode;
134+
}
135+
136+
/// An event fired when the camera is going to close.
137+
class CameraClosingEvent extends CameraEvent {
138+
/// Build a CameraClosing event triggered from the camera represented by
139+
/// `cameraId`.
140+
CameraClosingEvent(int cameraId) : super(cameraId);
141+
142+
/// Converts the supplied [Map] to an instance of the [CameraClosingEvent]
143+
/// class.
144+
CameraClosingEvent.fromJson(Map<String, dynamic> json)
145+
: super(json['cameraId']);
146+
147+
/// Converts the [CameraClosingEvent] instance into a [Map] instance that can
148+
/// be serialized to JSON.
149+
Map<String, dynamic> toJson() => {
150+
'cameraId': cameraId,
151+
};
152+
153+
@override
154+
bool operator ==(Object other) =>
155+
identical(this, other) ||
156+
super == (other) &&
157+
other is CameraClosingEvent &&
158+
runtimeType == other.runtimeType;
159+
160+
@override
161+
int get hashCode => super.hashCode;
162+
}
163+
164+
/// An event fired when an error occured while operating the camera.
165+
class CameraErrorEvent extends CameraEvent {
166+
/// Description of the error.
167+
final String description;
168+
169+
/// Build a CameraError event triggered from the camera represented by
170+
/// `cameraId`.
171+
///
172+
/// The `description` represents the error occured on the camera.
173+
CameraErrorEvent(int cameraId, this.description) : super(cameraId);
174+
175+
/// Converts the supplied [Map] to an instance of the [CameraErrorEvent]
176+
/// class.
177+
CameraErrorEvent.fromJson(Map<String, dynamic> json)
178+
: description = json['description'],
179+
super(json['cameraId']);
180+
181+
/// Converts the [CameraErrorEvent] instance into a [Map] instance that can be
182+
/// serialized to JSON.
183+
Map<String, dynamic> toJson() => {
184+
'cameraId': cameraId,
185+
'description': description,
186+
};
187+
188+
@override
189+
bool operator ==(Object other) =>
190+
identical(this, other) ||
191+
super == (other) &&
192+
other is CameraErrorEvent &&
193+
runtimeType == other.runtimeType &&
194+
description == other.description;
195+
196+
@override
197+
int get hashCode => super.hashCode ^ description.hashCode;
198+
}

0 commit comments

Comments
 (0)