Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Di Da committed Mar 9, 2020
1 parent 72f2dc9 commit 51a4927
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 26 deletions.
4 changes: 4 additions & 0 deletions src/RNCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const requestPermissions = async (
} else {
hasCameraPermissions = cameraPermissionResult === PermissionsAndroid.RESULTS.GRANTED;
}
} else if (Platform.OS === 'windows') {
hasCameraPermissions = await CameraManager.checkMediaCapturePermission();
}

if (captureAudio) {
Expand All @@ -69,6 +71,8 @@ const requestPermissions = async (
`Otherwise you should set the 'captureAudio' property on the component instance to 'false'.`,
);
}
} else if (Platform.OS === 'windows') {
hasRecordAudioPermissions = await CameraManager.checkMediaCapturePermission();
}
}

Expand Down
2 changes: 1 addition & 1 deletion windows/ReactNativeCameraCPP/CameraRotationHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace winrt::ReactNativeCameraCPP::implementation
return result;
}

int CameraRotationHelper::GetConvertedCameraPreviewOrientation()
int CameraRotationHelper::GetCameraPreviewClockwiseDegrees()
{
auto rotation = GetCameraPreviewOrientation();
return ConvertSimpleOrientationToClockwiseDegrees(rotation);
Expand Down
11 changes: 6 additions & 5 deletions windows/ReactNativeCameraCPP/CameraRotationHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace winrt::ReactNativeCameraCPP::implementation
winrt::Windows::Devices::Sensors::SimpleOrientation GetCameraCaptureOrientation();
winrt::Windows::Storage::FileProperties::PhotoOrientation GetConvertedCameraCaptureOrientation();
winrt::Windows::Devices::Sensors::SimpleOrientation GetCameraPreviewOrientation();
int GetConvertedCameraPreviewOrientation();
int GetCameraPreviewClockwiseDegrees();

private:

Expand All @@ -34,11 +34,12 @@ namespace winrt::ReactNativeCameraCPP::implementation
winrt::Windows::Devices::Sensors::SimpleOrientation MirrorOrientation(winrt::Windows::Devices::Sensors::SimpleOrientation orientation);
winrt::Windows::Devices::Sensors::SimpleOrientation AddOrientations(winrt::Windows::Devices::Sensors::SimpleOrientation a, winrt::Windows::Devices::Sensors::SimpleOrientation b);

winrt::Windows::Storage::FileProperties::PhotoOrientation ConvertSimpleOrientationToPhotoOrientation(
winrt::Windows::Devices::Sensors::SimpleOrientation orientation);
int ConvertSimpleOrientationToClockwiseDegrees(winrt::Windows::Devices::Sensors::SimpleOrientation orientation);
winrt::Windows::Devices::Sensors::SimpleOrientation ConvertDisplayOrientationToSimpleOrientation(Windows::Graphics::Display::DisplayOrientations orientation);
winrt::Windows::Devices::Sensors::SimpleOrientation ConvertClockwiseDegreesToSimpleOrientation(int orientation);

static winrt::Windows::Storage::FileProperties::PhotoOrientation ConvertSimpleOrientationToPhotoOrientation(
winrt::Windows::Devices::Sensors::SimpleOrientation orientation);
static int ConvertSimpleOrientationToClockwiseDegrees(winrt::Windows::Devices::Sensors::SimpleOrientation orientation);
static winrt::Windows::Devices::Sensors::SimpleOrientation ConvertClockwiseDegreesToSimpleOrientation(int orientation);

void SimpleOrientationSensor_OrientationChanged(IInspectable const& sender, IInspectable const& args);
void DisplayInformation_OrientationChanged(IInspectable const& sender, IInspectable const& args);
Expand Down
2 changes: 1 addition & 1 deletion windows/ReactNativeCameraCPP/CameraRotationHelper.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ReactNativeCameraCPP
Windows.Devices.Sensors.SimpleOrientation GetCameraCaptureOrientation();
Windows.Devices.Sensors.SimpleOrientation GetCameraPreviewOrientation();
Windows.Storage.FileProperties.PhotoOrientation GetConvertedCameraCaptureOrientation();
Int32 GetConvertedCameraPreviewOrientation();
Int32 GetCameraPreviewClockwiseDegrees();

event Windows.Foundation.EventHandler<Boolean> OrientationChanged;
}
Expand Down
6 changes: 2 additions & 4 deletions windows/ReactNativeCameraCPP/ReactCameraModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ namespace winrt::ReactNativeCameraCPP {
#pragma endregion

public:
RNCameraModule() {
}
RNCameraModule() = default;

~RNCameraModule() {
}
~RNCameraModule() = default;

};

Expand Down
28 changes: 16 additions & 12 deletions windows/ReactNativeCameraCPP/ReactCameraView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ namespace winrt::ReactNativeCameraCPP {
auto titleString = titleValue.String();
props.Title(winrt::to_hstring(titleString));
}
else
{
throw winrt::hresult_invalid_argument();
}
co_await props.SavePropertiesAsync();
}
}

// RNW has a bug where the numeric value is set as int in debug but double in release
// ToDo: remove this function once bug https://github.com/microsoft/react-native-windows/issues/4225 is fixed.
bool ReactCameraView::GetNumericValue(std::map<std::wstring, JSValue> const& options, const std::wstring key, int &value)
bool ReactCameraView::TryGetalueAsInt(std::map<std::wstring, JSValue> const& options, const std::wstring key, int &value)
{
bool found = false;
auto search = options.find(key);
Expand Down Expand Up @@ -130,7 +134,7 @@ namespace winrt::ReactNativeCameraCPP {
{
if (!m_isInitialized)
{
result.Reject(L"Media device is not initialized!");
result.Reject(L"Media device is not initialized.");
return;
}

Expand All @@ -142,9 +146,9 @@ namespace winrt::ReactNativeCameraCPP {
auto randomStream = winrt::InMemoryRandomAccessStream();
co_await mediaCapture.CapturePhotoToStreamAsync(encoding, randomStream);
int target;
if (!GetNumericValue(options, L"target", target))
if (!TryGetalueAsInt(options, L"target", target))
{
result.Reject(L"target parameter not specified!");
result.Reject(L"target parameter not specified.");
return;
}
if (target == CameraCaptureTargetMemory)
Expand Down Expand Up @@ -178,7 +182,7 @@ namespace winrt::ReactNativeCameraCPP {
}
else
{
result.Reject(L"Media device is not initialized!");
result.Reject(L"Media device is not initialized.");
}

co_await resume_background();
Expand All @@ -188,7 +192,7 @@ namespace winrt::ReactNativeCameraCPP {
{
if (!m_isInitialized)
{
result.Reject(L"Media device is not initialized!");
result.Reject(L"Media device is not initialized.");
return;
}

Expand All @@ -197,7 +201,7 @@ namespace winrt::ReactNativeCameraCPP {
if (auto mediaCapture = m_childElement.Source())
{
int quality = static_cast<int>(VideoEncodingQuality::Auto);
GetNumericValue(options, L"quality", quality);
TryGetalueAsInt(options, L"quality", quality);
auto encodingProfile = winrt::MediaEncodingProfile();
auto encoding = encodingProfile.CreateMp4(static_cast<VideoEncodingQuality>(quality));

Expand All @@ -209,12 +213,12 @@ namespace winrt::ReactNativeCameraCPP {
}

int totalSeconds = INT_MAX;
GetNumericValue(options, L"totalSeconds", totalSeconds);
TryGetalueAsInt(options, L"totalSeconds", totalSeconds);

int target;
if (!GetNumericValue(options, L"target", target))
if (!TryGetalueAsInt(options, L"target", target))
{
result.Reject(L"target parameter not specified!");
result.Reject(L"target parameter not specified.");
return;
}
if (target == CameraCaptureTargetMemory)
Expand Down Expand Up @@ -248,7 +252,7 @@ namespace winrt::ReactNativeCameraCPP {
}
else
{
result.Reject("No media capture device found!");
result.Reject("No media capture device found");
}
co_await resume_background();
}
Expand Down Expand Up @@ -482,7 +486,7 @@ namespace winrt::ReactNativeCameraCPP {
{
const GUID RotationKey = { 0xC380465D, 0x2271, 0x428C, {0x9B, 0x83, 0xEC, 0xEA, 0x3B, 0x4A, 0x85, 0xC1} };
auto props = mediaCapture.VideoDeviceController().GetMediaStreamProperties(MediaStreamType::VideoPreview);
props.Properties().Insert(RotationKey, winrt::box_value(m_rotationHelper.GetConvertedCameraPreviewOrientation()));
props.Properties().Insert(RotationKey, winrt::box_value(m_rotationHelper.GetCameraPreviewClockwiseDegrees()));
co_await mediaCapture.SetEncodingPropertiesAsync(MediaStreamType::VideoPreview, props, nullptr);
}
}
Expand Down
2 changes: 1 addition & 1 deletion windows/ReactNativeCameraCPP/ReactCameraView.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace winrt::ReactNativeCameraCPP {
void OnApplicationResuming();
winrt::Windows::Foundation::IAsyncAction OnUnloaded();

bool GetNumericValue(std::map<std::wstring, winrt::Microsoft::ReactNative::JSValue> const& options, const std::wstring key, int& value);
bool TryGetalueAsInt(std::map<std::wstring, winrt::Microsoft::ReactNative::JSValue> const& options, const std::wstring key, int& value);

winrt::Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
winrt::Windows::UI::Xaml::Controls::CaptureElement m_childElement;
Expand Down
4 changes: 2 additions & 2 deletions windows/ReactNativeCameraCPP/ReactCameraViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace winrt::ReactNativeCameraCPP::implementation {
}
else
{
result.Reject("No camera instance found!");
result.Reject("No camera instance found.");
}
}

Expand All @@ -111,7 +111,7 @@ namespace winrt::ReactNativeCameraCPP::implementation {
}
else
{
result.Reject("No camera instance found!");
result.Reject("No camera instance found.");
}
}

Expand Down

0 comments on commit 51a4927

Please sign in to comment.