Compare commits

...

88 Commits

Author SHA1 Message Date
Manuel Beck
90ad137398 fix(ios)! remove iPadOS popover code (#941)
- On iPadOS it was possible to configure a popover for setting the position, width and arrow position of the popover.  The code used the deprecated `UIPopoverController`, which would have to be fixed. To keep the plugin also maintainable, this was removed.
- The popover could repositioned with a `CameraPopoverHandle` on a `window.onorientationchange`. This was removed also.
- Removed documentation for popover from `README.md`
2026-01-15 17:26:37 +01:00
Manuel Beck
dc682b2532 feat(ios): use PHPickerViewController for iOS 14+ (#937)
- Does not need any permissions for reading images
- The PHPickerViewController class is an alternative to UIImagePickerController. PHPickerViewController improves stability and reliability, and includes several benefits to developers and users, such as the following:
- Deferred image loading and recovery UI
- Reliable handling of large and complex assets, like RAW and panoramic images
- User-selectable assets that aren’t available for UIImagePickerController
- Configuration of the picker to display only Live Photos
- Availability of PHLivePhoto objects without library access
- Stricter validations against invalid inputs
- See documentation of PHPickerViewController: https://developer.apple.com/documentation/photosui/phpickerviewcontroller?language=objc
- Added tests for PHPickerViewController in `CameraTest.m`

* Documentation and formatting

- Document `takePicture` and `showCameraPicker` in `CDVCamera.m`
- A pragmas for UIImagePickerControllerDelegate methods and CLLocationManager methods
- Format some long methods declarations to multi-line instead single-line for better readability
- Remove unnecessry `dispatch_async(dispatch_get_main_queue() ...` in `takePicture` before calling `showCameraPicker`. This is already done in `showCameraPicker`.
- Source out code for permission denied alert dialog when accessing the camera or UIImagePickerController on iOS < 14 for picking images

* feat(ios): proper formatting of methods

- Use linux brace style: A brace have to be on a new line for method declarations
- Remove unnecessary whitespaces in method declrations

* doc: readme update

- Better document usage descriptions
- `NSPhotoLibraryUsageDescription` not needed for iOS 14+ when only picking images
- Improve formatting for xml, js
- sourceType `SAVEDPHOTOALBUM` is the same as `PHOTOLIBRARY` on Android and iOS 14+
- Use `PHOTOLIBRARY` as sourceType instead of `SAVEDPHOTOALBUM` in  photo picker example

* Android: Document `SAVEDPHOTOALBUM``

- Make clear that `SAVEDPHOTOALBUM` is the same like `PHOTOLIBRARY` and has only an effect on iOS < 14
- Format code when creating image chooser and document the request code parameter
2026-01-13 08:33:59 +01:00
Manuel Beck
599954887b chore(ci): update obsolete XCode versions, add iOS 18 and 26 (#938)
- XCode 13 and 14 is not available anymore, replaced it with XCode 15
- Added tests for iOS 18 and 26
2026-01-12 15:43:09 +01:00
Manuel Beck
80a2f18a05 chore!: bump 9.0.0-dev and cordova-ios to 7.0.0 (#939)
- Increased cordova-ios version to 7.x to make newer features availabe
- Removed wrong cordovaDependency for `5.0.4-dev`. `cordovaDependencies` matches only against npm released versions.
2025-12-08 16:32:50 +01:00
Norman Breau
8864262022 fix(android): Error propagation when no Camera application is available (#926) 2025-03-20 16:13:02 -03:00
エリス
b002b48735 chore: remove trailing whitespace (#921) 2025-01-15 16:03:14 +09:00
Norman Breau
979155ee98 chore: 8.0.1-dev 2024-10-30 11:12:59 -03:00
Norman Breau
7f33ef4add chore: version 8.0.0 2024-10-30 11:08:58 -03:00
Norman Breau
4c9dc10512 deprecation: allowEdit (#914)
* deprecation: allowEdit

* applied suggestions to verbiage
2024-10-30 09:48:20 -03:00
Norman Breau
de468b6f63 fix: Remove WRITE_EXTERNAL_PERMISSION (#915) 2024-10-29 01:15:50 -03:00
Norman Breau
48c4cdd47f refactor(android): Make WRITE_EXTERNAL_STORAGE optional (#909)
* refactor(android): Rework permission management to make WRITE_EXTERNAL_STORAGE optional

* removed unused getPermissions API

* Proper error if WRITE_EXTERNAL_STORAGE is required but missing the declaration

* removed obsolete hasPermissions API
2024-10-28 15:21:37 -03:00
Norman Breau
7d159cf3c9 docs: Revisions for v8 public API changes with the return string formats of getPicture (#913) 2024-10-28 14:01:04 -03:00
Norman Breau
c208754c08 refactor(android): remove query img usage (#907)
* refactor: remove unnecessary duplicate image checks and queryImgDb usage

* remove unused imageType parameter, because it's a private API anyway
2024-10-28 13:32:35 -03:00
Norman Breau
0d86764b90 refactor(android): replace image path usage with image uris (#906)
* refactor(android): clean up image file path usages

* removed references of image paths in log messages
2024-10-28 12:35:50 -03:00
Norman Breau
7adccc8ee9 fix(ios): Sync camera API return to match Android changes (#911) 2024-10-27 08:32:38 -03:00
Norman Breau
53795454f4 fix(browser): Make data uri be returned as actual URI strings (#912) 2024-10-27 08:32:14 -03:00
Norman Breau
2eaa9a3972 fix: return content uris when possible when selecting from gallery (#902) 2024-10-26 00:58:24 -03:00
Norman Breau
a672c31efb fix(android): Return data uris as an URI (#910) 2024-10-26 00:58:00 -03:00
Norman Breau
16325102c7 fix(android): Improper serialization of image uri in save instance state (#903) 2024-10-25 13:59:29 -03:00
Norman Breau
36bf8e7331 fix(android): improper cache path construction during image manipulation (#905) 2024-10-25 13:15:59 -03:00
Norman Breau
feb7643bc3 fix(android): Use VERSION_CODES instead of hard-coded API literals (#904) 2024-10-25 13:10:05 -03:00
Norman Breau
44480300d9 fix(android): Isolate provider access to a subdirectory (#901) 2024-10-25 13:09:03 -03:00
ravi-yk
faa4615ee0 Remove media permissions to make complaint with Android 14 requirements (#889)
Co-authored-by: Ravi Yakasiri <ravi.yakasiri@planonsoftware.com>
2024-10-24 13:38:18 -03:00
Norman Breau
0a4bfe1a74 chore: Update package to 8.0.0-dev (#899) 2024-10-21 09:38:35 -03:00
Norman Breau
49438dee6d chore: Update eslint config to 5.1.0 (#898) 2024-10-19 12:17:43 -03:00
jcesarmobile
f38aba2b59 ci: sync workflow with paramedic (#895) 2024-08-08 12:16:56 +09:00
Norman Breau
3b73186b91 chore: Added npmrc 2024-07-27 01:30:29 -03:00
Norman Breau
40a5db10c7 ci(android): Update Android CI to be compatible with cordova-android@13 (#890) 2024-07-16 14:54:17 -03:00
Erisu
9eecbaa1af chore: bump version 7.0.1-dev 2023-09-06 17:02:23 +09:00
Erisu
24839eb71f release(camera-v7.0.0): updated version and RELEASENOTES.md 2023-09-06 16:55:23 +09:00
エリス
64bd32d641 ci(gh-action): sync with paramedic configs (#851) 2023-09-06 02:27:41 +09:00
jcesarmobile
0796f784c1 chore: remove windows/osx from plugin.xml (#850) 2023-09-01 02:12:00 +02:00
jcesarmobile
20293f3d64 fix!: remove deprecated platforms (#848) 2023-09-01 00:34:30 +02:00
jcesarmobile
8cb34e1175 chore: Update SUPPORT_QUESTION.md template (#849) 2023-08-30 23:35:39 +02:00
エリス
505ccefb4c feat(android)!: Android 13 support (#844)
* feat(android)!: Android 13 support
* refactor(android): simplify getPermissions logic
* feat(android)!: bump cordova-android requirement to >=12.0.0
* feat(android): update saveAlbumPermission to include Android 9 and below use case

---------

Co-authored-by: ochakov <evgeny@ochakov.com>
2023-08-26 20:21:32 +09:00
エリス
61a6e9bb44 dep(dev)!: bump @cordova/eslint-config@5.0.0 (#846)
* dep(dev)!: bump @cordova/eslint-config@5.0.0
* chore: apply automatic lint fix
2023-08-18 00:59:25 +09:00
エリス
c2eb21d201 chore: bump plugin version 7.0.0-dev (#845) 2023-08-17 21:43:13 +09:00
Norman Breau
23642f09b5 ci(android): Drop API 22 & 31. Added API 24 & 33 (#835)
Co-authored-by: エリス <erisu@users.noreply.github.com>
2023-06-05 22:50:49 +09:00
Norman Breau
84166f6355 chore(android): Cleanup obsolete BuildConfig comments (#831) 2023-04-14 08:48:12 -03:00
seamlink-aalves
2c09ade500 fix(android): set applicationId (#827)
Co-authored-by: Alexandre Alves <aalves@seamlink.com>
2023-04-14 08:06:30 -03:00
エリス
827bb611ee ci: sync workflow with paramedic (#804) 2022-10-01 13:28:27 +09:00
jcesarmobile
d0545c879f fix(browser): use navigator.mediaDevices.getUserMedia (#810) 2022-09-30 00:59:27 +02:00
jcesarmobile
d0d46c151c docs(README): Document ANDROIDX_CORE_VERSION variable (#808) 2022-09-25 05:03:51 +02:00
エリス
a18fda7ddf dep(npm): bump package-lock v2 w/ rebuild (#800) 2022-08-10 16:49:43 +09:00
エリス
3e548770b7 ci(android): update java requirement for cordova-android@11 (#798) 2022-07-17 13:30:47 +09:00
Scott Murphy
4608f8ef80 fix(ios): preserving EXIF data (#712) 2022-03-22 16:23:56 +09:00
Marcus Abrahamsson
53223c3df2 fix(android): update queries in plugin.xml (#780)
Solves the issue with duplicate queries elements in AppManifest.xml

https://github.com/apache/cordova-plugin-camera/issues/779
2022-03-18 09:08:13 +09:00
エリス
bf12b39d18 ci(ios): update workflow w/ iOS 15 (#770) 2021-10-18 21:13:02 +09:00
エリス
ed216ce714 ci: remove old ci workflow (#766) 2021-09-25 16:02:04 +09:00
エリス
879712028a ci: add action-badge (#765) 2021-09-25 01:04:05 +09:00
エリス
204234b1b9 ci: remove travis & appveyor (#764) 2021-09-25 00:07:07 +09:00
エリス
0fba37cac3 ci: add gh-actions workflows (#762) 2021-09-16 22:17:11 +09:00
Erisu
5b8263732a Increment package version to 6.0.1-dev 2021-08-23 14:25:23 +09:00
Erisu
869f02da1a Updated version and RELEASENOTES.md for release 6.0.0 (camera-v6.0.0) 2021-08-19 17:14:44 +09:00
Dave Alden
e9db20e381 fix(android): return exception message (where it exists) (#687)
Co-authored-by: エリス <erisu@users.noreply.github.com>
Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
2021-08-11 16:56:42 +09:00
Francis
c7971d9f63 fix(android): file path correction if Uri authority is FileProvider (#585)
Co-authored-by: Tim Brust <ratchet.player@gmx.de>
Co-authored-by: Francis Monier <contactpro@francismonier.com>
Co-authored-by: Norman Breau <norman@normanbreau.com>
2021-08-11 16:34:20 +09:00
Niklas Merz
3112e5fb15 chore: add release notify action (#654)
Co-authored-by: Erisu <ellis.bryan@gmail.com>
2021-08-11 16:04:14 +09:00
Jesse MacFadyen
c56a255fe8 ci(gh-action): added workflow to run tests (#745)
Co-authored-by: Erisu <ellis.bryan@gmail.com>
2021-08-11 16:03:34 +09:00
エリス
0227cdcf14 feat(android)!: support AndroidX (#751)
* feat: migrate FileProvider to androidx
* feat: add androidx.core:core with variable override ANDROIDX_CORE_VERSION
2021-08-09 23:09:29 +09:00
Pieter Van Poyer
75bf807261 Bugfix issue 711 heic format (#731)
* Android - issue/711 - support .heic format
2021-08-09 15:36:58 +02:00
エリス
abfbbd35d5 fix(android): supports sdk-30 package visibility (#684) 2021-08-09 20:57:12 +09:00
エリス
59cf76d1da feat: bump cordova-android requirements for 10.x (#750) 2021-08-09 20:56:15 +09:00
エリス
4ee90a84f3 feat: bump plugin version for next major (#749)
Co-authored-by: Niklas Merz <nmerz@gedys-intraware.de>
2021-08-09 20:52:50 +09:00
Niklas Merz
5587bec320 Bump 5.0.4-dev 2021-08-09 10:53:00 +02:00
Erisu
d523142cf2 Updated version and RELEASENOTES.md for release 5.0.3 (camera-v5.0.3) 2021-08-04 13:16:27 +09:00
エリス
0f6435ec73 chore: rebuilt package-lock.json (#754) 2021-08-04 13:06:29 +09:00
エリス
e81e02b21f fix: incorrect version in package-lock (#748) 2021-07-28 14:31:19 +09:00
エリス
1e20a9bd07 chore: set the 5.x versions locked to cordova-android <10.0.0 (#747) 2021-07-28 14:30:17 +09:00
Niklas Merz
d356030070 chore(asf): Update GitHub repo metadata (#729) 2021-06-28 11:16:24 +02:00
PVPoyer
7bc311fba9 Incremented plugin version. (cordova-plugin-camera20210511) 2021-05-11 22:45:53 +02:00
PVPoyer
e419a74546 Updated version and RELEASENOTES.md for release 5.0.2 (cordova-plugin-camera20210511) 2021-05-11 22:12:25 +02:00
Pieter Van Poyer
9a47f5c791 Merge pull request #728 from PieterVanPoyer/tasks/v5.0.2-audit-fix
chore(npm): fix npm audit issues automatically
2021-05-11 21:51:53 +02:00
PVPoyer
66d3f03270 plugin release preparation - audit fix 2021-05-11 21:39:28 +02:00
Pieter Van Poyer
731c10f5b2 Merge branch 'apache:master' into master 2021-05-11 21:32:47 +02:00
Pieter Van Poyer
f704689200 Bugfix issue 665 (#700)
* GH-665 - store the imageFilePath when the app is paused (onSaveInstance) and restore it back.

* Update src/android/CameraLauncher.java whitespace layout

Co-authored-by: Tim Brust <github@timbrust.de>

Co-authored-by: Tim Brust <github@timbrust.de>
2021-02-02 14:43:26 -04:00
Pieter Van Poyer
2bad1fd81c Merge pull request #1 from apache/master
make even
2021-01-08 17:14:07 +01:00
エリス
b43c78b419 ci: add node-14.x to workflow (#691) 2020-11-28 17:12:32 +09:00
Erisu
db2ffedecc Increment package version to 5.0.2-dev 2020-11-09 18:15:06 +09:00
Erisu
0d13b71d33 Updated version and RELEASENOTES.md for release 5.0.1 (camera-20201104) 2020-11-04 10:24:25 +09:00
エリス
8975171d7a chore(android): add missing apache license header (#686) 2020-11-04 10:12:19 +09:00
jcesarmobile
2d1ee66a2b fix(ios): correctly append exif on iOS 14 (#685) 2020-10-28 18:47:10 +01:00
Pieter Van Poyer
ebe0517a24 Bugfix issue 341 save to photo gallery - Fixes #341, fixes #577 (#669)
* GH-341 - GH-577 Fixed the Save Photo To Album starting from camera - Android

- saveToPhotoAlbum feature fixed by taken into count content - uri. (writeUncompressedImage method) ( see: https://github.com/apache/cordova-plugin-camera/issues/611#issuecomment-700273405 )
- make saveToPhotoAlbum future proof by using the MediaStore to insert the taken image
- made a method to calculate the compressFormat based on the encodingType (JPEG or PNG)
- layout of the performCrop method is adjusted
- removed unused rotate variable inside processResultFromGallery method

* Add extra VO class to the plugin.xml

* added package declaration to new VO

* GH-341 - GH-577 https://github.com/apache/cordova-plugin-camera/pull/669#discussion_r504632953 listen to review
2020-10-16 23:48:22 -03:00
Norman Breau
43d6591d9e Revert "fix(android): Declare CAMERA permission" (#677)
This reverts commit 140e8861e3.
2020-10-14 21:51:20 -03:00
jcesarmobile
64d8c5108a chore: Fix JIRA links in RELEASENOTES.md (#672) 2020-10-07 15:48:47 +02:00
Norman Breau
11769962bd Merge pull request #670 from breautek/fix/camera-perms
fix(android): Declare CAMERA permission
2020-10-01 07:56:42 -03:00
Norman Breau
140e8861e3 fix(android): Declare CAMERA permission 2020-09-30 22:07:19 -03:00
jcesarmobile
5ae56cf8f0 chore: Update RELEASENOTES (#664) 2020-09-24 17:00:05 +02:00
Erisu
e2e04ba3d8 Increment package version to 5.0.1-dev 2020-09-18 12:06:07 +09:00
39 changed files with 5860 additions and 3817 deletions

View File

@@ -1,32 +0,0 @@
# appveyor file
# http://www.appveyor.com/docs/appveyor-yml
max_jobs: 1
shallow_clone: true
init:
- git config --global core.autocrlf true
image:
- Visual Studio 2017
environment:
matrix:
- nodejs_version: "10"
- nodejs_version: "12"
platform:
- x86
- x64
install:
- ps: Install-Product node $env:nodejs_version
- node --version
- npm install -g github:apache/cordova-paramedic
- npm install -g cordova
build: off
test_script:
- cordova-paramedic --config pr\windows-10-store --plugin . --justBuild

View File

@@ -15,6 +15,30 @@
# specific language governing permissions and limitations
# under the License.
github:
description: Apache Cordova Plugin camera
homepage: https://cordova.apache.org/
labels:
- cordova
- mobile
- javascript
- nodejs
- hacktoberfest
- java
- objective-c
- cordova-plugin
features:
wiki: false
issues: true
projects: true
enabled_merge_buttons:
squash: true
merge: false
rebase: false
notifications:
commits: commits@cordova.apache.org
issues: issues@cordova.apache.org

View File

@@ -13,7 +13,8 @@ For usage and support questions, please check out the resources below. Thanks!
You can get answers to your usage and support questions about **Apache Cordova** on:
* Slack Community Chat: https://cordova.slack.com (you can sign-up at http://slack.cordova.io/)
* GitHub Discussions: https://github.com/apache/cordova/discussions
* Slack Community Chat: https://cordova.slack.com (you can sign-up at https://s.apache.org/cordova-slack)
* StackOverflow: https://stackoverflow.com/questions/tagged/cordova using the tag `cordova`
---
@@ -22,6 +23,4 @@ If you are using a tool that uses Cordova internally, like e.g. Ionic, check the
* **Ionic Framework**
* [Ionic Community Forum](https://forum.ionicframework.com/)
* [Ionic Worldwide Slack](https://ionicworldwide.herokuapp.com/)
* **PhoneGap**
* [PhoneGap Developer Community](https://forums.adobe.com/community/phonegap)
* [Ionic Discord](https://ionic.link/discord)

161
.github/workflows/android.yml vendored Normal file
View File

@@ -0,0 +1,161 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
name: Android Testsuite
on:
push:
paths-ignore:
- '**.md'
- 'LICENSE'
- '.eslint*'
pull_request:
paths-ignore:
- '**.md'
- 'LICENSE'
- '.eslint*'
jobs:
test:
name: Android ${{ matrix.versions.android }} Test
runs-on: ubuntu-latest
continue-on-error: true
# hoist configurations to top that are expected to be updated
env:
# Storing a copy of the repo
repo: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
node-version: 20
# These are the default Java configurations used by most tests.
# To customize these options, add "java-distro" or "java-version" to the strategy matrix with its overriding value.
default_java-distro: temurin
default_java-version: 17
# These are the default Android System Image configurations used by most tests.
# To customize these options, add "system-image-arch" or "system-image-target" to the strategy matrix with its overriding value.
default_system-image-arch: x86_64
default_system-image-target: google_apis # Most system images have a google_api option. Set this as default.
# configurations for each testing strategy (test matrix)
strategy:
matrix:
versions:
- android: 7
android-api: 24
- android: 7.1
android-api: 25
- android: 8
android-api: 26
- android: 8.1
android-api: 27
system-image-arch: x86
- android: 9
android-api: 28
- android: 10
android-api: 29
- android: 11
android-api: 30
- android: 12
android-api: 31
- android: 12L
android-api: 32
- android: 13
android-api: 33
- android: 14
android-api: 34
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
- uses: actions/setup-java@v4
env:
java-version: ${{ matrix.versions.java-version == '' && env.default_java-version || matrix.versions.java-version }}
java-distro: ${{ matrix.versions.java-distro == '' && env.default_java-distro || matrix.versions.java-distro }}
with:
distribution: ${{ env.java-distro }}
java-version: ${{ env.java-version }}
- name: Enable KVM group perms
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
- name: Run Environment Information
run: |
node --version
npm --version
java -version
- name: Run npm install
run: |
export PATH="/usr/local/lib/android/sdk/platform-tools":$PATH
export JAVA_HOME=$JAVA_HOME_11_X64
npm i -g cordova@latest
npm ci
- name: Run paramedic install
if: ${{ endswith(env.repo, '/cordova-paramedic') != true }}
run: npm i -g github:apache/cordova-paramedic
- uses: reactivecircus/android-emulator-runner@v2
env:
system-image-arch: ${{ matrix.versions.system-image-arch == '' && env.default_system-image-arch || matrix.versions.system-image-arch }}
system-image-target: ${{ matrix.versions.system-image-target == '' && env.default_system-image-target || matrix.versions.system-image-target }}
with:
api-level: ${{ matrix.versions.android-api }}
target: ${{ env.system-image-target }}
arch: ${{ env.system-image-arch }}
force-avd-creation: false
disable-animations: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim
script: echo "Pregenerate the AVD before running Paramedic"
- name: Run paramedic tests
uses: reactivecircus/android-emulator-runner@v2
env:
system-image-arch: ${{ matrix.versions.system-image-arch == '' && env.default_system-image-arch || matrix.versions.system-image-arch }}
system-image-target: ${{ matrix.versions.system-image-target == '' && env.default_system-image-target || matrix.versions.system-image-target }}
test_config: 'android-${{ matrix.versions.android }}.config.json'
# Generally, this should automatically work for cordova-paramedic & plugins. If the path is unique, this can be manually changed.
test_plugin_path: ${{ endswith(env.repo, '/cordova-paramedic') && './spec/testable-plugin/' || './' }}
paramedic: ${{ endswith(env.repo, '/cordova-paramedic') && 'node main.js' || 'cordova-paramedic' }}
with:
api-level: ${{ matrix.versions.android-api }}
target: ${{ env.system-image-target }}
arch: ${{ env.system-image-arch }}
force-avd-creation: false
disable-animations: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim
script: ${{ env.paramedic }} --config ./pr/local/${{ env.test_config }} --plugin ${{ env.test_plugin_path }}

73
.github/workflows/chrome.yml vendored Normal file
View File

@@ -0,0 +1,73 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
name: Chrome Testsuite
on:
push:
paths-ignore:
- '**.md'
- 'LICENSE'
- '.eslint*'
pull_request:
paths-ignore:
- '**.md'
- 'LICENSE'
- '.eslint*'
jobs:
test:
name: Chrome Latest Test
runs-on: ubuntu-latest
# hoist configurations to top that are expected to be updated
env:
# Storing a copy of the repo
repo: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
node-version: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
- name: Run install xvfb
run: sudo apt-get install xvfb
- name: Run Environment Information
run: |
node --version
npm --version
- name: Run npm install
run: |
npm i -g cordova@latest
npm ci
- name: Run paramedic install
if: ${{ endswith(env.repo, '/cordova-paramedic') != true }}
run: npm i -g github:apache/cordova-paramedic
- name: Run paramedic tests
env:
test_config: 'browser.config.json'
# Generally, this should automatically work for cordova-paramedic & plugins. If the path is unique, this can be manually changed.
test_plugin_path: ${{ endswith(env.repo, '/cordova-paramedic') && './spec/testable-plugin/' || './' }}
paramedic: ${{ endswith(env.repo, '/cordova-paramedic') && 'node main.js' || 'cordova-paramedic' }}
run: xvfb-run --auto-servernum ${{ env.paramedic }} --config ./pr/local/${{ env.test_config }} --plugin ${{ env.test_plugin_path }}

105
.github/workflows/ios.yml vendored Normal file
View File

@@ -0,0 +1,105 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
name: iOS Testsuite
on:
push:
paths-ignore:
- '**.md'
- 'LICENSE'
- '.eslint*'
pull_request:
paths-ignore:
- '**.md'
- 'LICENSE'
- '.eslint*'
jobs:
test:
name: iOS ${{ matrix.versions.ios-version }} Test
runs-on: ${{ matrix.versions.os-version }}
continue-on-error: true
# hoist configurations to top that are expected to be updated
env:
# Storing a copy of the repo
repo: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
node-version: 20
# > Starting April 26, 2021, all iOS and iPadOS apps submitted to the App Store must be built with Xcode 12 and the iOS 14 SDK.
# Because of Apple's requirement, listed above, We will only be using the latest Xcode release for testing.
# To customize these options, add "xcode-version" to the strategy matrix with its overriding value.
default_xcode-version: latest-stable
strategy:
matrix:
versions:
- os-version: macos-14
ios-version: 15.x
xcode-version: 15.x
- os-version: macos-14
ios-version: 16.x
xcode-version: 15.x
- os-version: macos-14
ios-version: 17.x
xcode-version: 15.x
- os-version: macos-15
ios-version: 18.x
xcode-version: 16.x
- os-version: macos-26
ios-version: 26.x
xcode-version: 26.x
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
- uses: maxim-lobanov/setup-xcode@60606e260d2fc5762a71e64e74b2174e8ea3c8bd
env:
xcode-version: ${{ matrix.versions.xcode-version == '' && env.default_xcode-version || matrix.versions.xcode-version }}
with:
xcode-version: ${{ env.xcode-version }}
- name: Run Environment Information
run: |
node --version
npm --version
xcodebuild -version
- name: Run npm install
run: |
npm i -g cordova@latest ios-deploy@latest
npm ci
- name: Run paramedic install
if: ${{ endswith(env.repo, '/cordova-paramedic') != true }}
run: npm i -g github:apache/cordova-paramedic
- name: Run paramedic tests
env:
test_config: 'ios-${{ matrix.versions.ios-version }}.config.json'
# Generally, this should automatically work for cordova-paramedic & plugins. If the path is unique, this can be manually changed.
test_plugin_path: ${{ endswith(env.repo, '/cordova-paramedic') && './spec/testable-plugin/' || './' }}
paramedic: ${{ endswith(env.repo, '/cordova-paramedic') && 'node main.js' || 'cordova-paramedic' }}
run: ${{ env.paramedic }} --config ./pr/local/${{ env.test_config }} --plugin ${{ env.test_plugin_path }}

56
.github/workflows/lint.yml vendored Normal file
View File

@@ -0,0 +1,56 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
name: Lint Test
on:
push:
paths:
- '**.js'
- '.eslint*'
- '.github/workflow/lint.yml'
pull_request:
paths:
- '**.js'
- '.eslint*'
- '.github/workflow/lint.yml'
jobs:
test:
name: Lint Test
runs-on: ubuntu-latest
env:
node-version: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
- name: Run Environment Information
run: |
node --version
npm --version
- name: Run npm install
run: |
npm ci
- name: Run lint test
run: |
npm run lint

13
.github/workflows/release-notify.yml vendored Normal file
View File

@@ -0,0 +1,13 @@
name: Close issue asking for release
on:
issues:
types: [opened]
jobs:
action-test:
runs-on: ubuntu-latest
steps:
- uses: niklasmerz/release-notify@master
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -1,3 +1,2 @@
.*
appveyor.yml
tests

2
.npmrc Normal file
View File

@@ -0,0 +1,2 @@
registry=https://registry.npmjs.org

View File

@@ -1,118 +0,0 @@
# This Travis configuration file is built after a Cordova Paramedic
# specific template with minimal modifications and adaptations:
# https://github.com/apache/cordova-paramedic/blob/master/.travis.yml
sudo: false
addons:
jwt:
# SAUCE_ACCESS_KEY
secure: QivPLlqTVvOo3TJeHxuBOfxU6lho1I0IxQ3b68yntkEQQJko6kzleXHfgjf0a8aw8m38E3+fxaBWF1bGyucGwOLDWY8Ddt2P2xg44zdXH5EXHd9oIqAgngIdzLvUtH3Db2TbQEtIGOkrnNR2STovjqB7vHGLASQrgs4oL7r32/s=
env:
global:
- SAUCE_USERNAME=snay
- TRAVIS_NODE_VERSION=12
- ANDROID_API_LEVEL=29
- ANDROID_BUILD_TOOLS_VERSION=29.0.2
language: node_js
node_js: 12
# yaml anchor/alias: https://medium.com/@tommyvn/travis-yml-dry-with-anchors-8b6a3ac1b027
_ios: &_ios
os: osx
osx_image: xcode11.6
_android: &_android
language: android
os: linux
jdk: oraclejdk8
android:
components:
- tools
- build-tools-$ANDROID_BUILD_TOOLS_VERSION
- android-$ANDROID_API_LEVEL
licenses:
- "android-sdk-preview-license-.+"
- "android-sdk-license-.+"
- "google-gdk-license-.+"
matrix:
include:
# additional tests
- env: ADDITIONAL_TESTS_DIR=./tests/ios
os: osx
osx_image: xcode11.5
# local tests, without saucelabs
- env: PLATFORM=local/browser
<<: *_ios
- env: PLATFORM=local/ios-10.0
<<: *_ios
# many tests with saucelabs
- env: PLATFORM=browser-chrome
- env: PLATFORM=browser-firefox
- env: PLATFORM=browser-safari
- env: PLATFORM=browser-edge
- env: PLATFORM=ios-11.3
<<: *_ios
- env: PLATFORM=ios-12.0
<<: *_ios
- env: PLATFORM=ios-12.2
<<: *_ios
- env: PLATFORM=android-5.1
<<: *_android
- env: PLATFORM=android-6.0
<<: *_android
- env: PLATFORM=android-7.0
<<: *_android
- env: PLATFORM=android-7.1
<<: *_android
- env: PLATFORM=android-8.0
<<: *_android
- env: PLATFORM=android-8.1
<<: *_android
- env: PLATFORM=android-9.0
<<: *_android
before_install:
# manually install Node for `language: android`
- if [[ "$PLATFORM" =~ android ]]; then nvm install $TRAVIS_NODE_VERSION; fi
- node --version
- if [[ "$PLATFORM" =~ android ]]; then gradle --version; fi
- if [[ "$PLATFORM" =~ ios ]]; then npm install -g ios-deploy; fi
- npm install -g cordova
# install paramedic if not running on paramedic repo
- if ! [[ "$TRAVIS_REPO_SLUG" =~ cordova-paramedic ]]; then npm install -g github:apache/cordova-paramedic; fi
install:
- npm install
before_script:
- |
if [[ "$TRAVIS_REPO_SLUG" =~ cordova-paramedic ]]; then
# when used in the cordova-paramedic repo
TEST_COMMAND="npm run eslint"
PARAMEDIC_PLUGIN_TO_TEST="./spec/testable-plugin/"
PARAMEDIC_COMMAND="node main.js"
else
# when used in any other (plugin) repo
TEST_COMMAND="npm test"
PARAMEDIC_PLUGIN_TO_TEST=$(pwd)
PARAMEDIC_COMMAND="cordova-paramedic"
fi
- PARAMEDIC_BUILDNAME=travis-$TRAVIS_REPO_SLUG-$TRAVIS_JOB_NUMBER
script:
- $TEST_COMMAND
- |
if [[ "$ADDITIONAL_TESTS_DIR" != "" ]];
then cd $ADDITIONAL_TESTS_DIR && npm install && npm test;
else
$PARAMEDIC_COMMAND --config ./pr/$PLATFORM --plugin $PARAMEDIC_PLUGIN_TO_TEST --buildName $PARAMEDIC_BUILDNAME;
fi

441
README.md
View File

@@ -21,35 +21,39 @@ description: Take pictures with the device camera.
# under the License.
-->
|AppVeyor|Travis CI|
|:-:|:-:|
|[![Build status](https://ci.appveyor.com/api/projects/status/github/apache/cordova-plugin-camera?branch=master)](https://ci.appveyor.com/project/ApacheSoftwareFoundation/cordova-plugin-camera)|[![Build Status](https://travis-ci.org/apache/cordova-plugin-camera.svg?branch=master)](https://travis-ci.org/apache/cordova-plugin-camera)|
# cordova-plugin-camera
[![Android Testsuite](https://github.com/apache/cordova-plugin-camera/actions/workflows/android.yml/badge.svg)](https://github.com/apache/cordova-plugin-camera/actions/workflows/android.yml) [![Chrome Testsuite](https://github.com/apache/cordova-plugin-camera/actions/workflows/chrome.yml/badge.svg)](https://github.com/apache/cordova-plugin-camera/actions/workflows/chrome.yml) [![iOS Testsuite](https://github.com/apache/cordova-plugin-camera/actions/workflows/ios.yml/badge.svg)](https://github.com/apache/cordova-plugin-camera/actions/workflows/ios.yml) [![Lint Test](https://github.com/apache/cordova-plugin-camera/actions/workflows/lint.yml/badge.svg)](https://github.com/apache/cordova-plugin-camera/actions/workflows/lint.yml)
This plugin defines a global `navigator.camera` object, which provides an API for taking pictures and for choosing images from
the system's image library.
Although the object is attached to the global scoped `navigator`, it is not available until after the `deviceready` event.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(navigator.camera);
}
```js
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(navigator.camera);
}
```
## Installation
This requires cordova 5.0+
cordova plugin add cordova-plugin-camera
Older versions of cordova can still install via the __deprecated__ id
cordova plugin add org.apache.cordova.camera
It is also possible to install via repo url directly ( unstable )
cordova plugin add https://github.com/apache/cordova-plugin-camera.git
## Plugin variables
The plugin uses the `ANDROIDX_CORE_VERSION` variable to configure `androidx.core:core` dependency. This allows to avoid conflicts with other plugins that have the dependency hardcoded.
If no value is passed, it will use `1.6.+` as the default value.
The variable is configured on install time
cordova plugin add cordova-plugin-camera --variable ANDROIDX_CORE_VERSION=1.8.0
## How to Contribute
@@ -63,41 +67,37 @@ In order for your changes to be accepted, you need to sign and submit an Apache
**And don't forget to test and document your code.**
### iOS Quirks
### iOS Specifics
Since iOS 10 it's mandatory to provide an usage description in the `info.plist` if trying to access privacy-sensitive data. When the system prompts the user to allow access, this usage description string will displayed as part of the permission dialog box, but if you didn't provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide an usage description.
Since iOS 10 it's mandatory to provide a usage description in the `info.plist` when accessing privacy-sensitive data. The required keys depend on how you use the plugin and which iOS versions you support:
This plugins requires the following usage descriptions:
| Key | Description |
| ------------------------------ | ----------- |
| NSCameraUsageDescription | Required whenever the camera is used (e.g. `Camera.PictureSourceType.CAMERA`). |
| NSPhotoLibraryUsageDescription | Required only when your app runs on iOS 13 or older and using as `sourceType` `Camera.PictureSourceType.PHOTOLIBRARY`. On iOS 14+ the plugin uses PHPicker for read-only access, which does not need this key. |
| NSPhotoLibraryAddUsageDescription | Required when the plugin writes to the user's library (e.g. `saveToPhotoAlbum=true`). |
| NSLocationWhenInUseUsageDescription | Required if `CameraUsesGeolocation` is set to `true`. |
- `NSCameraUsageDescription` specifies the reason for your app to access the device's camera.
- `NSPhotoLibraryUsageDescription` specifies the reason for your app to access the user's photo library.
- `NSLocationWhenInUseUsageDescription` specifies the reason for your app to access the user's location information while your app is in use. (Set it if you have `CameraUsesGeolocation` preference set to `true`)
- `NSPhotoLibraryAddUsageDescription` specifies the reason for your app to get write-only access to the user's photo library
When the system prompts the user to allow access, this usage description string will be displayed as part of the permission dialog box. If you don't provide the required usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide a usage description.
To add these entries into the `info.plist`, you can use the `edit-config` tag in the `config.xml` like this:
```
```xml
<edit-config target="NSCameraUsageDescription" file="*-Info.plist" mode="merge">
<string>need camera access to take pictures</string>
</edit-config>
```
```
<edit-config target="NSPhotoLibraryUsageDescription" file="*-Info.plist" mode="merge">
<string>need photo library access to get pictures from there</string>
</edit-config>
```
```
<edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
<string>need location access to find things nearby</string>
</edit-config>
```
```
<edit-config target="NSPhotoLibraryAddUsageDescription" file="*-Info.plist" mode="merge">
<string>need photo library access to save pictures there</string>
</edit-config>
<edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
<string>need location access to find things nearby</string>
</edit-config>
```
---
@@ -118,12 +118,8 @@ To add these entries into the `info.plist`, you can use the `edit-config` tag in
* [.EncodingType](#module_Camera.EncodingType) : <code>enum</code>
* [.MediaType](#module_Camera.MediaType) : <code>enum</code>
* [.PictureSourceType](#module_Camera.PictureSourceType) : <code>enum</code>
* [.PopoverArrowDirection](#module_Camera.PopoverArrowDirection) : <code>enum</code>
* [.Direction](#module_Camera.Direction) : <code>enum</code>
* [CameraPopoverHandle](#module_CameraPopoverHandle)
* [CameraPopoverOptions](#module_CameraPopoverOptions)
---
<a name="module_camera"></a>
@@ -133,8 +129,31 @@ To add these entries into the `info.plist`, you can use the `edit-config` tag in
### camera.getPicture(successCallback, errorCallback, options)
Takes a photo using the camera, or retrieves a photo from the device's
image gallery. The image is passed to the success callback as a
Base64-encoded `String`, or as the URI for the image file.
image gallery. The result is provided in the first parameter of the `successCallback` as a string.
As of v8.0.0, the result is formatted as URIs. The scheme will vary depending on settings and platform.
|Platform|Destination Type|Format|
|---|---|---|
|Android|FILE_URI|An URI scheme such as `file://...` or `content://...`|
||DATA_URL|Base 64 encoded with the proper data URI header|
|iOS|FILE_URI|`file://` schemed paths|
||DATA_URL|Base 64 encoded with the proper data URI header|
|Browser|FILE_URI|Not supported|
||DATA_URL|Base 64 encoded with the proper data URI header|
v7 and earlier versions, the return format is as follows:
|Platform|Destination Type|Format|
|---|---|---|
|Android|FILE_URI|Raw file path (unprefixed)|
||DATA_URL|Base 64 encoded, without the `data:` prefix
|iOS|FILE_URI|`file://` schemed paths|
||DATA_URL|Base 64 encoded, without the `data:` prefix
|Browser|FILE_URI|Not supported|
||DATA_URL|Base 64 encoded, without the `data:` prefix|
For this reason, upgrading to v8 is strongly recommended as it greatly streamlines the return data.
The `camera.getPicture` function opens the device's default camera
application that allows users to snap pictures by default - this behavior occurs,
@@ -147,16 +166,10 @@ that allows users to select an existing image.
The return value is sent to the [`cameraSuccess`](#module_camera.onSuccess) callback function, in
one of the following formats, depending on the specified
`cameraOptions`:
`cameraOptions`. You can do whatever you want with content:
- A `String` containing the Base64-encoded photo image.
- A `String` representing the image file location on local storage (default).
You can do whatever you want with the encoded image or URI, for
example:
- Render the image in an `<img>` tag, as in the example below
- Save the data locally (`LocalStorage`, [Lawnchair](http://brianleroux.github.com/lawnchair/), etc.)
- Render the content in an `<img>` or `<video>` tag
- Copy the data to a persistent location
- Post the data to a remote server
__NOTE__: Photo resolution on newer devices is quite good. Photos
@@ -165,17 +178,41 @@ quality, even if a `quality` parameter is specified. To avoid common
memory problems, set `Camera.destinationType` to `FILE_URI` rather
than `DATA_URL`.
__NOTE__: To use `saveToPhotoAlbum` option on Android 9 (API 28) and lower, the `WRITE_EXTERNAL_STORAGE` permission must be declared.
To do this, add the following in your `config.xml`:
```xml
<config-file target="AndroidManifest.xml" parent="/*" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
</config-file>
```
Android 10 (API 29) and later devices does not require `WRITE_EXTERNAL_STORAGE` permission. If your application only supports Android 10 or later, then this step is not necessary.
#### FILE_URI Usage
When `FILE_URI` is used, the returned path is not directly usable. The file path needs to be resolved into
a DOM-usable URL using the [Cordova File Plugin](https://github.com/apache/cordova-plugin-file).
Additionally, the file URIs returned is a temporary read access grant. The OS reserves the right to revoke permission to access the resource, which typically occurs after the app has been closed. For images captured using the camera, the image is stored in a temporary location which can be cleared at any time, usually after the app exits. It's the application's decision to decide how the content should be used depending on their use cases.
For persistent access to the content, the resource should be copied to your app's storage container. An example use case is an app allowing an user to select a profile picture from their gallery or camera. The application will need
consistent access to that resource, so it's not suitable to retain the temporary access path. So the appplication should copy the resource to a persistent location.
For use cases that involve temporary use, it is valid and safe to use the temporary file path to display the content. An example of this could be an image editing application, rendering the data into a canvas.
__NOTE__: The returned schemes is an implementation detail. Do not assume that it will always be a `file://` URI.
__Supported Platforms__
- Android
- Browser
- iOS
- Windows
- OSX
More examples [here](#camera-getPicture-examples). Quirks [here](#camera-getPicture-quirks).
**Kind**: static method of <code>[camera](#module_camera)</code>
**Kind**: static method of <code>[camera](#module_camera)</code>
| Param | Type | Description |
| --- | --- | --- |
@@ -183,7 +220,7 @@ More examples [here](#camera-getPicture-examples). Quirks [here](#camera-getPict
| errorCallback | <code>[onError](#module_camera.onError)</code> | |
| options | <code>[CameraOptions](#module_camera.CameraOptions)</code> | CameraOptions |
**Example**
**Example**
```js
navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions);
```
@@ -199,8 +236,8 @@ __Supported Platforms__
- iOS
**Kind**: static method of <code>[camera](#module_camera)</code>
**Example**
**Kind**: static method of <code>[camera](#module_camera)</code>
**Example**
```js
navigator.camera.cleanup(onSuccess, onFail);
@@ -217,7 +254,7 @@ function onFail(message) {
### camera.onError : <code>function</code>
Callback function that provides an error message.
**Kind**: static typedef of <code>[camera](#module_camera)</code>
**Kind**: static typedef of <code>[camera](#module_camera)</code>
| Param | Type | Description |
| --- | --- | --- |
@@ -228,19 +265,26 @@ Callback function that provides an error message.
### camera.onSuccess : <code>function</code>
Callback function that provides the image data.
**Kind**: static typedef of <code>[camera](#module_camera)</code>
**Kind**: static typedef of <code>[camera](#module_camera)</code>
| Param | Type | Description |
| --- | --- | --- |
| imageData | <code>string</code> | Base64 encoding of the image data, _or_ the image file URI, depending on [`cameraOptions`](#module_camera.CameraOptions) in effect. |
| imageData | <code>string</code> | Data URI, _or_ the image file URI, depending on [`cameraOptions`](#module_camera.CameraOptions) in effect. |
**Example**
**Example**
```js
// Show image
//
// Show image captured with FILE_URI
function cameraCallback(imageData) {
window.resolveLocalFileSystemURL(uri, (entry) => {
let image = document.getElementById('myImage');
image.src = entry.toURL();
}, onError);
}
// Show image captured with DATA_URL
function cameraCallback(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
image.src = imageData;
}
```
<a name="module_camera.CameraOptions"></a>
@@ -249,7 +293,7 @@ function cameraCallback(imageData) {
Optional parameters to customize the camera settings.
* [Quirks](#CameraOptions-quirks)
**Kind**: static typedef of <code>[camera](#module_camera)</code>
**Kind**: static typedef of <code>[camera](#module_camera)</code>
**Properties**
| Name | Type | Default | Description |
@@ -257,14 +301,13 @@ Optional parameters to customize the camera settings.
| quality | <code>number</code> | <code>50</code> | Quality of the saved image, expressed as a range of 0-100, where 100 is typically full resolution with no loss from file compression. (Note that information about the camera's resolution is unavailable.) |
| destinationType | <code>[DestinationType](#module_Camera.DestinationType)</code> | <code>FILE_URI</code> | Choose the format of the return value. |
| sourceType | <code>[PictureSourceType](#module_Camera.PictureSourceType)</code> | <code>CAMERA</code> | Set the source of the picture. |
| allowEdit | <code>Boolean</code> | <code>false</code> | Allow simple editing of image before selection. |
| ~~allowEdit~~ | <code>Boolean</code> | <code>false</code> | **Deprecated**. Allow simple editing of image before selection. |
| encodingType | <code>[EncodingType](#module_Camera.EncodingType)</code> | <code>JPEG</code> | Choose the returned image file's encoding. |
| targetWidth | <code>number</code> | | Width in pixels to scale image. Must be used with `targetHeight`. Aspect ratio remains constant. |
| targetHeight | <code>number</code> | | Height in pixels to scale image. Must be used with `targetWidth`. Aspect ratio remains constant. |
| mediaType | <code>[MediaType](#module_Camera.MediaType)</code> | <code>PICTURE</code> | Set the type of media to select from. Only works when `PictureSourceType` is `PHOTOLIBRARY` or `SAVEDPHOTOALBUM`. |
| correctOrientation | <code>Boolean</code> | | Rotate the image to correct for the orientation of the device during capture. |
| saveToPhotoAlbum | <code>Boolean</code> | | Save the image to the photo album on the device after capture. |
| popoverOptions | <code>[CameraPopoverOptions](#module_CameraPopoverOptions)</code> | | iOS-only options that specify popover location in iPad. |
| saveToPhotoAlbum | <code>Boolean</code> | | Save the image to the photo album on the device after capture.<br />See [Android Quirks](#cameragetpicturesuccesscallback-errorcallback-options). |
| cameraDirection | <code>[Direction](#module_Camera.Direction)</code> | <code>BACK</code> | Choose the camera to use (front- or back-facing). |
---
@@ -277,18 +320,18 @@ Optional parameters to customize the camera settings.
### Camera.DestinationType : <code>enum</code>
Defines the output format of `Camera.getPicture` call.
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| DATA_URL | <code>number</code> | <code>0</code> | Return base64 encoded string. DATA_URL can be very memory intensive and cause app crashes or out of memory errors. Use FILE_URI if possible |
| DATA_URL | <code>number</code> | <code>0</code> | Return data uri. DATA_URL can be very memory intensive and cause app crashes or out of memory errors. Use FILE_URI if possible |
| FILE_URI | <code>number</code> | <code>1</code> | Return file uri (content://media/external/images/media/2 for Android) |
<a name="module_Camera.EncodingType"></a>
### Camera.EncodingType : <code>enum</code>
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Properties**
| Name | Type | Default | Description |
@@ -299,7 +342,7 @@ Defines the output format of `Camera.getPicture` call.
<a name="module_Camera.MediaType"></a>
### Camera.MediaType : <code>enum</code>
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Properties**
| Name | Type | Default | Description |
@@ -313,35 +356,19 @@ Defines the output format of `Camera.getPicture` call.
### Camera.PictureSourceType : <code>enum</code>
Defines the output format of `Camera.getPicture` call.
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| PHOTOLIBRARY | <code>number</code> | <code>0</code> | Choose image from the device's photo library (same as SAVEDPHOTOALBUM for Android) |
| PHOTOLIBRARY | <code>number</code> | <code>0</code> | Choose image from the device's photo library. |
| CAMERA | <code>number</code> | <code>1</code> | Take picture from camera |
| SAVEDPHOTOALBUM | <code>number</code> | <code>2</code> | Choose image only from the device's Camera Roll album (same as PHOTOLIBRARY for Android) |
<a name="module_Camera.PopoverArrowDirection"></a>
### Camera.PopoverArrowDirection : <code>enum</code>
Matches iOS UIPopoverArrowDirection constants to specify arrow location on popover.
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Properties**
| Name | Type | Default |
| --- | --- | --- |
| ARROW_UP | <code>number</code> | <code>1</code> |
| ARROW_DOWN | <code>number</code> | <code>2</code> |
| ARROW_LEFT | <code>number</code> | <code>4</code> |
| ARROW_RIGHT | <code>number</code> | <code>8</code> |
| ARROW_ANY | <code>number</code> | <code>15</code> |
| SAVEDPHOTOALBUM | <code>number</code> | <code>2</code> | Same as `PHOTOLIBRARY`, when running on Android or iOS 14+. On iOS older than 14, an image can only be chosen from the device's Camera Roll album with this setting. |
<a name="module_Camera.Direction"></a>
### Camera.Direction : <code>enum</code>
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Kind**: static enum property of <code>[Camera](#module_Camera)</code>
**Properties**
| Name | Type | Default | Description |
@@ -351,96 +378,52 @@ Matches iOS UIPopoverArrowDirection constants to specify arrow location on popov
---
<a name="module_CameraPopoverOptions"></a>
## CameraPopoverOptions
iOS-only parameters that specify the anchor element location and arrow
direction of the popover when selecting images from an iPad's library
or album.
Note that the size of the popover may change to adjust to the
direction of the arrow and orientation of the screen. Make sure to
account for orientation changes when specifying the anchor element
location.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [x] | <code>Number</code> | <code>0</code> | x pixel coordinate of screen element onto which to anchor the popover. |
| [y] | <code>Number</code> | <code>32</code> | y pixel coordinate of screen element onto which to anchor the popover. |
| [width] | <code>Number</code> | <code>320</code> | width, in pixels, of the screen element onto which to anchor the popover. |
| [height] | <code>Number</code> | <code>480</code> | height, in pixels, of the screen element onto which to anchor the popover. |
| [arrowDir] | <code>[PopoverArrowDirection](#module_Camera.PopoverArrowDirection)</code> | <code>ARROW_ANY</code> | Direction the arrow on the popover should point. |
| [popoverWidth] | <code>Number</code> | <code>0</code> | width of the popover (0 or not specified will use apple's default width). |
| [popoverHeight] | <code>Number</code> | <code>0</code> | height of the popover (0 or not specified will use apple's default height). |
---
<a name="module_CameraPopoverHandle"></a>
## CameraPopoverHandle
A handle to an image picker popover.
__Supported Platforms__
- iOS
**Example**
```js
navigator.camera.getPicture(onSuccess, onFail,
{
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
popoverOptions: new CameraPopoverOptions(300, 300, 100, 100, Camera.PopoverArrowDirection.ARROW_ANY, 300, 600)
});
// Reposition the popover if the orientation changes.
window.onorientationchange = function() {
var cameraPopoverHandle = new CameraPopoverHandle();
var cameraPopoverOptions = new CameraPopoverOptions(0, 0, 100, 100, Camera.PopoverArrowDirection.ARROW_ANY, 400, 500);
cameraPopoverHandle.setPosition(cameraPopoverOptions);
}
```
---
## `camera.getPicture` Errata
#### Example <a name="camera-getPicture-examples"></a>
Take a photo and retrieve the image's file location:
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI });
```javascript
// Don't forget to install cordova-plugin-file for resolveLocalFileSystemURL!
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
}
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI });
function onFail(message) {
alert('Failed because: ' + message);
}
function onSuccess(imageURI) {
window.resolveLocalFileSystemURL(uri, (entry) => {
let img = document.getElementById('image');
img.src = entry.toURL();
}, onFail);
}
function onFail(message) {
alert('Failed because: ' + message);
}
```
Take a photo and retrieve it as a Base64-encoded image:
/**
* Warning: Using DATA_URL is not recommended! The DATA_URL destination
* type is very memory intensive, even with a low quality setting. Using it
* can result in out of memory errors and application crashes. Use FILE_URI
* instead.
*/
navigator.camera.getPicture(onSuccess, onFail, { quality: 25,
destinationType: Camera.DestinationType.DATA_URL
});
```javascript
/**
* Warning: Using DATA_URL is not recommended! The DATA_URL destination
* type is very memory intensive, even with a low quality setting. Using it
* can result in out of memory errors and application crashes. Use FILE_URI
* instead.
*/
navigator.camera.getPicture(onSuccess, onFail, { quality: 25,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
function onFail(message) {
alert('Failed because: ' + message);
}
```
#### Preferences (iOS)
@@ -461,28 +444,20 @@ successful.
#### Browser Quirks
Can only return photos as Base64-encoded image.
Can only return photos as data URI image.
#### iOS Quirks
Including a JavaScript `alert()` in either of the callback functions
can cause problems. Wrap the alert within a `setTimeout()` to allow
the iOS image picker or popover to fully close before the alert
the iOS image picker to fully close before the alert
displays:
setTimeout(function() {
// do your thing here!
}, 0);
#### Windows quirks
On Windows Phone 8.1 using `SAVEDPHOTOALBUM` or `PHOTOLIBRARY` as a source type causes application to suspend until file picker returns the selected image and
then restore with start page as defined in app's `config.xml`. In case when `camera.getPicture` was called from different page, this will lead to reloading
start page from scratch and success and error callbacks will never be called.
To avoid this we suggest using SPA pattern or call `camera.getPicture` only from your app's start page.
More information about Windows Phone 8.1 picker APIs is here: [How to continue your Windows Phone app after calling a file picker](https://msdn.microsoft.com/en-us/library/windows/apps/dn720490.aspx)
```javascript
setTimeout(function() {
// do your thing here!
}, 0);
```
## `CameraOptions` Errata <a name="CameraOptions-quirks"></a>
@@ -496,11 +471,6 @@ More information about Windows Phone 8.1 picker APIs is here: [How to continue y
- Ignores the `encodingType` parameter if the image is unedited (i.e. `quality` is 100, `correctOrientation` is false, and no `targetHeight` or `targetWidth` are specified). The `CAMERA` source will always return the JPEG file given by the native camera and the `PHOTOLIBRARY` and `SAVEDPHOTOALBUM` sources will return the selected file in its existing encoding.
#### iOS Quirks
- When using `destinationType.FILE_URI`, photos are saved in the application's temporary directory. The contents of the application's temporary directory is deleted when the application ends.
[android_lifecycle]: http://cordova.apache.org/docs/en/dev/guide/platforms/android/lifecycle.html
## Sample: Take Pictures, Select Pictures from the Picture Library, and Get Thumbnails <a name="sample"></a>
@@ -569,12 +539,6 @@ function displayImage(imgUri) {
}
```
To display the image on some platforms, you might need to include the main part of the URI in the Content-Security-Policy `<meta>` element in index.html. For example, on Windows 10, you can include `ms-appdata:` in your `<meta>` element. Here is an example.
```html
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: ms-appdata: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
```
## Take a Picture and Return Thumbnails (Resize the Picture) <a name="getThumbnails"></a>
To get smaller images, you can return a resized image by passing both `targetHeight` and `targetWidth` values with your CameraOptions object. In this example, you resize the returned image to fit in a 100px by 100px box (the aspect ratio is maintained, so 100px is either the height or width, whichever is greater in the source).
@@ -604,23 +568,25 @@ function openCamera(selection) {
## Select a File from the Picture Library <a name="selectFile"></a>
When selecting a file using the file picker, you also need to set the CameraOptions object. In this example, set the `sourceType` to `Camera.PictureSourceType.SAVEDPHOTOALBUM`. To open the file picker, call `getPicture` just as you did in the previous example, passing in the success and error callbacks along with CameraOptions object.
When selecting a file using the file picker, you also need to set the CameraOptions object. In this example, set the `sourceType` to `Camera.PictureSourceType.PHOTOLIBRARY`. To open the file picker, call `getPicture` just as you did in the previous example, passing in the success and error callbacks along with CameraOptions object.
```js
function openFilePicker(selection) {
var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
var srcType = Camera.PictureSourceType.PHOTOLIBRARY;
var options = setOptions(srcType);
var func = createNewFileEntry;
navigator.camera.getPicture(function cameraSuccess(imageUri) {
// Do something
}, function cameraError(error) {
console.debug("Unable to obtain picture: " + error, "app");
}, options);
navigator.camera.getPicture(
// success callback
(imageUri) => {
// Do something
},
// error callback
(error) => {
console.debug("Unable to obtain picture: " + error, "app");
},
options);
}
```
@@ -631,7 +597,7 @@ Resizing a file selected with the file picker works just like resizing using the
```js
function openFilePicker(selection) {
var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
var srcType = Camera.PictureSourceType.PHOTOLIBRARY;
var options = setOptions(srcType);
var func = createNewFileEntry;
@@ -642,22 +608,24 @@ function openFilePicker(selection) {
options.targetWidth = 100;
}
navigator.camera.getPicture(function cameraSuccess(imageUri) {
// Do something with image
}, function cameraError(error) {
console.debug("Unable to obtain picture: " + error, "app");
}, options);
navigator.camera.getPicture(
// success callback
(imageUri) {
// Do something with image
},
// error callback
(error) => {
console.debug("Unable to obtain picture: " + error, "app");
},
options);
}
```
## Take a picture and get a FileEntry Object <a name="convert"></a>
If you want to do something like copy the image to another location, or upload it somewhere using the FileTransfer plugin, you need to get a FileEntry object for the returned picture. To do that, call `window.resolveLocalFileSystemURL` on the file URI returned by the Camera app. If you need to use a FileEntry object, set the `destinationType` to `Camera.DestinationType.FILE_URI` in your CameraOptions object (this is also the default value).
If you want to do something like copy the image to another location, or upload it somewhere, an `FileEntry` is needed for the returned picture. To do this, call `window.resolveLocalFileSystemURL` on the file URI returned by the Camera app. If you need to use a FileEntry object, set the `destinationType` to `Camera.DestinationType.FILE_URI` in your CameraOptions object (this is also the default value).
>*Note* You need the [File plugin](https://www.npmjs.com/package/cordova-plugin-file) to call `window.resolveLocalFileSystemURL`.
__NOTE:__ You need the [File plugin](https://www.npmjs.com/package/cordova-plugin-file) to call `window.resolveLocalFileSystemURL`.
Here is the call to `window.resolveLocalFileSystemURL`. The image URI is passed to this function from the success callback of `getPicture`. The success handler of `resolveLocalFileSystemURL` receives the FileEntry object.
@@ -665,39 +633,26 @@ Here is the call to `window.resolveLocalFileSystemURL`. The image URI is passed
function getFileEntry(imgUri) {
window.resolveLocalFileSystemURL(imgUri, function success(fileEntry) {
// Do something with the FileEntry object, like write to it, upload it, etc.
// writeFile(fileEntry, imgUri);
console.log("got file: " + fileEntry.fullPath);
// displayFileData(fileEntry.nativeURL, "Native URL");
// Example 1: Copy to app data directory
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dataDirectoryEntry) {
fileEntry.copyTo(dataDirectoryEntry, "profilePic", onSuccess, onError);
}, onError);
}, function () {
// If don't get the FileEntry (which may happen when testing
// on some emulators), copy to a new FileEntry.
createNewFileEntry(imgUri);
});
}
```
In the example shown in the preceding code, you call the app's `createNewFileEntry` function if you don't get a valid FileEntry object. The image URI returned from the Camera app should result in a valid FileEntry, but platform behavior on some emulators may be different for files returned from the file picker.
>*Note* To see an example of writing to a FileEntry, see the [File plugin README](https://www.npmjs.com/package/cordova-plugin-file).
The code shown here creates a file in your app's cache (in sandboxed storage) named `tempFile.jpeg`. With the new FileEntry object, you can copy the image to the file or do something else like upload it.
```js
function createNewFileEntry(imgUri) {
window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, function success(dirEntry) {
// JPEG file
dirEntry.getFile("tempFile.jpeg", { create: true, exclusive: false }, function (fileEntry) {
// Do something with it, like write to it, upload it, etc.
// writeFile(fileEntry, imgUri);
console.log("got file: " + fileEntry.fullPath);
// displayFileData(fileEntry.fullPath, "File copied to");
}, onErrorCreateFile);
}, onErrorResolveUrl);
// Example 2: Upload it!
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://myserver.com/upload');
xhr.onload = function () {
// All done!
};
xhr.send(this.result);
};
reader.readAsArrayBuffer(file);
}, onError);
}, onError);
}
```

View File

@@ -20,6 +20,116 @@
-->
# Release Notes
### 8.0.0 (Oct 30, 2024)
**Breaking Changes:**
* [GH-889](https://github.com/apache/cordova-plugin-camera/pull/889) fix(android): Remove media permissions to make complaint with **Android** 14 requirements (#889)
* [GH-902](https://github.com/apache/cordova-plugin-camera/pull/902) fix(android): return content uris when possible when selecting from gallery (#902)
* [GH-909](https://github.com/apache/cordova-plugin-camera/pull/909) refactor(android): Make WRITE_EXTERNAL_STORAGE optional (#909)
* [GH-910](https://github.com/apache/cordova-plugin-camera/pull/910) fix(android): Return data uris as an URI (#910)
* [GH-911](https://github.com/apache/cordova-plugin-camera/pull/911) fix(ios): Sync camera API return to match **Android** changes (#911)
* [GH-912](https://github.com/apache/cordova-plugin-camera/pull/912) fix(browser): Make data uri be returned as actual URI strings (#912)
**Fixes**:
* [GH-901](https://github.com/apache/cordova-plugin-camera/pull/901) fix(android): Isolate provider access to a subdirectory (#901)
* [GH-915](https://github.com/apache/cordova-plugin-camera/pull/903) fix(android): Improper serialization of image uri in save instance state (#903)
* [GH-904](https://github.com/apache/cordova-plugin-camera/pull/904) fix(android): Use VERSION_CODES instead of hard-coded API literals (#904)
* [GH-915](https://github.com/apache/cordova-plugin-camera/pull/905) fix(android): improper cache path construction during image manipulation (#905)
* [GH-906](https://github.com/apache/cordova-plugin-camera/pull/906) refactor(android): replace image path usage with image uris (#906)
* [GH-915](https://github.com/apache/cordova-plugin-camera/pull/907) refactor(android): remove query img usage (#907)
* [GH-915](https://github.com/apache/cordova-plugin-camera/pull/915) fix!: Remove WRITE_EXTERNAL_PERMISSION (#915)
**CI**:
* [GH-890](https://github.com/apache/cordova-plugin-camera/pull/890) ci(android): Update **Android** CI to be compatible with `cordova-android`@13 (#890)
* [GH-895](https://github.com/apache/cordova-plugin-camera/pull/895) ci: sync workflow with paramedic (#895)
**Documentation**:
* [GH-913](https://github.com/apache/cordova-plugin-camera/pull/913) docs: Revisions for v8 public API changes with the return string formats of getPicture (#913)
**Other**:
* [GH-898](https://github.com/apache/cordova-plugin-camera/pull/898) chore: Update eslint config to 5.1.0 (#898)
* [GH-914](https://github.com/apache/cordova-plugin-camera/pull/914) deprecation: allowEdit (#914)
### 7.0.0 (Sep 06, 2023)
**Breaking Changes:**
* [GH-848](https://github.com/apache/cordova-plugin-camera/pull/848) fix!: remove deprecated platforms
* [GH-844](https://github.com/apache/cordova-plugin-camera/pull/844) feat(android)!: Android 13 support
**Fixes:**
* [GH-827](https://github.com/apache/cordova-plugin-camera/pull/827) fix(android): set `applicationId`
* [GH-810](https://github.com/apache/cordova-plugin-camera/pull/810) fix(browser): use `navigator.mediaDevices.getUserMedia`
* [GH-712](https://github.com/apache/cordova-plugin-camera/pull/712) fix(ios): preserving `EXIF` data
* [GH-780](https://github.com/apache/cordova-plugin-camera/pull/780) fix(android): update queries in `plugin.xml`
**Chores, Dependencies, Docs:**
* [GH-850](https://github.com/apache/cordova-plugin-camera/pull/850) chore: remove windows/osx from `plugin.xml`
* [GH-849](https://github.com/apache/cordova-plugin-camera/pull/849) chore: Update `SUPPORT_QUESTION.md` template
* [GH-831](https://github.com/apache/cordova-plugin-camera/pull/831) chore(android): Cleanup obsolete `BuildConfig` comments
* [GH-846](https://github.com/apache/cordova-plugin-camera/pull/846) dep(dev)!: bump `@cordova/eslint-config@5.0`
* [GH-800](https://github.com/apache/cordova-plugin-camera/pull/800) dep(npm): bump package-lock v2 w/ rebuild
* [GH-808](https://github.com/apache/cordova-plugin-camera/pull/808) docs(README): Document `ANDROIDX_CORE_VERSION` variable
**CI:**
* [GH-851](https://github.com/apache/cordova-plugin-camera/pull/851) ci(gh-action): sync with `paramedic` configs
* [GH-835](https://github.com/apache/cordova-plugin-camera/pull/835) ci(android): Drop API 22 & 31. Added API 24 & 33
* [GH-804](https://github.com/apache/cordova-plugin-camera/pull/804) ci: sync workflow with `paramedic`
* [GH-798](https://github.com/apache/cordova-plugin-camera/pull/798) ci(android): update java requirement for `cordova-android@11`
* [GH-770](https://github.com/apache/cordova-plugin-camera/pull/770) ci(ios): update workflow w/ iOS 15
* [GH-766](https://github.com/apache/cordova-plugin-camera/pull/766) ci: remove old ci workflow
* [GH-765](https://github.com/apache/cordova-plugin-camera/pull/765) ci: add action-badge
* [GH-764](https://github.com/apache/cordova-plugin-camera/pull/764) ci: remove `travis` & `appveyor`
* [GH-762](https://github.com/apache/cordova-plugin-camera/pull/762) ci: add `gh-actions` workflows
### 6.0.0 (Aug 19, 2021)
**Feature:**
* [GH-751](https://github.com/apache/cordova-plugin-camera/pull/751) feat(android)!: support **AndroidX**
* [GH-750](https://github.com/apache/cordova-plugin-camera/pull/750) feat(android): bump `cordova-android` requirements for `10.x`
* [GH-731](https://github.com/apache/cordova-plugin-camera/pull/731) feat(android): encode `heic` format to `EncodingType` for webview display [#711](https://github.com/apache/cordova-plugin-camera/issues/711)
* [GH-684](https://github.com/apache/cordova-plugin-camera/pull/684) feat(android): `sdk-30` package visibility support
**Fix:**
* [GH-687](https://github.com/apache/cordova-plugin-camera/pull/687) fix(android): return exception message (where it exists)
* [GH-585](https://github.com/apache/cordova-plugin-camera/pull/585) fix(android): file path correction if `Uri` authority is `FileProvider`
**Chore & CI:**
* [GH-749](https://github.com/apache/cordova-plugin-camera/pull/749) chore: bump plugin version for next major
* [GH-654](https://github.com/apache/cordova-plugin-camera/pull/654) chore: add release notify action
* [GH-745](https://github.com/apache/cordova-plugin-camera/pull/745) ci(gh-action): added workflow to run tests
### 5.0.3 (Aug 04, 2021)
* [GH-754](https://github.com/apache/cordova-plugin-camera/pull/754) chore: rebuilt `package-lock.json`
* [GH-748](https://github.com/apache/cordova-plugin-camera/pull/748) fix: incorrect version in `package-lock`
* [GH-747](https://github.com/apache/cordova-plugin-camera/pull/747) chore: set the 5.x versions locked to `cordova-android` `<10.0.0`
* [GH-729](https://github.com/apache/cordova-plugin-camera/pull/729) chore(asf): Update GitHub repo metadata
### 5.0.2 (May 11, 2021)
* [GH-728](https://github.com/apache/cordova-plugin-camera/pull/728) plugin release preparation - audit fix
* [GH-700](https://github.com/apache/cordova-plugin-camera/pull/700) Bugfix [issue 665](https://github.com/apache/cordova-plugin-camera/issues/665) - app crashes after taking a picture due to a bug in the camera plugin when app is resumed
* [GH-691](https://github.com/apache/cordova-plugin-camera/pull/691) ci: add node-14.x to workflow (#691)
### 5.0.1 (Nov 04, 2020)
* [GH-686](https://github.com/apache/cordova-plugin-camera/pull/686) chore(android): add missing apache license header
* [GH-685](https://github.com/apache/cordova-plugin-camera/pull/685) fix(ios): correctly append exif on **iOS** 14
* [GH-669](https://github.com/apache/cordova-plugin-camera/pull/669) fix(android): save to photo gallery - fixes issues [#341](https://github.com/apache/cordova-plugin-camera/pull/341) & [#577](https://github.com/apache/cordova-plugin-camera/pull/577)
* [GH-672](https://github.com/apache/cordova-plugin-camera/pull/672) chore: Fix JIRA links in RELEASENOTES.md
* [GH-664](https://github.com/apache/cordova-plugin-camera/pull/664) chore: Update RELEASENOTES
### 5.0.0 (Sep 14, 2020)
* [GH-648](https://github.com/apache/cordova-plugin-camera/pull/648) ci(travis): update osx xcode image
@@ -35,19 +145,17 @@
* [GH-626](https://github.com/apache/cordova-plugin-camera/pull/626) ci: fix additional tests
* [GH-627](https://github.com/apache/cordova-plugin-camera/pull/627) breaking: bump version 5.0.0-dev
* [GH-612](https://github.com/apache/cordova-plugin-camera/pull/612) fix(ios): `tempFilePath` called twice if using `CameraUsesGeolocation`
### 4.2.0 (May 07, 2020)
* Cache images in device storage, devices have enough space now.
* docs(readme): app renamed to Google Photos
* [GH-588](https://github.com/apache/cordova-plugin-camera/pull/588) Cache images in device storage, devices have enough space now.
* [GH-508](https://github.com/apache/cordova-plugin-camera/pull/508) docs(readme): app renamed to Google Photos
* chore(asf): update git notification settings
* fix(ios): return copy of video when picking from gallery on **iOS** 13 (#580)
* [GH-580](https://github.com/apache/cordova-plugin-camera/pull/580) fix(ios): return copy of video when picking from gallery on **iOS** 13
* Update CONTRIBUTING.md
* Fix UI API called on a background thread (#550, #530, #447) (#551)
* ci: updates Node.js versions (#576)
* chore(npm): adds ignore list (#575)
* docs(README): remove confusing comment (#513)
* docs(README): remove orphan **Windows** phone 7 note (#512)
* ImagePicker returning same image (#306)
* [GH-551](https://github.com/apache/cordova-plugin-camera/pull/551) Fix UI API called on a background thread
* [GH-576](https://github.com/apache/cordova-plugin-camera/pull/576) ci: updates Node.js versions
* [GH-575](https://github.com/apache/cordova-plugin-camera/pull/575) chore(npm): adds ignore list
* [GH-513](https://github.com/apache/cordova-plugin-camera/pull/513) docs(README): remove confusing comment
* [GH-512](https://github.com/apache/cordova-plugin-camera/pull/512) docs(README): remove orphan **Windows** phone 7 note
* [GH-306](https://github.com/apache/cordova-plugin-camera/pull/306) ImagePicker returning same image
### 4.1.0 (Jun 27, 2019)
@@ -64,23 +172,23 @@
- fix(ios): fixes UIImagePickerController cancel handling for iOS11+ ([#377](https://github.com/apache/cordova-plugin-camera/issues/377)) ([`24c8b6c`](https://github.com/apache/cordova-plugin-camera/commit/24c8b6c))
- docs: Remove deprecated platforms from docs ([#394](https://github.com/apache/cordova-plugin-camera/issues/394)) ([`7ddb3df`](https://github.com/apache/cordova-plugin-camera/commit/7ddb3df))
- fix(android): return DATA_URL for ALLMEDIA if it's an image ([#382](https://github.com/apache/cordova-plugin-camera/issues/382)) ([`60e7795`](https://github.com/apache/cordova-plugin-camera/commit/60e7795))
- refactor(ios): [CB-13813](https://issues.apache.org/jira/browse/13813): Remove old iOS code ([#381](https://github.com/apache/cordova-plugin-camera/issues/381)) ([`ce77aab`](https://github.com/apache/cordova-plugin-camera/commit/ce77aab))
- feat(ios): [CB-13865](https://issues.apache.org/jira/browse/13865): (Ipad) Making popover Window Size configurable using popoverOptions - imagePicker ([#314](https://github.com/apache/cordova-plugin-camera/issues/314)) ([`cd72047`](https://github.com/apache/cordova-plugin-camera/commit/cd72047))
- chore(types): [CB-13837](https://issues.apache.org/jira/browse/13837): fix TypeScript Definition for CameraPopoverOptions ([#379](https://github.com/apache/cordova-plugin-camera/issues/379)) ([`86b0bf2`](https://github.com/apache/cordova-plugin-camera/commit/86b0bf2))
- refactor(ios): [CB-13813](https://issues.apache.org/jira/browse/CB-13813): Remove old iOS code ([#381](https://github.com/apache/cordova-plugin-camera/issues/381)) ([`ce77aab`](https://github.com/apache/cordova-plugin-camera/commit/ce77aab))
- feat(ios): [CB-13865](https://issues.apache.org/jira/browse/CB-13865): (Ipad) Making popover Window Size configurable using popoverOptions - imagePicker ([#314](https://github.com/apache/cordova-plugin-camera/issues/314)) ([`cd72047`](https://github.com/apache/cordova-plugin-camera/commit/cd72047))
- chore(types): [CB-13837](https://issues.apache.org/jira/browse/CB-13837): fix TypeScript Definition for CameraPopoverOptions ([#379](https://github.com/apache/cordova-plugin-camera/issues/379)) ([`86b0bf2`](https://github.com/apache/cordova-plugin-camera/commit/86b0bf2))
- docs(android): clarify android quirk of cameraDirection ([`a5a3d88`](https://github.com/apache/cordova-plugin-camera/commit/a5a3d88), [`bfbe4a1`](https://github.com/apache/cordova-plugin-camera/commit/bfbe4a1))
- chore(release): Bump minor version ([#370](https://github.com/apache/cordova-plugin-camera/issues/370)) ([`eed4433`](https://github.com/apache/cordova-plugin-camera/commit/eed4433))
- build: Remove automatic README generation ([#365](https://github.com/apache/cordova-plugin-camera/issues/365)) ([`07e8574`](https://github.com/apache/cordova-plugin-camera/commit/07e8574))
- docs: remove JIRA link ([`bcb26fb`](https://github.com/apache/cordova-plugin-camera/commit/bcb26fb))
- ci(travis): also accept terms for android sdk `android-27` ([`a346212`](https://github.com/apache/cordova-plugin-camera/commit/a346212))
- docs: remove outdated docs translations that haven't been touched for 3 years ([`403682b`](https://github.com/apache/cordova-plugin-camera/commit/403682b))
- fix(android): [CB-14097](https://issues.apache.org/jira/browse/14097): Fix crash when selecting some files with getPicture ([#322](https://github.com/apache/cordova-plugin-camera/issues/322)) ([`5c23b65`](https://github.com/apache/cordova-plugin-camera/commit/5c23b65))
- fix(browser): [CB-13384](https://issues.apache.org/jira/browse/13384): Added deprecation of video.src compatibility ([#288](https://github.com/apache/cordova-plugin-camera/issues/288)) ([`5163d38`](https://github.com/apache/cordova-plugin-camera/commit/5163d38))
- fix(android): [CB-14097](https://issues.apache.org/jira/browse/CB-14097): Fix crash when selecting some files with getPicture ([#322](https://github.com/apache/cordova-plugin-camera/issues/322)) ([`5c23b65`](https://github.com/apache/cordova-plugin-camera/commit/5c23b65))
- fix(browser): [CB-13384](https://issues.apache.org/jira/browse/CB-13384): Added deprecation of video.src compatibility ([#288](https://github.com/apache/cordova-plugin-camera/issues/288)) ([`5163d38`](https://github.com/apache/cordova-plugin-camera/commit/5163d38))
- fix(browser): Remove audio flag from getUserMedia ([#284](https://github.com/apache/cordova-plugin-camera/issues/284)) ([`36343a8`](https://github.com/apache/cordova-plugin-camera/commit/36343a8))
- docs: replace warning emoji with warning unicode ([#317](https://github.com/apache/cordova-plugin-camera/issues/317)) ([`ead7d5e`](https://github.com/apache/cordova-plugin-camera/commit/ead7d5e))
- feat(android): Update engines to use variables ([#323](https://github.com/apache/cordova-plugin-camera/issues/323)) ([`6899c5e`](https://github.com/apache/cordova-plugin-camera/commit/6899c5e))
- feat(android): [CB-14017](https://issues.apache.org/jira/browse/14017): Make com.android.support:support-v4 version configurable ([#318](https://github.com/apache/cordova-plugin-camera/issues/318)) ([`e334656`](https://github.com/apache/cordova-plugin-camera/commit/e334656))
- refactor(android): [CB-14047](https://issues.apache.org/jira/browse/14047): CameraLauncher: Replacing Repeated String literals with final variables ([#319](https://github.com/apache/cordova-plugin-camera/issues/319)) ([`5ec121b`](https://github.com/apache/cordova-plugin-camera/commit/5ec121b))
- fix(windows): [CB-11714](https://issues.apache.org/jira/browse/11714): added extra check for content-type in savePhoto() without options.targetWidth/Height ([#242](https://github.com/apache/cordova-plugin-camera/issues/242)) ([`a201722`](https://github.com/apache/cordova-plugin-camera/commit/a201722), [`dc73954`](https://github.com/apache/cordova-plugin-camera/commit/dc73954), [`dca4b9c`](https://github.com/apache/cordova-plugin-camera/commit/dca4b9c), [`c1b9772`](https://github.com/apache/cordova-plugin-camera/commit/c1b9772), [`eb57b02`](https://github.com/apache/cordova-plugin-camera/commit/eb57b02))
- feat(android): [CB-14017](https://issues.apache.org/jira/browse/CB-14017): Make com.android.support:support-v4 version configurable ([#318](https://github.com/apache/cordova-plugin-camera/issues/318)) ([`e334656`](https://github.com/apache/cordova-plugin-camera/commit/e334656))
- refactor(android): [CB-14047](https://issues.apache.org/jira/browse/CB-14047): CameraLauncher: Replacing Repeated String literals with final variables ([#319](https://github.com/apache/cordova-plugin-camera/issues/319)) ([`5ec121b`](https://github.com/apache/cordova-plugin-camera/commit/5ec121b))
- fix(windows): [CB-11714](https://issues.apache.org/jira/browse/CB-11714): added extra check for content-type in savePhoto() without options.targetWidth/Height ([#242](https://github.com/apache/cordova-plugin-camera/issues/242)) ([`a201722`](https://github.com/apache/cordova-plugin-camera/commit/a201722), [`dc73954`](https://github.com/apache/cordova-plugin-camera/commit/dc73954), [`dca4b9c`](https://github.com/apache/cordova-plugin-camera/commit/dca4b9c), [`c1b9772`](https://github.com/apache/cordova-plugin-camera/commit/c1b9772), [`eb57b02`](https://github.com/apache/cordova-plugin-camera/commit/eb57b02))
### 4.0.3 (Apr 12, 2018)

4612
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-camera",
"version": "5.0.0",
"version": "9.0.0-dev",
"description": "Cordova Camera Plugin",
"types": "./types/index.d.ts",
"cordova": {
@@ -8,9 +8,7 @@
"platforms": [
"android",
"ios",
"browser",
"windows",
"osx"
"browser"
]
},
"repository": "github:apache/cordova-plugin-camera",
@@ -21,9 +19,7 @@
"ecosystem:cordova",
"cordova-android",
"cordova-ios",
"cordova-browser",
"cordova-windows",
"cordova-osx"
"cordova-browser"
],
"scripts": {
"test": "npm run lint",
@@ -46,11 +42,31 @@
"cordova": ">=9.0.0"
},
"6.0.0": {
"cordova-android": ">=10.0.0",
"cordova-ios": ">=5.1.0",
"cordova": ">=9.0.0"
},
"7.0.0": {
"cordova-android": ">=12.0.0",
"cordova-ios": ">=5.1.0",
"cordova": ">=9.0.0"
},
"8.0.0": {
"cordova-android": ">=12.0.0",
"cordova-ios": ">=5.1.0",
"cordova": ">=9.0.0"
},
"9.0.0": {
"cordova-android": ">=12.0.0",
"cordova-ios": ">=7.0.0",
"cordova": ">=9.0.0"
},
"10.0.0": {
"cordova": ">100"
}
}
},
"devDependencies": {
"@cordova/eslint-config": "^3.0.0"
"@cordova/eslint-config": "^5.1.0"
}
}

View File

@@ -21,7 +21,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-camera"
version="5.0.0">
version="9.0.0-dev">
<name>Camera</name>
<description>Cordova Camera Plugin</description>
<license>Apache 2.0</license>
@@ -31,18 +31,14 @@
<engines>
<engine name="cordova" version=">=9.0.0"/>
<engine name="cordova-android" version=">=9.0.0" />
<engine name="cordova-ios" version=">=5.1.0" />
<engine name="cordova-android" version=">=12.0.0" />
<engine name="cordova-ios" version=">=7.0.0" />
</engines>
<js-module src="www/CameraConstants.js" name="Camera">
<clobbers target="Camera" />
</js-module>
<js-module src="www/CameraPopoverOptions.js" name="CameraPopoverOptions">
<clobbers target="CameraPopoverOptions" />
</js-module>
<js-module src="www/Camera.js" name="camera">
<clobbers target="navigator.camera" />
</js-module>
@@ -54,9 +50,6 @@
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</config-file>
<config-file target="AndroidManifest.xml" parent="application">
<provider
android:name="org.apache.cordova.camera.FileProvider"
@@ -69,19 +62,31 @@
</provider>
</config-file>
<config-file target="AndroidManifest.xml" parent="queries">
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
<intent>
<action android:name="android.intent.action.GET_CONTENT" />
</intent>
<intent>
<action android:name="android.intent.action.PICK" />
</intent>
<intent>
<action android:name="com.android.camera.action.CROP" />
<data android:scheme="content" android:mimeType="image/*"/>
</intent>
</config-file>
<source-file src="src/android/CameraLauncher.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/FileHelper.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/ExifHelper.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/FileProvider.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/GalleryPathVO.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/xml/camera_provider_paths.xml" target-dir="res/xml" />
<js-module src="www/CameraPopoverHandle.js" name="CameraPopoverHandle">
<clobbers target="CameraPopoverHandle" />
</js-module>
<preference name="ANDROID_SUPPORT_V4_VERSION" default="27.+"/>
<framework src="com.android.support:support-v4:$ANDROID_SUPPORT_V4_VERSION"/>
<preference name="ANDROIDX_CORE_VERSION" default="1.6.+"/>
<framework src="androidx.core:core:$ANDROIDX_CORE_VERSION" />
</platform>
<!-- ios -->
@@ -93,10 +98,6 @@
<preference name="CameraUsesGeolocation" value="false" />
</config-file>
<js-module src="www/ios/CameraPopoverHandle.js" name="CameraPopoverHandle">
<clobbers target="CameraPopoverHandle" />
</js-module>
<header-file src="src/ios/UIImage+CropScaleOrientation.h" />
<source-file src="src/ios/UIImage+CropScaleOrientation.m" />
<header-file src="src/ios/CDVCamera.h" />
@@ -127,36 +128,4 @@
</js-module>
</platform>
<!-- windows -->
<platform name="windows">
<config-file target="package.appxmanifest" parent="/Package/Capabilities">
<DeviceCapability Name="webcam" />
</config-file>
<js-module src="www/CameraPopoverHandle.js" name="CameraPopoverHandle">
<clobbers target="CameraPopoverHandle" />
</js-module>
<js-module src="src/windows/CameraProxy.js" name="CameraProxy">
<runs />
</js-module>
</platform>
<!-- osx -->
<platform name="osx">
<config-file target="config.xml" parent="/*">
<feature name="Camera">
<param name="osx-package" value="CDVCamera"/>
</feature>
</config-file>
<js-module src="www/CameraPopoverHandle.js" name="CameraPopoverHandle">
<clobbers target="CameraPopoverHandle" />
</js-module>
<header-file src="src/osx/CDVCamera.h" />
<source-file src="src/osx/CDVCamera.m" />
<framework src="Quartz.framework" />
<framework src="AppKit.framework" />
</platform>
</plugin>

File diff suppressed because it is too large Load Diff

View File

@@ -19,6 +19,7 @@
package org.apache.cordova.camera;
import java.io.IOException;
import java.io.InputStream;
import android.media.ExifInterface;
@@ -56,6 +57,16 @@ public class ExifHelper {
this.inFile = new ExifInterface(filePath);
}
/**
* The file before it is compressed
*
* @param input
* @throws IOException
*/
public void createInFile(InputStream input) throws IOException {
this.inFile = new ExifInterface(input);
}
/**
* The file after it has been compressed
*

View File

@@ -19,7 +19,6 @@ package org.apache.cordova.camera;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
@@ -29,8 +28,8 @@ import android.provider.MediaStore;
import android.webkit.MimeTypeMap;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.LOG;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -44,7 +43,7 @@ public class FileHelper {
* Returns the real path of the given URI string.
* If the given URI string represents a content:// URI, the real path is retrieved from the media store.
*
* @param uriString the URI string of the audio/image/video
* @param uri the URI of the audio/image/video
* @param cordova the current application context
* @return the full path to the file
*/
@@ -57,7 +56,7 @@ public class FileHelper {
* Returns the real path of the given URI.
* If the given URI is a content:// URI, the real path is retrieved from the media store.
*
* @param uri the URI of the audio/image/video
* @param uriString the URI string from which to obtain the input stream
* @param cordova the current application context
* @return the full path to the file
*/
@@ -132,6 +131,9 @@ public class FileHelper {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
if (isFileProviderUri(context, uri))
return getFileProviderPath(context, uri);
return getDataColumn(context, uri, null, null);
}
// File
@@ -142,22 +144,6 @@ public class FileHelper {
return null;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
try {
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
} catch (Exception e) {
result = null;
}
return result;
}
/**
* Returns an input stream based on given URI string.
*
@@ -177,6 +163,7 @@ public class FileHelper {
if (question > -1) {
uriString = uriString.substring(0, question);
}
if (uriString.startsWith("file:///android_asset/")) {
Uri uri = Uri.parse(uriString);
String relativePath = uri.getPath().substring(15);
@@ -206,6 +193,7 @@ public class FileHelper {
* @return a path without the "file://" prefix
*/
public static String stripFileProtocol(String uriString) {
if (uriString.startsWith("file://")) {
uriString = uriString.substring(7);
}
@@ -225,7 +213,7 @@ public class FileHelper {
}
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
/**
* Returns the mime type of the data specified by the given URI string.
*
@@ -233,7 +221,7 @@ public class FileHelper {
* @return the mime type of the specified data
*/
public static String getMimeType(String uriString, CordovaInterface cordova) {
String mimeType = null;
String mimeType;
Uri uri = Uri.parse(uriString);
if (uriString.startsWith("content://")) {
@@ -316,4 +304,28 @@ public class FileHelper {
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
/**
* @param context The Application context
* @param uri The Uri is checked by functions
* @return Whether the Uri authority is FileProvider
*/
public static boolean isFileProviderUri(final Context context, final Uri uri) {
final String packageName = context.getPackageName();
final String authority = new StringBuilder(packageName).append(".provider").toString();
return authority.equals(uri.getAuthority());
}
/**
* @param context The Application context
* @param uri The Uri is checked by functions
* @return File path or null if file is missing
*/
public static String getFileProviderPath(final Context context, final Uri uri)
{
final File appDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
final File file = new File(appDir, uri.getLastPathSegment());
return file.exists() ? file.toString(): null;
}
}

View File

@@ -18,4 +18,4 @@
*/
package org.apache.cordova.camera;
public class FileProvider extends android.support.v4.content.FileProvider {}
public class FileProvider extends androidx.core.content.FileProvider {}

View File

@@ -0,0 +1,43 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.cordova.camera;
public class GalleryPathVO {
private final String galleryPath;
private String picturesDirectory;
private String galleryFileName;
public GalleryPathVO(String picturesDirectory, String galleryFileName) {
this.picturesDirectory = picturesDirectory;
this.galleryFileName = galleryFileName;
this.galleryPath = this.picturesDirectory + "/" + this.galleryFileName;
}
public String getGalleryPath() {
return galleryPath;
}
public String getPicturesDirectory() {
return picturesDirectory;
}
public String getGalleryFileName() {
return galleryFileName;
}
}

View File

@@ -17,5 +17,5 @@
-->
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="cache_files" path="." />
</paths>
<cache-path name="cache_files" path="org.apache.cordova.camera/" />
</paths>

View File

@@ -19,13 +19,13 @@
*
*/
var HIGHEST_POSSIBLE_Z_INDEX = 2147483647;
const HIGHEST_POSSIBLE_Z_INDEX = 2147483647;
function takePicture (success, error, opts) {
if (opts && opts[2] === 1) {
capture(success, error, opts);
} else {
var input = document.createElement('input');
const input = document.createElement('input');
input.style.position = 'relative';
input.style.zIndex = HIGHEST_POSSIBLE_Z_INDEX;
input.className = 'cordova-camera-select';
@@ -33,13 +33,13 @@ function takePicture (success, error, opts) {
input.name = 'files[]';
input.onchange = function (inputEvent) {
var reader = new FileReader(); /* eslint no-undef : 0 */
const reader = new FileReader(); /* eslint no-undef : 0 */
reader.onload = function (readerEvent) {
input.parentNode.removeChild(input);
var imageData = readerEvent.target.result;
const imageData = readerEvent.target.result;
return success(imageData.substr(imageData.indexOf(',') + 1));
return success(imageData);
};
reader.readAsDataURL(inputEvent.target.files[0]);
@@ -50,16 +50,16 @@ function takePicture (success, error, opts) {
}
function capture (success, errorCallback, opts) {
var localMediaStream;
var targetWidth = opts[3];
var targetHeight = opts[4];
let localMediaStream;
let targetWidth = opts[3];
let targetHeight = opts[4];
targetWidth = targetWidth === -1 ? 320 : targetWidth;
targetHeight = targetHeight === -1 ? 240 : targetHeight;
var video = document.createElement('video');
var button = document.createElement('button');
var parent = document.createElement('div');
const video = document.createElement('video');
const button = document.createElement('button');
const parent = document.createElement('div');
parent.style.position = 'relative';
parent.style.zIndex = HIGHEST_POSSIBLE_Z_INDEX;
parent.className = 'cordova-camera-capture';
@@ -72,14 +72,13 @@ function capture (success, errorCallback, opts) {
button.onclick = function () {
// create a canvas and capture a frame from video stream
var canvas = document.createElement('canvas');
const canvas = document.createElement('canvas');
canvas.width = targetWidth;
canvas.height = targetHeight;
canvas.getContext('2d').drawImage(video, 0, 0, targetWidth, targetHeight);
// convert image stored in canvas to base64 encoded image
var imageData = canvas.toDataURL('image/png');
imageData = imageData.replace('data:image/png;base64,', '');
const imageData = canvas.toDataURL('image/png');
// stop video stream, remove video and button.
// Note that MediaStream.stop() is deprecated as of Chrome 47.
@@ -100,7 +99,7 @@ function capture (success, errorCallback, opts) {
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
var successCallback = function (stream) {
const successCallback = function (stream) {
localMediaStream = stream;
if ('srcObject' in video) {
video.srcObject = localMediaStream;
@@ -111,7 +110,11 @@ function capture (success, errorCallback, opts) {
document.body.appendChild(parent);
};
if (navigator.getUserMedia) {
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(successCallback)
.catch(errorCallback);
} else if (navigator.getUserMedia) {
navigator.getUserMedia({ video: true, audio: false }, successCallback, errorCallback);
} else {
alert('Browser does not support camera :(');
@@ -119,7 +122,7 @@ function capture (success, errorCallback, opts) {
}
module.exports = {
takePicture: takePicture,
takePicture,
cleanup: function () {}
};

View File

@@ -22,6 +22,26 @@
#import <CoreLocation/CLLocationManager.h>
#import <Cordova/CDVPlugin.h>
// Since iOS 14, we can use PHPickerViewController to select images from the photo library
//
// The following condition checks if the iOS 14 SDK is available for XCode
// which is true for XCode 12+. It does not check on runtime, if the device is running iOS 14+.
// For that API_AVAILABLE(ios(14)) is used for methods declarations and @available(iOS 14, *) for the code.
// The condition here makes just sure that the code can compile in XCode
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 // Always true on XCode12+
// Import UniformTypeIdentifiers.h for using UTType* things, available since iOS 14,
// which replaces for e.g. kUTTypeImage with UTTypeImage, which must be used in the future instead
// Currently only used for PHPickerViewController
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
// Import PhotosUI framework for using PHPickerViewController
// PhotosUI is already available since iOS 8, but since we need it currently
// only for the PHPickerViewController, we import it conditionally here
#import <PhotosUI/PhotosUI.h>
#endif
enum CDVDestinationType {
DestinationTypeDataUrl = 0,
DestinationTypeFileUri
@@ -52,10 +72,8 @@ typedef NSUInteger CDVMediaType;
@property (assign) BOOL allowsEditing;
@property (assign) BOOL correctOrientation;
@property (assign) BOOL saveToPhotoAlbum;
@property (strong) NSDictionary* popoverOptions;
@property (assign) UIImagePickerControllerCameraDevice cameraDirection;
@property (assign) BOOL popoverSupported;
@property (assign) BOOL usesGeolocation;
@property (assign) BOOL cropToSize;
@@ -69,7 +87,6 @@ typedef NSUInteger CDVMediaType;
@property (copy) NSString* callbackId;
@property (copy) NSString* postUrl;
@property (strong) UIPopoverController* pickerPopoverController;
@property (assign) BOOL cropToSize;
@property (strong) UIView* webView;
@@ -78,38 +95,48 @@ typedef NSUInteger CDVMediaType;
@end
// ======================================================================= //
// Use PHPickerViewController in iOS 14+ to select images from the photo library
// PHPickerViewControllerDelegate is only available since iOS 14
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 // Always true on XCode12+
@interface CDVCamera : CDVPlugin <UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
CLLocationManagerDelegate,
PHPickerViewControllerDelegate>
{}
#else
@interface CDVCamera : CDVPlugin <UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIPopoverControllerDelegate,
CLLocationManagerDelegate>
{}
#endif
@property (strong) CDVCameraPicker* pickerController;
@property (strong) NSMutableDictionary *metadata;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong) NSData* data;
/*
* getPicture
*
* arguments:
* 1: this is the javascript function that will be called with the results, the first parameter passed to the
* javascript function is the picture as a Base64 encoded string
* 2: this is the javascript function to be called if there was an error
* options:
* quality: integer between 1 and 100
*/
- (void)takePicture:(CDVInvokedUrlCommand*)command;
- (void)cleanup:(CDVInvokedUrlCommand*)command;
- (void)repositionPopover:(CDVInvokedUrlCommand*)command;
// UIImagePickerControllerDelegate methods
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info;
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo;
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker;
// UINavigationControllerDelegate method
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
// CLLocationManagerDelegate methods
- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation;
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
// PHPickerViewController specific methods (iOS 14+)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 // Always true on XCode12+
- (void)showPHPicker:(NSString*)callbackId withOptions:(CDVPictureOptions*)pictureOptions API_AVAILABLE(ios(14));
- (void)processPHPickerImage:(UIImage*)image assetIdentifier:(NSString*)assetIdentifier callbackId:(NSString*)callbackId options:(CDVPictureOptions*)options API_AVAILABLE(ios(14));
- (void)finalizePHPickerImage:(UIImage*)image metadata:(NSDictionary*)metadata callbackId:(NSString*)callbackId options:(CDVPictureOptions*)options API_AVAILABLE(ios(14));
// PHPickerViewControllerDelegate method
- (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results API_AVAILABLE(ios(14));
#endif
@end

View File

@@ -29,6 +29,7 @@
#import <ImageIO/CGImageDestination.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import <objc/message.h>
#import <Photos/Photos.h>
#ifndef __CORDOVA_4_0_0
#import <Cordova/NSData+Base64.h>
@@ -36,9 +37,8 @@
#define CDV_PHOTO_PREFIX @"cdv_photo_"
static NSSet* org_apache_cordova_validArrowDirections;
static NSString* toBase64(NSData* data) {
static NSString* toBase64(NSData* data)
{
SEL s1 = NSSelectorFromString(@"cdv_base64EncodedString");
SEL s2 = NSSelectorFromString(@"base64EncodedString");
SEL s3 = NSSelectorFromString(@"base64EncodedStringWithOptions:");
@@ -57,9 +57,12 @@ static NSString* toBase64(NSData* data) {
}
}
static NSString* MIME_PNG = @"image/png";
static NSString* MIME_JPEG = @"image/jpeg";
@implementation CDVPictureOptions
+ (instancetype) createFromTakePictureArguments:(CDVInvokedUrlCommand*)command
+ (instancetype)createFromTakePictureArguments:(CDVInvokedUrlCommand*)command
{
CDVPictureOptions* pictureOptions = [[CDVPictureOptions alloc] init];
@@ -79,10 +82,8 @@ static NSString* toBase64(NSData* data) {
pictureOptions.allowsEditing = [[command argumentAtIndex:7 withDefault:@(NO)] boolValue];
pictureOptions.correctOrientation = [[command argumentAtIndex:8 withDefault:@(NO)] boolValue];
pictureOptions.saveToPhotoAlbum = [[command argumentAtIndex:9 withDefault:@(NO)] boolValue];
pictureOptions.popoverOptions = [command argumentAtIndex:10 withDefault:nil];
pictureOptions.cameraDirection = [[command argumentAtIndex:11 withDefault:@(UIImagePickerControllerCameraDeviceRear)] unsignedIntegerValue];
pictureOptions.cameraDirection = [[command argumentAtIndex:10 withDefault:@(UIImagePickerControllerCameraDeviceRear)] unsignedIntegerValue];
pictureOptions.popoverSupported = NO;
pictureOptions.usesGeolocation = NO;
return pictureOptions;
@@ -99,14 +100,9 @@ static NSString* toBase64(NSData* data) {
@implementation CDVCamera
+ (void)initialize
{
org_apache_cordova_validArrowDirections = [[NSSet alloc] initWithObjects:[NSNumber numberWithInt:UIPopoverArrowDirectionUp], [NSNumber numberWithInt:UIPopoverArrowDirectionDown], [NSNumber numberWithInt:UIPopoverArrowDirectionLeft], [NSNumber numberWithInt:UIPopoverArrowDirectionRight], [NSNumber numberWithInt:UIPopoverArrowDirectionAny], nil];
}
@synthesize hasPendingOperation, pickerController, locationManager;
- (NSURL*) urlTransformer:(NSURL*)url
- (NSURL*)urlTransformer:(NSURL*)url
{
NSURL* urlToTransform = url;
@@ -130,12 +126,28 @@ static NSString* toBase64(NSData* data) {
return [(NSNumber*)useGeo boolValue];
}
- (BOOL)popoverSupported
{
return (NSClassFromString(@"UIPopoverController") != nil) &&
(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
}
/**
Called by JS function navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions)
which will invoke the camera or photo picker to capture or select an image or video.
@param command A Cordova command whose arguments map to camera options:
- index 0 (quality): NSNumber (1100). JPEG quality when encodingType is JPEG. Default: 50.
- index 1 (destinationType): NSNumber (DestinationType). File URI or Data URL. Default: File URI.
- index 2 (sourceType): NSNumber (UIImagePickerControllerSourceType). Camera or Photo Library. Default: Camera.
- index 3 (targetWidth): NSNumber (optional). Desired width for scaling/cropping.
- index 4 (targetHeight): NSNumber (optional). Desired height for scaling/cropping.
- index 5 (encodingType): NSNumber (EncodingType). JPEG or PNG. Default: JPEG.
- index 6 (mediaType): NSNumber (MediaType). Picture, Video, or All. Default: Picture.
- index 7 (allowsEditing): NSNumber(BOOL). Allow user to crop/edit. Default: NO.
- index 8 (correctOrientation): NSNumber(BOOL). Fix EXIF orientation. Default: NO.
- index 9 (saveToPhotoAlbum): NSNumber(BOOL). Save captured image to Photos. Default: NO.
- index 10 (cameraDirection): NSNumber (UIImagePickerControllerCameraDevice). Front/Rear. Default: Rear.
@discussion
This method validates hardware availability and permissions (camera or photo library),
then presents the appropriate UI (UIImagePickerController or PHPickerViewController on iOS 14+).
The result is returned via the Cordova callback.
*/
- (void)takePicture:(CDVInvokedUrlCommand*)command
{
self.hasPendingOperation = YES;
@@ -143,77 +155,90 @@ static NSString* toBase64(NSData* data) {
[self.commandDelegate runInBackground:^{
CDVPictureOptions* pictureOptions = [CDVPictureOptions createFromTakePictureArguments:command];
pictureOptions.popoverSupported = [weakSelf popoverSupported];
pictureOptions.usesGeolocation = [weakSelf usesGeolocation];
pictureOptions.cropToSize = NO;
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:pictureOptions.sourceType];
if (!hasCamera) {
NSLog(@"Camera.getPicture: source type %lu not available.", (unsigned long)pictureOptions.sourceType);
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No camera available"];
[weakSelf.commandDelegate sendPluginResult:result callbackId:command.callbackId];
return;
}
// Validate the app has permission to access the camera
// The camera should be used to take a picture
if (pictureOptions.sourceType == UIImagePickerControllerSourceTypeCamera) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
{
if(!granted)
{
// Denied; show an alert
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"] message:NSLocalizedString(@"Access to the camera has been prohibited; please enable it in the Settings app to continue.", nil) preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf sendNoPermissionResult:command.callbackId];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Settings", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
[weakSelf sendNoPermissionResult:command.callbackId];
}]];
[weakSelf.viewController presentViewController:alertController animated:YES completion:nil];
});
// Check if camera is available
if (![UIImagePickerController isSourceTypeAvailable:pictureOptions.sourceType]) {
NSLog(@"Camera.getPicture: source type %lu not available.", (unsigned long)pictureOptions.sourceType);
[weakSelf.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No camera available"]
callbackId:command.callbackId];
return;
}
// Validate the app has permission to access the camera
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
// Show an alert if not granted
if (!granted) {
[weakSelf presentPermissionDeniedAlertWithMessage:@"Access to the camera has been prohibited; please enable it in the Settings app to continue."
callbackId:command.callbackId];
} else {
[weakSelf showCameraPicker:command.callbackId withOptions:pictureOptions];
}
}];
// A photo should be picked from the photo library
} else {
[weakSelf showCameraPicker:command.callbackId withOptions:pictureOptions];
// Use PHPickerViewController on iOS 14+
// Doesn't require permissions
if (@available(iOS 14, *)) {
[weakSelf showCameraPicker:command.callbackId withOptions:pictureOptions];
// On iOS < 14, use UIImagePickerController and request permissions
} else {
// Request permission
[weakSelf options:pictureOptions requestPhotoPermissions:^(BOOL granted) {
if (!granted) {
// Denied; show an alert
[weakSelf presentPermissionDeniedAlertWithMessage:@"Access to the camera roll has been prohibited; please enable it in the Settings to continue."
callbackId:command.callbackId];
} else {
[weakSelf showCameraPicker:command.callbackId withOptions:pictureOptions];
}
}];
}
}
}];
}
- (void)showCameraPicker:(NSString*)callbackId withOptions:(CDVPictureOptions *) pictureOptions
/**
Presents a permission denial alert with OK and Settings actions.
@param message The alert message to show.
@param callbackId The Cordova callback identifier to send an error if needed.
*/
- (void)presentPermissionDeniedAlertWithMessage:(NSString*)message callbackId:(NSString*)callbackId
{
// Perform UI operations on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
CDVCameraPicker* cameraPicker = [CDVCameraPicker createFromPictureOptions:pictureOptions];
self.pickerController = cameraPicker;
NSString *bundleDisplayName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:bundleDisplayName
message:NSLocalizedString(message, nil)
preferredStyle:UIAlertControllerStyleAlert];
// Add buttons
__weak CDVCamera *weakSelf = self;
// Ok button
[alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[weakSelf sendNoPermissionResult:callbackId];
}]];
// Button for open settings
[alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Settings", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
// Open settings
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]
options:@{}
completionHandler:nil];
[weakSelf sendNoPermissionResult:callbackId];
}]];
cameraPicker.delegate = self;
cameraPicker.callbackId = callbackId;
// we need to capture this state for memory warnings that dealloc this object
cameraPicker.webView = self.webView;
// If a popover is already open, close it; we only want one at a time.
if (([[self pickerController] pickerPopoverController] != nil) && [[[self pickerController] pickerPopoverController] isPopoverVisible]) {
[[[self pickerController] pickerPopoverController] dismissPopoverAnimated:YES];
[[[self pickerController] pickerPopoverController] setDelegate:nil];
[[self pickerController] setPickerPopoverController:nil];
}
if ([self popoverSupported] && (pictureOptions.sourceType != UIImagePickerControllerSourceTypeCamera)) {
if (cameraPicker.pickerPopoverController == nil) {
cameraPicker.pickerPopoverController = [[NSClassFromString(@"UIPopoverController") alloc] initWithContentViewController:cameraPicker];
}
[self displayPopover:pictureOptions.popoverOptions];
self.hasPendingOperation = NO;
} else {
cameraPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
[self.viewController presentViewController:cameraPicker animated:YES completion:^{
self.hasPendingOperation = NO;
}];
}
[self.viewController presentViewController:alertController animated:YES completion:nil];
});
}
@@ -222,22 +247,251 @@ static NSString* toBase64(NSData* data) {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"has no access to camera"]; // error callback expects string ATM
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
self.hasPendingOperation = NO;
self.pickerController = nil;
}
- (void)repositionPopover:(CDVInvokedUrlCommand*)command
/**
Presents the appropriate UI to capture or select media based on the provided options and OS version.
On iOS 14 and later, when the source type is PHOTOLIBRARY (or SAVEDPHOTOALBUM), this method presents
PHPickerViewController to select media without requiring Photos authorization. Otherwise, it falls back
to UIImagePickerController for camera usage or on older iOS versions.
Threading:
- Ensures presentation occurs on the main thread.
Behavior:
- Configures delegates and media types
- Updates `hasPendingOperation` to reflect plugin activity state.
@param callbackId The Cordova callback identifier used to deliver results back to JavaScript.
@param pictureOptions Parsed camera options (sourceType, mediaType, allowsEditing, etc.).
*/
- (void)showCameraPicker:(NSString*)callbackId withOptions:(CDVPictureOptions*)pictureOptions
{
if (([[self pickerController] pickerPopoverController] != nil) && [[[self pickerController] pickerPopoverController] isPopoverVisible]) {
// Perform UI operations on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Use PHPickerViewController for photo library on iOS 14+
if (@available(iOS 14, *)) {
// sourceType is PHOTOLIBRARY
if (pictureOptions.sourceType == UIImagePickerControllerSourceTypePhotoLibrary ||
// sourceType is SAVEDPHOTOALBUM (same as PHOTOLIBRARY)
pictureOptions.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
[self showPHPicker:callbackId withOptions:pictureOptions];
return;
}
}
// Use UIImagePickerController for camera or as image picker for iOS older than 14
CDVCameraPicker* cameraPicker = [CDVCameraPicker createFromPictureOptions:pictureOptions];
self.pickerController = cameraPicker;
[[[self pickerController] pickerPopoverController] dismissPopoverAnimated:NO];
NSDictionary* options = [command argumentAtIndex:0 withDefault:nil];
[self displayPopover:options];
}
cameraPicker.delegate = self;
cameraPicker.callbackId = callbackId;
// we need to capture this state for memory warnings that dealloc this object
cameraPicker.webView = self.webView;
cameraPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
[self.viewController presentViewController:cameraPicker
animated:YES
completion:^{
self.hasPendingOperation = NO;
}];
});
}
// Since iOS 14, we can use PHPickerViewController to select images from the photo library
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 // Always true on XCode12+
- (void)showPHPicker:(NSString*)callbackId withOptions:(CDVPictureOptions*)pictureOptions API_AVAILABLE(ios(14))
{
PHPickerConfiguration *config = [[PHPickerConfiguration alloc] init];
// Configure filter based on media type
// Images
if (pictureOptions.mediaType == MediaTypePicture) {
config.filter = [PHPickerFilter imagesFilter];
// Videos
} else if (pictureOptions.mediaType == MediaTypeVideo) {
config.filter = [PHPickerFilter videosFilter];
// Images and videos
} else if (pictureOptions.mediaType == MediaTypeAll) {
config.filter = [PHPickerFilter anyFilterMatchingSubfilters:@[
[PHPickerFilter imagesFilter],
[PHPickerFilter videosFilter]
]];
}
config.selectionLimit = 1;
config.preferredAssetRepresentationMode = PHPickerConfigurationAssetRepresentationModeCurrent;
PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:config];
picker.delegate = self;
// Store callback ID and options in picker with objc_setAssociatedObject
// PHPickerViewControllers delegate method picker:didFinishPicking: only gives you back the picker instance
// and the results array. It doesnt carry arbitrary context. By associating the callbackId and pictureOptions
// with the picker, you can retrieve them later inside the delegate method
objc_setAssociatedObject(picker, "callbackId", callbackId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(picker, "pictureOptions", pictureOptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self.viewController presentViewController:picker animated:YES completion:^{
self.hasPendingOperation = NO;
}];
}
// PHPickerViewControllerDelegate method
- (void)picker:(PHPickerViewController*)picker didFinishPicking:(NSArray<PHPickerResult*>*)results API_AVAILABLE(ios(14))
{
NSString *callbackId = objc_getAssociatedObject(picker, "callbackId");
CDVPictureOptions *pictureOptions = objc_getAssociatedObject(picker, "pictureOptions");
__weak CDVCamera* weakSelf = self;
[picker dismissViewControllerAnimated:YES completion:^{
if (results.count == 0) {
// User cancelled
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No Image Selected"];
[weakSelf.commandDelegate sendPluginResult:result callbackId:callbackId];
weakSelf.hasPendingOperation = NO;
return;
}
PHPickerResult *pickerResult = results.firstObject;
// Check if it's a video
if ([pickerResult.itemProvider hasItemConformingToTypeIdentifier:UTTypeMovie.identifier]) {
[pickerResult.itemProvider loadFileRepresentationForTypeIdentifier:UTTypeMovie.identifier completionHandler:^(NSURL * _Nullable url, NSError * _Nullable error) {
if (error) {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
[weakSelf.commandDelegate sendPluginResult:result callbackId:callbackId];
weakSelf.hasPendingOperation = NO;
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
NSString* videoPath = [weakSelf createTmpVideo:[url path]];
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:videoPath];
[weakSelf.commandDelegate sendPluginResult:result callbackId:callbackId];
weakSelf.hasPendingOperation = NO;
});
}];
// Handle image
} else if ([pickerResult.itemProvider canLoadObjectOfClass:[UIImage class]]) {
[pickerResult.itemProvider loadObjectOfClass:[UIImage class] completionHandler:^(__kindof id<NSItemProviderReading> _Nullable object, NSError * _Nullable error) {
if (error) {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
[weakSelf.commandDelegate sendPluginResult:result callbackId:callbackId];
weakSelf.hasPendingOperation = NO;
return;
}
UIImage *image = (UIImage *)object;
// Get asset identifier to fetch metadata
NSString *assetIdentifier = pickerResult.assetIdentifier;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf processPHPickerImage:image assetIdentifier:assetIdentifier callbackId:callbackId options:pictureOptions];
});
}];
}
}];
}
- (void)processPHPickerImage:(UIImage*)image
assetIdentifier:(NSString*)assetIdentifier
callbackId:(NSString*)callbackId
options:(CDVPictureOptions*)options API_AVAILABLE(ios(14))
{
__weak CDVCamera* weakSelf = self;
// Fetch metadata if asset identifier is available
if (assetIdentifier) {
PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetIdentifier] options:nil];
PHAsset *asset = result.firstObject;
if (asset) {
PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
imageOptions.synchronous = YES;
imageOptions.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:asset
options:imageOptions
resultHandler:^(NSData *_Nullable imageData, NSString *_Nullable dataUTI, CGImagePropertyOrientation orientation, NSDictionary *_Nullable info) {
NSDictionary *metadata = imageData ? [weakSelf convertImageMetadata:imageData] : nil;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf finalizePHPickerImage:image metadata:metadata callbackId:callbackId options:options];
});
}];
return;
}
}
// No metadata available
[self finalizePHPickerImage:image metadata:nil callbackId:callbackId options:options];
}
- (void)finalizePHPickerImage:(UIImage*)image
metadata:(NSDictionary*)metadata
callbackId:(NSString*)callbackId
options:(CDVPictureOptions*)options API_AVAILABLE(ios(14))
{
// Process image according to options
UIImage *processedImage = image;
if (options.correctOrientation) {
processedImage = [processedImage imageCorrectedForCaptureOrientation];
}
if ((options.targetSize.width > 0) && (options.targetSize.height > 0)) {
if (options.cropToSize) {
processedImage = [processedImage imageByScalingAndCroppingForSize:options.targetSize];
} else {
processedImage = [processedImage imageByScalingNotCroppingForSize:options.targetSize];
}
}
// Create info dictionary similar to UIImagePickerController
NSMutableDictionary *info = [NSMutableDictionary dictionary];
[info setObject:processedImage forKey:UIImagePickerControllerOriginalImage];
if (metadata) {
[info setObject:metadata forKey:@"UIImagePickerControllerMediaMetadata"];
}
// Store metadata for processing
if (metadata) {
self.metadata = [[NSMutableDictionary alloc] init];
NSMutableDictionary* EXIFDictionary = [[metadata objectForKey:(NSString*)kCGImagePropertyExifDictionary] mutableCopy];
if (EXIFDictionary) {
[self.metadata setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];
}
NSMutableDictionary* TIFFDictionary = [[metadata objectForKey:(NSString*)kCGImagePropertyTIFFDictionary] mutableCopy];
if (TIFFDictionary) {
[self.metadata setObject:TIFFDictionary forKey:(NSString*)kCGImagePropertyTIFFDictionary];
}
NSMutableDictionary* GPSDictionary = [[metadata objectForKey:(NSString*)kCGImagePropertyGPSDictionary] mutableCopy];
if (GPSDictionary) {
[self.metadata setObject:GPSDictionary forKey:(NSString*)kCGImagePropertyGPSDictionary];
}
}
__weak CDVCamera* weakSelf = self;
// Process and return result
[self resultForImage:options info:info completion:^(CDVPluginResult* res) {
[weakSelf.commandDelegate sendPluginResult:res callbackId:callbackId];
weakSelf.hasPendingOperation = NO;
weakSelf.pickerController = nil;
}];
}
#endif
- (NSInteger)integerValueForKey:(NSDictionary*)dict key:(NSString*)key defaultValue:(NSInteger)defaultValue
{
NSInteger value = defaultValue;
@@ -250,51 +504,18 @@ static NSString* toBase64(NSData* data) {
return value;
}
- (void)displayPopover:(NSDictionary*)options
// UINavigationControllerDelegate method
- (void)navigationController:(UINavigationController*)navigationController
willShowViewController:(UIViewController*)viewController
animated:(BOOL)animated
{
NSInteger x = 0;
NSInteger y = 32;
NSInteger width = 320;
NSInteger height = 480;
UIPopoverArrowDirection arrowDirection = UIPopoverArrowDirectionAny;
// Backward compatibility for iOS < 14
// Set title "Videos", when picking videos with the legacy UIImagePickerController
if([navigationController isKindOfClass:[UIImagePickerController class]]) {
UIImagePickerController* imagePickerController = (UIImagePickerController*)navigationController;
if (options) {
x = [self integerValueForKey:options key:@"x" defaultValue:0];
y = [self integerValueForKey:options key:@"y" defaultValue:32];
width = [self integerValueForKey:options key:@"width" defaultValue:320];
height = [self integerValueForKey:options key:@"height" defaultValue:480];
arrowDirection = [self integerValueForKey:options key:@"arrowDir" defaultValue:UIPopoverArrowDirectionAny];
if (![org_apache_cordova_validArrowDirections containsObject:[NSNumber numberWithUnsignedInteger:arrowDirection]]) {
arrowDirection = UIPopoverArrowDirectionAny;
}
}
[[[self pickerController] pickerPopoverController] setDelegate:self];
[[[self pickerController] pickerPopoverController] presentPopoverFromRect:CGRectMake(x, y, width, height)
inView:[self.webView superview]
permittedArrowDirections:arrowDirection
animated:YES];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if([navigationController isKindOfClass:[UIImagePickerController class]]){
// If popoverWidth and popoverHeight are specified and are greater than 0, then set popover size, else use apple's default popoverSize
NSDictionary* options = self.pickerController.pictureOptions.popoverOptions;
if(options) {
NSInteger popoverWidth = [self integerValueForKey:options key:@"popoverWidth" defaultValue:0];
NSInteger popoverHeight = [self integerValueForKey:options key:@"popoverHeight" defaultValue:0];
if(popoverWidth > 0 && popoverHeight > 0)
{
[viewController setPreferredContentSize:CGSizeMake(popoverWidth,popoverHeight)];
}
}
UIImagePickerController* cameraPicker = (UIImagePickerController*)navigationController;
if(![cameraPicker.mediaTypes containsObject:(NSString*)kUTTypeImage]){
// Set title "Videos" when picking not images
if(![imagePickerController.mediaTypes containsObject:(NSString*)kUTTypeImage]) {
[viewController.navigationItem setTitle:NSLocalizedString(@"Videos", nil)];
}
}
@@ -335,31 +556,52 @@ static NSString* toBase64(NSData* data) {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)popoverControllerDidDismissPopover:(id)popoverController
- (NSString*)getMimeForEncoding:(CDVEncodingType)encoding
{
UIPopoverController* pc = (UIPopoverController*)popoverController;
[pc dismissPopoverAnimated:YES];
pc.delegate = nil;
if (self.pickerController && self.pickerController.callbackId && self.pickerController.pickerPopoverController) {
self.pickerController.pickerPopoverController = nil;
NSString* callbackId = self.pickerController.callbackId;
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"no image selected"]; // error callback expects string ATM
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
switch (encoding) {
case EncodingTypePNG: return MIME_PNG;
case EncodingTypeJPEG:
default:
return MIME_JPEG;
}
self.hasPendingOperation = NO;
}
- (NSString*)formatAsDataURI:(NSData*)data withMIME:(NSString*)mime
{
NSString* base64 = toBase64(data);
if (base64 == nil) {
return nil;
}
return [NSString stringWithFormat:@"data:%@;base64,%@", mime, base64];
}
- (NSString*)processImageAsDataUri:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options
{
NSString* mime = nil;
NSData* data = [self processImage:image info:info options:options outMime:&mime];
return [self formatAsDataURI:data withMIME:mime];
}
- (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options
{
return [self processImage:image info:info options:options outMime:nil];
}
- (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options outMime:(NSString**)outMime
{
NSData* data = nil;
switch (options.encodingType) {
case EncodingTypePNG:
data = UIImagePNGRepresentation(image);
if (outMime != nil) *outMime = MIME_PNG;
break;
case EncodingTypeJPEG:
{
if (outMime != nil) *outMime = MIME_JPEG;
if ((options.allowsEditing == NO) && (options.targetSize.width <= 0) && (options.targetSize.height <= 0) && (options.correctOrientation == NO) && (([options.quality integerValue] == 100) || (options.sourceType != UIImagePickerControllerSourceTypeCamera))){
// use image unedited as requested , don't resize
data = UIImageJPEGRepresentation(image, 1.0);
@@ -367,33 +609,142 @@ static NSString* toBase64(NSData* data) {
data = UIImageJPEGRepresentation(image, [options.quality floatValue] / 100.0f);
}
if (options.usesGeolocation) {
NSDictionary* controllerMetadata = [info objectForKey:@"UIImagePickerControllerMediaMetadata"];
if (pickerController.sourceType == UIImagePickerControllerSourceTypeCamera) {
if (options.usesGeolocation) {
NSDictionary* controllerMetadata = [info objectForKey:@"UIImagePickerControllerMediaMetadata"];
if (controllerMetadata) {
self.data = data;
self.metadata = [[NSMutableDictionary alloc] init];
NSMutableDictionary* EXIFDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
if (EXIFDictionary) {
[self.metadata setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];
}
if (IsAtLeastiOSVersion(@"8.0")) {
[[self locationManager] performSelector:NSSelectorFromString(@"requestWhenInUseAuthorization") withObject:nil afterDelay:0];
}
[[self locationManager] startUpdatingLocation];
}
data = nil;
}
} else if (pickerController.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
PHAsset* asset = [info objectForKey:@"UIImagePickerControllerPHAsset"];
NSDictionary* controllerMetadata = [self getImageMetadataFromAsset:asset];
self.data = data;
if (controllerMetadata) {
self.data = data;
self.metadata = [[NSMutableDictionary alloc] init];
NSMutableDictionary* EXIFDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
if (EXIFDictionary) {
[self.metadata setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];
}
if (IsAtLeastiOSVersion(@"8.0")) {
[[self locationManager] performSelector:NSSelectorFromString(@"requestWhenInUseAuthorization") withObject:nil afterDelay:0];
NSMutableDictionary* TIFFDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyTIFFDictionary
]mutableCopy];
if (TIFFDictionary) {
[self.metadata setObject:TIFFDictionary forKey:(NSString*)kCGImagePropertyTIFFDictionary];
}
NSMutableDictionary* GPSDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyGPSDictionary
]mutableCopy];
if (GPSDictionary) {
[self.metadata setObject:GPSDictionary forKey:(NSString*)kCGImagePropertyGPSDictionary
];
}
[[self locationManager] startUpdatingLocation];
}
data = nil;
}
}
break;
default:
break;
};
return data;
}
/* --------------------------------------------------------------
-- get the metadata of the image from a PHAsset
-------------------------------------------------------------- */
- (NSDictionary*)getImageMetadataFromAsset:(PHAsset*)asset
{
if(asset == nil) {
return nil;
}
// get photo info from this asset
__block NSDictionary *dict = nil;
PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;
[[PHImageManager defaultManager]
requestImageDataForAsset:asset
options:imageRequestOptions
resultHandler: ^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
dict = [self convertImageMetadata:imageData]; // as this imageData is in NSData format so we need a method to convert this NSData into NSDictionary
}];
return dict;
}
- (NSDictionary*)convertImageMetadata:(NSData*)imageData
{
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)(imageData), NULL);
if (imageSource) {
NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache : [NSNumber numberWithBool:NO]};
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options);
if (imageProperties) {
NSDictionary *metadata = (__bridge NSDictionary *)imageProperties;
CFRelease(imageProperties);
CFRelease(imageSource);
NSLog(@"Metadata of selected image%@", metadata);// image metadata after converting NSData into NSDictionary
return metadata;
}
CFRelease(imageSource);
}
NSLog(@"Can't read image metadata");
return nil;
}
/**
Requests Photos library permissions when needed for picking media from the photo library.
This is only needed for iOS 13 and older when using UIImagePickerController for picking an image.
On iOS 14 and later, PHPickerViewController is used and does not need extra permissions.
@param options The picture options indicating the requested source type.
@param completion A block invoked with YES when access is authorized (or not required),
or NO when access is denied or restricted.
*/
- (void)options:(CDVPictureOptions*)options requestPhotoPermissions:(void (^)(BOOL auth))completion
{
// This is would be no good response
if(options.sourceType == UIImagePickerControllerSourceTypeCamera) {
completion(YES);
} else {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch (status) {
case PHAuthorizationStatusAuthorized:
completion(YES);
break;
case PHAuthorizationStatusNotDetermined: {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus authorizationStatus) {
if (authorizationStatus == PHAuthorizationStatusAuthorized) {
completion(YES);
} else {
completion(NO);
}
}];
break;
}
default:
completion(NO);
break;
}
}
}
- (NSString*)tempFilePath:(NSString*)extension
{
NSString* docsPath = [NSTemporaryDirectory()stringByStandardizingPath];
@@ -433,7 +784,9 @@ static NSString* toBase64(NSData* data) {
return (scaledImage == nil ? image : scaledImage);
}
- (void)resultForImage:(CDVPictureOptions*)options info:(NSDictionary*)info completion:(void (^)(CDVPluginResult* res))completion
- (void)resultForImage:(CDVPictureOptions*)options
info:(NSDictionary*)info
completion:(void (^)(CDVPluginResult* res))completion
{
CDVPluginResult* result = nil;
BOOL saveToPhotoAlbum = options.saveToPhotoAlbum;
@@ -443,9 +796,9 @@ static NSString* toBase64(NSData* data) {
case DestinationTypeDataUrl:
{
image = [self retrieveImage:info options:options];
NSData* data = [self processImage:image info:info options:options];
NSString* data = [self processImageAsDataUri:image info:info options:options];
if (data) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(data)];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: data];
}
}
break;
@@ -453,18 +806,50 @@ static NSString* toBase64(NSData* data) {
{
image = [self retrieveImage:info options:options];
NSData* data = [self processImage:image info:info options:options];
if (data) {
if (pickerController.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
NSMutableData *imageDataWithExif = [NSMutableData data];
if (self.metadata) {
CGImageSourceRef sourceImage = CGImageSourceCreateWithData((__bridge CFDataRef)self.data, NULL);
CFStringRef sourceType = CGImageSourceGetType(sourceImage);
NSString* extension = options.encodingType == EncodingTypePNG? @"png" : @"jpg";
NSString* filePath = [self tempFilePath:extension];
NSError* err = nil;
CGImageDestinationRef destinationImage = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageDataWithExif, sourceType, 1, NULL);
CGImageDestinationAddImageFromSource(destinationImage, sourceImage, 0, (__bridge CFDictionaryRef)self.metadata);
CGImageDestinationFinalize(destinationImage);
// save file
if (![data writeToFile:filePath options:NSAtomicWrite error:&err]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[self urlTransformer:[NSURL fileURLWithPath:filePath]] absoluteString]];
CFRelease(sourceImage);
CFRelease(destinationImage);
} else {
imageDataWithExif = [self.data mutableCopy];
}
NSError* err = nil;
NSString* extension = self.pickerController.pictureOptions.encodingType == EncodingTypePNG ? @"png":@"jpg";
NSString* filePath = [self tempFilePath:extension];
// save file
if (![imageDataWithExif writeToFile:filePath options:NSAtomicWrite error:&err]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
}
else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[self urlTransformer:[NSURL fileURLWithPath:filePath]] absoluteString]];
}
} else if (pickerController.sourceType != UIImagePickerControllerSourceTypeCamera || !options.usesGeolocation) {
// No need to save file if usesGeolocation is true since it will be saved after the location is tracked
NSString* extension = options.encodingType == EncodingTypePNG? @"png" : @"jpg";
NSString* filePath = [self tempFilePath:extension];
NSError* err = nil;
// save file
if (![data writeToFile:filePath options:NSAtomicWrite error:&err]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[self urlTransformer:[NSURL fileURLWithPath:filePath]] absoluteString]];
}
}
}
}
break;
@@ -487,7 +872,8 @@ static NSString* toBase64(NSData* data) {
return [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:moviePath];
}
- (NSString *) createTmpVideo:(NSString *) moviePath {
- (NSString*)createTmpVideo:(NSString*)moviePath
{
NSString* moviePathExtension = [moviePath pathExtension];
NSString* copyMoviePath = [self tempFilePath:moviePathExtension];
NSFileManager* fileMgr = [[NSFileManager alloc] init];
@@ -496,6 +882,8 @@ static NSString* toBase64(NSData* data) {
return [[NSURL fileURLWithPath:copyMoviePath] absoluteString];
}
#pragma mark UIImagePickerControllerDelegate methods
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
__weak CDVCameraPicker* cameraPicker = (CDVCameraPicker*)picker;
@@ -522,18 +910,13 @@ static NSString* toBase64(NSData* data) {
}
};
if (cameraPicker.pictureOptions.popoverSupported && (cameraPicker.pickerPopoverController != nil)) {
[cameraPicker.pickerPopoverController dismissPopoverAnimated:YES];
cameraPicker.pickerPopoverController.delegate = nil;
cameraPicker.pickerPopoverController = nil;
invoke();
} else {
[[cameraPicker presentingViewController] dismissViewControllerAnimated:YES completion:invoke];
}
[[cameraPicker presentingViewController] dismissViewControllerAnimated:YES completion:invoke];
}
// older api calls newer didFinishPickingMediaWithInfo
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo
- (void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingImage:(UIImage*)image
editingInfo:(NSDictionary*)editingInfo
{
NSDictionary* imageInfo = [NSDictionary dictionaryWithObject:image forKey:UIImagePickerControllerOriginalImage];
@@ -563,6 +946,8 @@ static NSString* toBase64(NSData* data) {
[[cameraPicker presentingViewController] dismissViewControllerAnimated:YES completion:invoke];
}
#pragma mark CLLocationManager
- (CLLocationManager*)locationManager
{
if (locationManager != nil) {
@@ -576,7 +961,11 @@ static NSString* toBase64(NSData* data) {
return locationManager;
}
- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation
# pragma mark CLLocationManagerDelegate methods
- (void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation
{
if (locationManager == nil) {
return;
@@ -649,23 +1038,31 @@ static NSString* toBase64(NSData* data) {
{
CDVPictureOptions* options = self.pickerController.pictureOptions;
CDVPluginResult* result = nil;
NSMutableData *imageDataWithExif = [NSMutableData data];
if (self.metadata) {
CGImageSourceRef sourceImage = CGImageSourceCreateWithData((__bridge CFDataRef)self.data, NULL);
NSData* dataCopy = [self.data mutableCopy];
CGImageSourceRef sourceImage = CGImageSourceCreateWithData((__bridge CFDataRef)dataCopy, NULL);
CFStringRef sourceType = CGImageSourceGetType(sourceImage);
CGImageDestinationRef destinationImage = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)self.data, sourceType, 1, NULL);
CGImageDestinationRef destinationImage = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageDataWithExif, sourceType, 1, NULL);
CGImageDestinationAddImageFromSource(destinationImage, sourceImage, 0, (__bridge CFDictionaryRef)self.metadata);
CGImageDestinationFinalize(destinationImage);
dataCopy = nil;
CFRelease(sourceImage);
CFRelease(destinationImage);
} else {
imageDataWithExif = [self.data mutableCopy];
}
switch (options.destinationType) {
case DestinationTypeDataUrl:
{
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(self.data)];
NSString* mime = [self getMimeForEncoding: self.pickerController.pictureOptions.encodingType];
NSString* uri = [self formatAsDataURI: self.data withMIME: mime];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: uri];
}
break;
default: // DestinationTypeFileUri
@@ -693,7 +1090,7 @@ static NSString* toBase64(NSData* data) {
self.pickerController = nil;
self.data = nil;
self.metadata = nil;
imageDataWithExif = nil;
if (options.saveToPhotoAlbum) {
UIImageWriteToSavedPhotosAlbum([[UIImage alloc] initWithData:self.data], nil, nil, nil);
}
@@ -723,7 +1120,7 @@ static NSString* toBase64(NSData* data) {
[super viewWillAppear:animated];
}
+ (instancetype) createFromPictureOptions:(CDVPictureOptions*)pictureOptions;
+ (instancetype)createFromPictureOptions:(CDVPictureOptions*)pictureOptions
{
CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
cameraPicker.pictureOptions = pictureOptions;

View File

@@ -1,80 +0,0 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import <Quartz/Quartz.h>
#import <AppKit/AppKit.h>
#import <Cordova/CDVPlugin.h>
enum CDVDestinationType {
DestinationTypeDataUrl = 0,
DestinationTypeFileUri
};
typedef NSUInteger CDVDestinationType;
enum CDVSourceType {
SourceTypePhotoLibrary = 0,
SourceTypeCamera,
SourceTypePhotoAlbum
};
typedef NSUInteger CDVSourceType;
enum CDVEncodingType {
EncodingTypeJPEG = 0,
EncodingTypePNG
};
typedef NSUInteger CDVEncodingType;
enum CDVMediaType {
MediaTypePicture = 0,
MediaTypeVideo,
MediaTypeAll
};
typedef NSUInteger CDVMediaType;
// ======================================================================= //
@interface CDVPictureOptions : NSObject
@property (strong) NSNumber *quality;
@property (assign) CDVDestinationType destinationType;
@property (assign) CDVSourceType sourceType;
@property (assign) CGSize targetSize;
@property (assign) CDVEncodingType encodingType;
@property (assign) CDVMediaType mediaType;
@property (assign) BOOL allowsEditing;
@property (assign) BOOL correctOrientation;
@property (assign) BOOL saveToPhotoAlbum;
+ (instancetype) createFromTakePictureArguments:(CDVInvokedUrlCommand *)command;
@end
// ======================================================================= //
@interface CDVCamera : CDVPlugin
- (void)takePicture:(CDVInvokedUrlCommand *)command;
- (void)cleanup:(CDVInvokedUrlCommand *)command;
@end

View File

@@ -1,258 +0,0 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import "CDVCamera.h"
@implementation CDVPictureOptions
+ (instancetype) createFromTakePictureArguments:(CDVInvokedUrlCommand*)command {
CDVPictureOptions *pictureOptions = [[CDVPictureOptions alloc] init];
pictureOptions.quality = [command argumentAtIndex:0 withDefault:@(50)];
pictureOptions.destinationType = [[command argumentAtIndex:1 withDefault:@(DestinationTypeFileUri)] unsignedIntegerValue];
pictureOptions.sourceType = [[command argumentAtIndex:2 withDefault:@(SourceTypeCamera)] unsignedIntegerValue];
NSNumber *targetWidth = [command argumentAtIndex:3 withDefault:nil];
NSNumber *targetHeight = [command argumentAtIndex:4 withDefault:nil];
pictureOptions.targetSize = CGSizeMake(0, 0);
if ((targetWidth != nil) && (targetHeight != nil)) {
pictureOptions.targetSize = CGSizeMake([targetWidth floatValue], [targetHeight floatValue]);
}
pictureOptions.encodingType = [[command argumentAtIndex:5 withDefault:@(EncodingTypeJPEG)] unsignedIntegerValue];
pictureOptions.mediaType = [[command argumentAtIndex:6 withDefault:@(MediaTypePicture)] unsignedIntegerValue];
pictureOptions.allowsEditing = [[command argumentAtIndex:7 withDefault:@(NO)] boolValue];
pictureOptions.correctOrientation = [[command argumentAtIndex:8 withDefault:@(NO)] boolValue];
pictureOptions.saveToPhotoAlbum = [[command argumentAtIndex:9 withDefault:@(NO)] boolValue];
return pictureOptions;
}
@end
// ======================================================================= //
@implementation CDVCamera
/*!
Static array that stores the temporary created files allowing to delete them when calling navigator.camera.cleanup(...)
*/
static NSMutableArray *cleanUpFiles;
+ (void)initialize {
cleanUpFiles = [NSMutableArray array];
}
- (void)takePicture:(CDVInvokedUrlCommand *)command {
CDVPictureOptions *pictureOptions = [CDVPictureOptions createFromTakePictureArguments:command];
if (pictureOptions.sourceType == SourceTypeCamera) {
[self takePictureFromCamera:command withOptions:pictureOptions];
} else {
[self takePictureFromFile:command withOptions:pictureOptions];
}
}
- (void)cleanup:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
if (cleanUpFiles.count > 0) {
for (int i=0; i<cleanUpFiles.count; i++) {
NSString *path = [cleanUpFiles objectAtIndex:i];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
[cleanUpFiles removeAllObjects];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
}];
}
#pragma mark - Camera
/*!
Takes a picture from the iSight camera using the default OS dialog.
@see https://developer.apple.com/documentation/quartz/ikpicturetaker
*/
- (void)takePictureFromCamera:(CDVInvokedUrlCommand *)command withOptions:(CDVPictureOptions *)pictureOptions {
IKPictureTaker *pictureTaker = [IKPictureTaker pictureTaker];
[pictureTaker setValue:[NSNumber numberWithBool:YES] forKey:IKPictureTakerAllowsVideoCaptureKey];
[pictureTaker setValue:[NSNumber numberWithBool:NO] forKey:IKPictureTakerAllowsFileChoosingKey];
[pictureTaker setValue:[NSNumber numberWithBool:pictureOptions.allowsEditing] forKey:IKPictureTakerShowEffectsKey];
[pictureTaker setValue:[NSNumber numberWithBool:pictureOptions.allowsEditing] forKey:IKPictureTakerAllowsEditingKey];
NSDictionary *contextInfo = @{ @"command": command, @"pictureOptions" : pictureOptions};
[pictureTaker beginPictureTakerSheetForWindow:self.viewController.contentView.window withDelegate:self didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) contextInfo:(void *)CFBridgingRetain(contextInfo)];
}
- (void)pictureTakerDidEnd:(IKPictureTaker *)pictureTaker returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
if (returnCode == NSOKButton) {
NSDictionary *contextInfoDictionary = (NSDictionary *)CFBridgingRelease(contextInfo);
CDVInvokedUrlCommand *command = [contextInfoDictionary valueForKey:@"command"];
CDVPictureOptions *pictureOptions = [contextInfoDictionary valueForKey:@"pictureOptions"];
[self returnImage:pictureTaker.outputImage command:command options:pictureOptions];
}
}
#pragma mark - File
/*!
Allows to select an image or video using the system native dialog.
*/
- (void)takePictureFromFile:(CDVInvokedUrlCommand *)command withOptions:(CDVPictureOptions *)pictureOptions {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
openPanel.canChooseFiles = YES;
openPanel.canChooseDirectories = NO;
openPanel.canCreateDirectories = YES;
openPanel.allowsMultipleSelection = NO;
NSMutableArray *allowedTypes = [NSMutableArray array];
if (pictureOptions.mediaType == MediaTypePicture || pictureOptions.mediaType == MediaTypeAll) {
[allowedTypes addObjectsFromArray:[NSImage imageTypes]];
}
if (pictureOptions.mediaType == MediaTypeVideo || pictureOptions.mediaType == MediaTypeAll) {
[allowedTypes addObjectsFromArray:@[(NSString *)kUTTypeMovie]];
}
[openPanel setAllowedFileTypes:allowedTypes];
[openPanel beginSheetModalForWindow:self.viewController.contentView.window completionHandler:^(NSInteger result) {
if (result == NSOKButton) {
NSURL *fileURL = [openPanel.URLs objectAtIndex:0];
if ([self fileIsImage:fileURL]) {
NSImage *image = [[NSImage alloc] initWithContentsOfFile:fileURL.path];
[self returnImage:image command:command options:pictureOptions];
} else {
if (pictureOptions.destinationType == DestinationTypeDataUrl) {
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Camera.DestinationType.DATA_URL is only available with image files"];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} else {
[self returnUri:fileURL.path command:command options:pictureOptions];
}
}
}
}];
}
#pragma mark - Common
/*!
Returns to JavaScript a URI.
Called when Camera.DestinationType.FILE_URI.
*/
- (void)returnUri:(NSString *)path command:(CDVInvokedUrlCommand *)command options:(CDVPictureOptions *)pictureOptions {
NSString *protocol = (pictureOptions.destinationType == DestinationTypeFileUri) ? @"file://" : @"";
NSString *uri = [NSString stringWithFormat:@"%@%@", protocol, path];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:uri];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
/*!
Returns to JavaScript a base64 encoded image.
Called when Camera.DestinationType.DATA_URL.
*/
- (void)returnImage:(NSImage *)image command:(CDVInvokedUrlCommand *)command options:(CDVPictureOptions *)pictureOptions {
[self.commandDelegate runInBackground:^{
NSData *processedImageData = [self processImage:image options:pictureOptions];
if (pictureOptions.destinationType == DestinationTypeDataUrl) {
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[processedImageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} else {
NSString *tempFilePath = [self uniqueImageName:pictureOptions];
[processedImageData writeToFile:tempFilePath atomically:YES];
[cleanUpFiles addObject:tempFilePath];
[self returnUri:tempFilePath command:command options:pictureOptions];
}
}];
}
/*!
Top level method to apply the size and quality required changes to an image.
*/
- (NSData *)processImage:(NSImage *)image options:(CDVPictureOptions *)pictureOptions {
NSImage *sourceImage = image;
if (pictureOptions.targetSize.width > 0 && pictureOptions.targetSize.height > 0) {
sourceImage = [self resizeImage:sourceImage toSize:pictureOptions.targetSize];
}
CGImageRef cgRef = [sourceImage CGImageForProposedRect:NULL context:nil hints:nil];
NSBitmapImageRep *imageRepresentation = [[NSBitmapImageRep alloc] initWithCGImage:cgRef];
NSData *data = (pictureOptions.encodingType == EncodingTypeJPEG)
? [imageRepresentation representationUsingType:NSJPEGFileType properties:@{NSImageCompressionFactor: [NSNumber numberWithFloat:pictureOptions.quality.floatValue/100.f]}]
: [imageRepresentation representationUsingType:NSPNGFileType properties:@{NSImageCompressionFactor: @1.0}];
return data;
}
/*!
Auxiliar method to resize an image.
*/
- (NSImage *)resizeImage:(NSImage *)image toSize:(CGSize)newSize {
CGFloat aspectWidth = newSize.width / image.size.width;
CGFloat aspectHeight = newSize.height / image.size.height;
CGFloat aspectRatio = MIN(aspectWidth, aspectHeight);
CGSize scaledSize = NSMakeSize(image.size.width*aspectRatio, image.size.height*aspectRatio);
NSImage *smallImage = [[NSImage alloc] initWithSize: scaledSize];
[smallImage lockFocus];
[image setSize: scaledSize];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[image drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, scaledSize.width, scaledSize.height) operation:NSCompositeCopy fraction:1.0];
[smallImage unlockFocus];
return smallImage;
}
/*!
Auxiliar method to know if a given file is an image or not.
*/
- (BOOL)fileIsImage:(NSURL *)fileURL {
NSString *type;
BOOL isImage = NO;
if ([fileURL getResourceValue:&type forKey:NSURLTypeIdentifierKey error:nil]) {
isImage = [[NSImage imageTypes] containsObject:type];
}
return isImage;
}
/*!
Auxiliar method that generates an unique filename for an image in the temporary directory.
*/
- (NSString *)uniqueImageName:(CDVPictureOptions *)pictureOptions {
NSString *tempDir = NSTemporaryDirectory();
NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString] ;
NSString *extension = (pictureOptions.encodingType == EncodingTypeJPEG) ? @"jpeg" : @"png";
NSString *uniqueFileName = [NSString stringWithFormat:@"%@%@.%@", tempDir, guid, extension];
return uniqueFileName;
}
@end

View File

@@ -1,861 +0,0 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/* global Windows:true, URL:true, module:true, require:true, WinJS:true */
var Camera = require('./Camera');
var getAppData = function () {
return Windows.Storage.ApplicationData.current;
};
var encodeToBase64String = function (buffer) {
return Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
};
var OptUnique = Windows.Storage.CreationCollisionOption.generateUniqueName;
var CapMSType = Windows.Media.Capture.MediaStreamType;
var webUIApp = Windows.UI.WebUI.WebUIApplication;
var fileIO = Windows.Storage.FileIO;
var pickerLocId = Windows.Storage.Pickers.PickerLocationId;
module.exports = {
// args will contain :
// ... it is an array, so be careful
// 0 quality:50,
// 1 destinationType:Camera.DestinationType.FILE_URI,
// 2 sourceType:Camera.PictureSourceType.CAMERA,
// 3 targetWidth:-1,
// 4 targetHeight:-1,
// 5 encodingType:Camera.EncodingType.JPEG,
// 6 mediaType:Camera.MediaType.PICTURE,
// 7 allowEdit:false,
// 8 correctOrientation:false,
// 9 saveToPhotoAlbum:false,
// 10 popoverOptions:null
// 11 cameraDirection:0
takePicture: function (successCallback, errorCallback, args) {
var sourceType = args[2];
if (sourceType !== Camera.PictureSourceType.CAMERA) {
takePictureFromFile(successCallback, errorCallback, args);
} else {
takePictureFromCamera(successCallback, errorCallback, args);
}
}
};
// https://msdn.microsoft.com/en-us/library/windows/apps/ff462087(v=vs.105).aspx
var windowsVideoContainers = ['.avi', '.flv', '.asx', '.asf', '.mov', '.mp4', '.mpg', '.rm', '.srt', '.swf', '.wmv', '.vob'];
var windowsPhoneVideoContainers = ['.avi', '.3gp', '.3g2', '.wmv', '.3gp', '.3g2', '.mp4', '.m4v'];
// Default aspect ratio 1.78 (16:9 hd video standard)
var DEFAULT_ASPECT_RATIO = '1.8';
// Highest possible z-index supported across browsers. Anything used above is converted to this value.
var HIGHEST_POSSIBLE_Z_INDEX = 2147483647;
// Resize method
function resizeImage (successCallback, errorCallback, file, targetWidth, targetHeight, encodingType) {
var tempPhotoFileName = '';
var targetContentType = '';
if (encodingType === Camera.EncodingType.PNG) {
tempPhotoFileName = 'camera_cordova_temp_return.png';
targetContentType = 'image/png';
} else {
tempPhotoFileName = 'camera_cordova_temp_return.jpg';
targetContentType = 'image/jpeg';
}
var storageFolder = getAppData().localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting)
.then(function (storageFile) {
return fileIO.readBufferAsync(storageFile);
})
.then(function (buffer) {
var strBase64 = encodeToBase64String(buffer);
var imageData = 'data:' + file.contentType + ';base64,' + strBase64;
var image = new Image(); /* eslint no-undef : 0 */
image.src = imageData;
image.onload = function () {
var ratio = Math.min(targetWidth / this.width, targetHeight / this.height);
var imageWidth = ratio * this.width;
var imageHeight = ratio * this.height;
var canvas = document.createElement('canvas');
var storageFileName;
canvas.width = imageWidth;
canvas.height = imageHeight;
canvas.getContext('2d').drawImage(this, 0, 0, imageWidth, imageHeight);
var fileContent = canvas.toDataURL(targetContentType).split(',')[1];
var storageFolder = getAppData().localFolder;
storageFolder.createFileAsync(tempPhotoFileName, OptUnique)
.then(function (storagefile) {
var content = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(fileContent);
storageFileName = storagefile.name;
return fileIO.writeBufferAsync(storagefile, content);
})
.done(function () {
successCallback('ms-appdata:///local/' + storageFileName);
}, errorCallback);
};
})
.done(null, function (err) {
errorCallback(err);
});
}
// Because of asynchronous method, so let the successCallback be called in it.
function resizeImageBase64 (successCallback, errorCallback, file, targetWidth, targetHeight) {
fileIO.readBufferAsync(file).done(function (buffer) {
var strBase64 = encodeToBase64String(buffer);
var imageData = 'data:' + file.contentType + ';base64,' + strBase64;
var image = new Image(); /* eslint no-undef : 0 */
image.src = imageData;
image.onload = function () {
var ratio = Math.min(targetWidth / this.width, targetHeight / this.height);
var imageWidth = ratio * this.width;
var imageHeight = ratio * this.height;
var canvas = document.createElement('canvas');
canvas.width = imageWidth;
canvas.height = imageHeight;
var ctx = canvas.getContext('2d');
ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
// The resized file ready for upload
var finalFile = canvas.toDataURL(file.contentType);
// Remove the prefix such as "data:" + contentType + ";base64," , in order to meet the Cordova API.
var arr = finalFile.split(',');
var newStr = finalFile.substr(arr[0].length + 1);
successCallback(newStr);
};
}, function (err) { errorCallback(err); });
}
function takePictureFromFile (successCallback, errorCallback, args) {
// Detect Windows Phone
if (navigator.appVersion.indexOf('Windows Phone 8.1') >= 0) {
takePictureFromFileWP(successCallback, errorCallback, args);
} else {
takePictureFromFileWindows(successCallback, errorCallback, args);
}
}
function takePictureFromFileWP (successCallback, errorCallback, args) {
var mediaType = args[6];
var destinationType = args[1];
var targetWidth = args[3];
var targetHeight = args[4];
var encodingType = args[5];
/*
Need to add and remove an event listener to catch activation state
Using FileOpenPicker will suspend the app and it's required to catch the PickSingleFileAndContinue
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx
*/
var filePickerActivationHandler = function (eventArgs) {
if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.pickFileContinuation) {
var file = eventArgs.files[0];
if (!file) {
errorCallback("User didn't choose a file.");
webUIApp.removeEventListener('activated', filePickerActivationHandler);
return;
}
if (destinationType === Camera.DestinationType.FILE_URI) {
if (targetHeight > 0 && targetWidth > 0) {
resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight, encodingType);
} else {
var storageFolder = getAppData().localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function (storageFile) {
successCallback(URL.createObjectURL(storageFile));
}, function () {
errorCallback("Can't access localStorage folder.");
});
}
} else {
if (targetHeight > 0 && targetWidth > 0) {
resizeImageBase64(successCallback, errorCallback, file, targetWidth, targetHeight);
} else {
fileIO.readBufferAsync(file).done(function (buffer) {
var strBase64 = encodeToBase64String(buffer);
successCallback(strBase64);
}, errorCallback);
}
}
webUIApp.removeEventListener('activated', filePickerActivationHandler);
}
};
var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
if (mediaType === Camera.MediaType.PICTURE) {
fileOpenPicker.fileTypeFilter.replaceAll(['.png', '.jpg', '.jpeg']);
fileOpenPicker.suggestedStartLocation = pickerLocId.picturesLibrary;
} else if (mediaType === Camera.MediaType.VIDEO) {
fileOpenPicker.fileTypeFilter.replaceAll(windowsPhoneVideoContainers);
fileOpenPicker.suggestedStartLocation = pickerLocId.videosLibrary;
} else {
fileOpenPicker.fileTypeFilter.replaceAll(['*']);
fileOpenPicker.suggestedStartLocation = pickerLocId.documentsLibrary;
}
webUIApp.addEventListener('activated', filePickerActivationHandler);
fileOpenPicker.pickSingleFileAndContinue();
}
function takePictureFromFileWindows (successCallback, errorCallback, args) {
var mediaType = args[6];
var destinationType = args[1];
var targetWidth = args[3];
var targetHeight = args[4];
var encodingType = args[5];
var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
if (mediaType === Camera.MediaType.PICTURE) {
fileOpenPicker.fileTypeFilter.replaceAll(['.png', '.jpg', '.jpeg']);
fileOpenPicker.suggestedStartLocation = pickerLocId.picturesLibrary;
} else if (mediaType === Camera.MediaType.VIDEO) {
fileOpenPicker.fileTypeFilter.replaceAll(windowsVideoContainers);
fileOpenPicker.suggestedStartLocation = pickerLocId.videosLibrary;
} else {
fileOpenPicker.fileTypeFilter.replaceAll(['*']);
fileOpenPicker.suggestedStartLocation = pickerLocId.documentsLibrary;
}
fileOpenPicker.pickSingleFileAsync().done(function (file) {
if (!file) {
errorCallback("User didn't choose a file.");
return;
}
if (destinationType === Camera.DestinationType.FILE_URI) {
if (targetHeight > 0 && targetWidth > 0) {
resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight, encodingType);
} else {
var storageFolder = getAppData().localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function (storageFile) {
successCallback(URL.createObjectURL(storageFile));
}, function () {
errorCallback("Can't access localStorage folder.");
});
}
} else {
if (targetHeight > 0 && targetWidth > 0) {
resizeImageBase64(successCallback, errorCallback, file, targetWidth, targetHeight);
} else {
fileIO.readBufferAsync(file).done(function (buffer) {
var strBase64 = encodeToBase64String(buffer);
successCallback(strBase64);
}, errorCallback);
}
}
}, function () {
errorCallback("User didn't choose a file.");
});
}
function takePictureFromCamera (successCallback, errorCallback, args) {
// Check if necessary API available
if (!Windows.Media.Capture.CameraCaptureUI) {
takePictureFromCameraWP(successCallback, errorCallback, args);
} else {
takePictureFromCameraWindows(successCallback, errorCallback, args);
}
}
function takePictureFromCameraWP (successCallback, errorCallback, args) {
// We are running on WP8.1 which lacks CameraCaptureUI class
// so we need to use MediaCapture class instead and implement custom UI for camera
var destinationType = args[1];
var targetWidth = args[3];
var targetHeight = args[4];
var encodingType = args[5];
var saveToPhotoAlbum = args[9];
var cameraDirection = args[11];
var capturePreview = null;
var cameraCaptureButton = null;
var cameraCancelButton = null;
var capture = null;
var captureSettings = null;
var CaptureNS = Windows.Media.Capture;
var sensor = null;
function createCameraUI () {
// create style for take and cancel buttons
var buttonStyle = 'width:45%;padding: 10px 16px;font-size: 18px;line-height: 1.3333333;color: #333;background-color: #fff;border-color: #ccc; border: 1px solid transparent;border-radius: 6px; display: block; margin: 20px; z-index: 1000;border-color: #adadad;';
// Create fullscreen preview
// z-order style element for capturePreview and cameraCancelButton elts
// is necessary to avoid overriding by another page elements, -1 sometimes is not enough
capturePreview = document.createElement('video');
capturePreview.style.cssText = 'position: fixed; left: 0; top: 0; width: 100%; height: 100%; z-index: ' + (HIGHEST_POSSIBLE_Z_INDEX - 1) + ';';
// Create capture button
cameraCaptureButton = document.createElement('button');
cameraCaptureButton.innerText = 'Take';
cameraCaptureButton.style.cssText = buttonStyle + 'position: fixed; left: 0; bottom: 0; margin: 20px; z-index: ' + HIGHEST_POSSIBLE_Z_INDEX + ';';
// Create cancel button
cameraCancelButton = document.createElement('button');
cameraCancelButton.innerText = 'Cancel';
cameraCancelButton.style.cssText = buttonStyle + 'position: fixed; right: 0; bottom: 0; margin: 20px; z-index: ' + HIGHEST_POSSIBLE_Z_INDEX + ';';
capture = new CaptureNS.MediaCapture();
captureSettings = new CaptureNS.MediaCaptureInitializationSettings();
captureSettings.streamingCaptureMode = CaptureNS.StreamingCaptureMode.video;
}
function continueVideoOnFocus () {
// if preview is defined it would be stuck, play it
if (capturePreview) {
capturePreview.play();
}
}
function startCameraPreview () {
// Search for available camera devices
// This is necessary to detect which camera (front or back) we should use
var DeviceEnum = Windows.Devices.Enumeration;
var expectedPanel = cameraDirection === 1 ? DeviceEnum.Panel.front : DeviceEnum.Panel.back;
// Add focus event handler to capture the event when user suspends the app and comes back while the preview is on
window.addEventListener('focus', continueVideoOnFocus);
DeviceEnum.DeviceInformation.findAllAsync(DeviceEnum.DeviceClass.videoCapture).then(function (devices) {
if (devices.length <= 0) {
destroyCameraPreview();
errorCallback('Camera not found');
return;
}
devices.forEach(function (currDev) {
if (currDev.enclosureLocation.panel && currDev.enclosureLocation.panel === expectedPanel) {
captureSettings.videoDeviceId = currDev.id;
}
});
captureSettings.photoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.photo;
return capture.initializeAsync(captureSettings);
}).then(function () {
// create focus control if available
var VideoDeviceController = capture.videoDeviceController;
var FocusControl = VideoDeviceController.focusControl;
if (FocusControl.supported === true) {
capturePreview.addEventListener('click', function () {
// Make sure function isn't called again before previous focus is completed
if (this.getAttribute('clicked') === '1') {
return false;
} else {
this.setAttribute('clicked', '1');
}
var preset = Windows.Media.Devices.FocusPreset.autoNormal;
var parent = this;
FocusControl.setPresetAsync(preset).done(function () {
// set the clicked attribute back to '0' to allow focus again
parent.setAttribute('clicked', '0');
});
});
}
// msdn.microsoft.com/en-us/library/windows/apps/hh452807.aspx
capturePreview.msZoom = true;
capturePreview.src = URL.createObjectURL(capture);
capturePreview.play();
// Bind events to controls
sensor = Windows.Devices.Sensors.SimpleOrientationSensor.getDefault();
if (sensor !== null) {
sensor.addEventListener('orientationchanged', onOrientationChange);
}
// add click events to capture and cancel buttons
cameraCaptureButton.addEventListener('click', onCameraCaptureButtonClick);
cameraCancelButton.addEventListener('click', onCameraCancelButtonClick);
// Change default orientation
if (sensor) {
setPreviewRotation(sensor.getCurrentOrientation());
} else {
setPreviewRotation(Windows.Graphics.Display.DisplayInformation.getForCurrentView().currentOrientation);
}
// Get available aspect ratios
var aspectRatios = getAspectRatios(capture);
// Couldn't find a good ratio
if (aspectRatios.length === 0) {
destroyCameraPreview();
errorCallback('There\'s not a good aspect ratio available');
return;
}
// add elements to body
document.body.appendChild(capturePreview);
document.body.appendChild(cameraCaptureButton);
document.body.appendChild(cameraCancelButton);
if (aspectRatios.indexOf(DEFAULT_ASPECT_RATIO) > -1) {
return setAspectRatio(capture, DEFAULT_ASPECT_RATIO);
} else {
// Doesn't support 16:9 - pick next best
return setAspectRatio(capture, aspectRatios[0]);
}
}).done(null, function (err) {
destroyCameraPreview();
errorCallback('Camera intitialization error ' + err);
});
}
function destroyCameraPreview () {
// If sensor is available, remove event listener
if (sensor !== null) {
sensor.removeEventListener('orientationchanged', onOrientationChange);
}
// Pause and dispose preview element
capturePreview.pause();
capturePreview.src = null;
// Remove event listeners from buttons
cameraCaptureButton.removeEventListener('click', onCameraCaptureButtonClick);
cameraCancelButton.removeEventListener('click', onCameraCancelButtonClick);
// Remove the focus event handler
window.removeEventListener('focus', continueVideoOnFocus);
// Remove elements
[capturePreview, cameraCaptureButton, cameraCancelButton].forEach(function (elem) {
if (elem /* && elem in document.body.childNodes */) {
document.body.removeChild(elem);
}
});
// Stop and dispose media capture manager
if (capture) {
capture.stopRecordAsync();
capture = null;
}
}
function captureAction () {
var encodingProperties;
var fileName;
var tempFolder = getAppData().temporaryFolder;
if (encodingType === Camera.EncodingType.PNG) {
fileName = 'photo.png';
encodingProperties = Windows.Media.MediaProperties.ImageEncodingProperties.createPng();
} else {
fileName = 'photo.jpg';
encodingProperties = Windows.Media.MediaProperties.ImageEncodingProperties.createJpeg();
}
tempFolder.createFileAsync(fileName, OptUnique)
.then(function (tempCapturedFile) {
return new WinJS.Promise(function (complete) {
var photoStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
var finalStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
capture.capturePhotoToStreamAsync(encodingProperties, photoStream)
.then(function () {
return Windows.Graphics.Imaging.BitmapDecoder.createAsync(photoStream);
})
.then(function (dec) {
finalStream.size = 0; // BitmapEncoder requires the output stream to be empty
return Windows.Graphics.Imaging.BitmapEncoder.createForTranscodingAsync(finalStream, dec);
})
.then(function (enc) {
// We need to rotate the photo wrt sensor orientation
enc.bitmapTransform.rotation = orientationToRotation(sensor.getCurrentOrientation());
return enc.flushAsync();
})
.then(function () {
return tempCapturedFile.openAsync(Windows.Storage.FileAccessMode.readWrite);
})
.then(function (fileStream) {
return Windows.Storage.Streams.RandomAccessStream.copyAndCloseAsync(finalStream, fileStream);
})
.done(function () {
photoStream.close();
finalStream.close();
complete(tempCapturedFile);
}, function () {
photoStream.close();
finalStream.close();
throw new Error('An error has occured while capturing the photo.');
});
});
})
.done(function (capturedFile) {
destroyCameraPreview();
savePhoto(capturedFile, {
destinationType: destinationType,
targetHeight: targetHeight,
targetWidth: targetWidth,
encodingType: encodingType,
saveToPhotoAlbum: saveToPhotoAlbum
}, successCallback, errorCallback);
}, function (err) {
destroyCameraPreview();
errorCallback(err);
});
}
function getAspectRatios (capture) {
var videoDeviceController = capture.videoDeviceController;
var photoAspectRatios = videoDeviceController.getAvailableMediaStreamProperties(CapMSType.photo).map(function (element) {
return (element.width / element.height).toFixed(1);
}).filter(function (element, index, array) { return (index === array.indexOf(element)); });
var videoAspectRatios = videoDeviceController.getAvailableMediaStreamProperties(CapMSType.videoRecord).map(function (element) {
return (element.width / element.height).toFixed(1);
}).filter(function (element, index, array) { return (index === array.indexOf(element)); });
var videoPreviewAspectRatios = videoDeviceController.getAvailableMediaStreamProperties(CapMSType.videoPreview).map(function (element) {
return (element.width / element.height).toFixed(1);
}).filter(function (element, index, array) { return (index === array.indexOf(element)); });
var allAspectRatios = [].concat(photoAspectRatios, videoAspectRatios, videoPreviewAspectRatios);
var aspectObj = allAspectRatios.reduce(function (map, item) {
if (!map[item]) {
map[item] = 0;
}
map[item]++;
return map;
}, {});
return Object.keys(aspectObj).filter(function (k) {
return aspectObj[k] === 3;
});
}
function setAspectRatio (capture, aspect) {
// Max photo resolution with desired aspect ratio
var videoDeviceController = capture.videoDeviceController;
var photoResolution = videoDeviceController.getAvailableMediaStreamProperties(CapMSType.photo)
.filter(function (elem) {
return ((elem.width / elem.height).toFixed(1) === aspect);
})
.reduce(function (prop1, prop2) {
return (prop1.width * prop1.height) > (prop2.width * prop2.height) ? prop1 : prop2;
});
// Max video resolution with desired aspect ratio
var videoRecordResolution = videoDeviceController.getAvailableMediaStreamProperties(CapMSType.videoRecord)
.filter(function (elem) {
return ((elem.width / elem.height).toFixed(1) === aspect);
})
.reduce(function (prop1, prop2) {
return (prop1.width * prop1.height) > (prop2.width * prop2.height) ? prop1 : prop2;
});
// Max video preview resolution with desired aspect ratio
var videoPreviewResolution = videoDeviceController.getAvailableMediaStreamProperties(CapMSType.videoPreview)
.filter(function (elem) {
return ((elem.width / elem.height).toFixed(1) === aspect);
})
.reduce(function (prop1, prop2) {
return (prop1.width * prop1.height) > (prop2.width * prop2.height) ? prop1 : prop2;
});
return videoDeviceController.setMediaStreamPropertiesAsync(CapMSType.photo, photoResolution)
.then(function () {
return videoDeviceController.setMediaStreamPropertiesAsync(CapMSType.videoPreview, videoPreviewResolution);
})
.then(function () {
return videoDeviceController.setMediaStreamPropertiesAsync(CapMSType.videoRecord, videoRecordResolution);
});
}
/**
* When Capture button is clicked, try to capture a picture and return
*/
function onCameraCaptureButtonClick () {
// Make sure user can't click more than once
if (this.getAttribute('clicked') === '1') {
return false;
} else {
this.setAttribute('clicked', '1');
}
captureAction();
}
/**
* When Cancel button is clicked, destroy camera preview and return with error callback
*/
function onCameraCancelButtonClick () {
// Make sure user can't click more than once
if (this.getAttribute('clicked') === '1') {
return false;
} else {
this.setAttribute('clicked', '1');
}
destroyCameraPreview();
errorCallback('no image selected');
}
/**
* When the phone orientation change, get the event and change camera preview rotation
* @param {Object} e - SimpleOrientationSensorOrientationChangedEventArgs
*/
function onOrientationChange (e) {
setPreviewRotation(e.orientation);
}
/**
* Converts SimpleOrientation to a VideoRotation to remove difference between camera sensor orientation
* and video orientation
* @param {number} orientation - Windows.Devices.Sensors.SimpleOrientation
* @return {number} - Windows.Media.Capture.VideoRotation
*/
function orientationToRotation (orientation) {
// VideoRotation enumerable and BitmapRotation enumerable have the same values
// https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.capture.videorotation.aspx
// https://msdn.microsoft.com/en-us/library/windows/apps/windows.graphics.imaging.bitmaprotation.aspx
switch (orientation) {
// portrait
case Windows.Devices.Sensors.SimpleOrientation.notRotated:
return Windows.Media.Capture.VideoRotation.clockwise90Degrees;
// landscape
case Windows.Devices.Sensors.SimpleOrientation.rotated90DegreesCounterclockwise:
return Windows.Media.Capture.VideoRotation.none;
// portrait-flipped (not supported by WinPhone Apps)
case Windows.Devices.Sensors.SimpleOrientation.rotated180DegreesCounterclockwise:
// Falling back to portrait default
return Windows.Media.Capture.VideoRotation.clockwise90Degrees;
// landscape-flipped
case Windows.Devices.Sensors.SimpleOrientation.rotated270DegreesCounterclockwise:
return Windows.Media.Capture.VideoRotation.clockwise180Degrees;
// faceup & facedown
default:
// Falling back to portrait default
return Windows.Media.Capture.VideoRotation.clockwise90Degrees;
}
}
/**
* Rotates the current MediaCapture's video
* @param {number} orientation - Windows.Devices.Sensors.SimpleOrientation
*/
function setPreviewRotation (orientation) {
capture.setPreviewRotation(orientationToRotation(orientation));
}
try {
createCameraUI();
startCameraPreview();
} catch (ex) {
errorCallback(ex);
}
}
function takePictureFromCameraWindows (successCallback, errorCallback, args) {
var destinationType = args[1];
var targetWidth = args[3];
var targetHeight = args[4];
var encodingType = args[5];
var allowCrop = !!args[7];
var saveToPhotoAlbum = args[9];
var WMCapture = Windows.Media.Capture;
var cameraCaptureUI = new WMCapture.CameraCaptureUI();
cameraCaptureUI.photoSettings.allowCropping = allowCrop;
if (encodingType === Camera.EncodingType.PNG) {
cameraCaptureUI.photoSettings.format = WMCapture.CameraCaptureUIPhotoFormat.png;
} else {
cameraCaptureUI.photoSettings.format = WMCapture.CameraCaptureUIPhotoFormat.jpeg;
}
// decide which max pixels should be supported by targetWidth or targetHeight.
var maxRes = null;
var UIMaxRes = WMCapture.CameraCaptureUIMaxPhotoResolution;
var totalPixels = targetWidth * targetHeight;
if (targetWidth === -1 && targetHeight === -1) {
maxRes = UIMaxRes.highestAvailable;
// Temp fix for CB-10539
/* else if (totalPixels <= 320 * 240) {
maxRes = UIMaxRes.verySmallQvga;
} */
} else if (totalPixels <= 640 * 480) {
maxRes = UIMaxRes.smallVga;
} else if (totalPixels <= 1024 * 768) {
maxRes = UIMaxRes.mediumXga;
} else if (totalPixels <= 3 * 1000 * 1000) {
maxRes = UIMaxRes.large3M;
} else if (totalPixels <= 5 * 1000 * 1000) {
maxRes = UIMaxRes.veryLarge5M;
} else {
maxRes = UIMaxRes.highestAvailable;
}
cameraCaptureUI.photoSettings.maxResolution = maxRes;
var cameraPicture;
// define focus handler for windows phone 10.0
var savePhotoOnFocus = function () {
window.removeEventListener('focus', savePhotoOnFocus);
// call only when the app is in focus again
savePhoto(cameraPicture, {
destinationType: destinationType,
targetHeight: targetHeight,
targetWidth: targetWidth,
encodingType: encodingType,
saveToPhotoAlbum: saveToPhotoAlbum
}, successCallback, errorCallback);
};
// if windows phone 10, add and delete focus eventHandler to capture the focus back from cameraUI to app
if (navigator.appVersion.indexOf('Windows Phone 10.0') >= 0) {
window.addEventListener('focus', savePhotoOnFocus);
}
cameraCaptureUI.captureFileAsync(WMCapture.CameraCaptureUIMode.photo).done(function (picture) {
if (!picture) {
errorCallback("User didn't capture a photo.");
// Remove the focus handler if present
window.removeEventListener('focus', savePhotoOnFocus);
return;
}
cameraPicture = picture;
// If not windows 10, call savePhoto() now. If windows 10, wait for the app to be in focus again
if (navigator.appVersion.indexOf('Windows Phone 10.0') < 0) {
savePhoto(cameraPicture, {
destinationType: destinationType,
targetHeight: targetHeight,
targetWidth: targetWidth,
encodingType: encodingType,
saveToPhotoAlbum: saveToPhotoAlbum
}, successCallback, errorCallback);
}
}, function () {
errorCallback('Fail to capture a photo.');
window.removeEventListener('focus', savePhotoOnFocus);
});
}
function savePhoto (picture, options, successCallback, errorCallback) {
// success callback for capture operation
var success = function (picture) {
if (options.destinationType === Camera.DestinationType.FILE_URI) {
if (options.targetHeight > 0 && options.targetWidth > 0) {
resizeImage(successCallback, errorCallback, picture, options.targetWidth, options.targetHeight, options.encodingType);
} else {
// CB-11714: check if target content-type is PNG to just rename as *.jpg since camera is captured as JPEG
if (options.encodingType === Camera.EncodingType.PNG) {
picture.name = picture.name.replace(/\.png$/, '.jpg');
}
picture.copyAsync(getAppData().localFolder, picture.name, OptUnique).done(function (copiedFile) {
successCallback('ms-appdata:///local/' + copiedFile.name);
}, errorCallback);
}
} else {
if (options.targetHeight > 0 && options.targetWidth > 0) {
resizeImageBase64(successCallback, errorCallback, picture, options.targetWidth, options.targetHeight);
} else {
fileIO.readBufferAsync(picture).done(function (buffer) {
var strBase64 = encodeToBase64String(buffer);
picture.deleteAsync().done(function () {
successCallback(strBase64);
}, function (err) {
errorCallback(err);
});
}, errorCallback);
}
}
};
if (!options.saveToPhotoAlbum) {
success(picture);
} else {
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
var saveFile = function (file) {
if (file) {
// Prevent updates to the remote version of the file until we're done
Windows.Storage.CachedFileManager.deferUpdates(file);
picture.moveAndReplaceAsync(file)
.then(function () {
// Let Windows know that we're finished changing the file so
// the other app can update the remote version of the file.
return Windows.Storage.CachedFileManager.completeUpdatesAsync(file);
})
.done(function (updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
success(picture);
} else {
errorCallback('File update status is not complete.');
}
}, errorCallback);
} else {
errorCallback('Failed to select a file.');
}
};
savePicker.suggestedStartLocation = pickerLocId.picturesLibrary;
if (options.encodingType === Camera.EncodingType.PNG) {
savePicker.fileTypeChoices.insert('PNG', ['.png']);
savePicker.suggestedFileName = 'photo.png';
} else {
savePicker.fileTypeChoices.insert('JPEG', ['.jpg']);
savePicker.suggestedFileName = 'photo.jpg';
}
// If Windows Phone 8.1 use pickSaveFileAndContinue()
if (navigator.appVersion.indexOf('Windows Phone 8.1') >= 0) {
/*
Need to add and remove an event listener to catch activation state
Using FileSavePicker will suspend the app and it's required to catch the pickSaveFileContinuation
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx
*/
var fileSaveHandler = function (eventArgs) {
if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.pickSaveFileContinuation) {
var file = eventArgs.file;
saveFile(file);
webUIApp.removeEventListener('activated', fileSaveHandler);
}
};
webUIApp.addEventListener('activated', fileSaveHandler);
savePicker.pickSaveFileAndContinue();
} else {
savePicker.pickSaveFileAsync()
.done(saveFile, errorCallback);
}
}
}
require('cordova/exec/proxy').add('Camera', module.exports);

View File

@@ -37,28 +37,30 @@
- (UIImage*)retrieveImage:(NSDictionary*)info options:(CDVPictureOptions*)options;
- (CDVPluginResult*)resultForImage:(CDVPictureOptions*)options info:(NSDictionary*)info;
- (CDVPluginResult*)resultForVideo:(NSDictionary*)info;
- (NSDictionary*)convertImageMetadata:(NSData*)imageData;
@end
@implementation CameraTest
- (void)setUp {
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
self.plugin = [[CDVCamera alloc] init];
}
- (void)tearDown {
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void) testPictureOptionsCreate
- (void)testPictureOptionsCreate
{
NSArray* args;
CDVPictureOptions* options;
NSDictionary* popoverOptions;
// No arguments, check whether the defaults are set
args = @[];
@@ -76,14 +78,10 @@
XCTAssertEqual(options.allowsEditing, NO);
XCTAssertEqual(options.correctOrientation, NO);
XCTAssertEqual(options.saveToPhotoAlbum, NO);
XCTAssertEqualObjects(options.popoverOptions, nil);
XCTAssertEqual(options.cameraDirection, (int)UIImagePickerControllerCameraDeviceRear);
XCTAssertEqual(options.popoverSupported, NO);
XCTAssertEqual(options.usesGeolocation, NO);
// Set each argument, check whether they are set. different from defaults
popoverOptions = @{ @"x" : @1, @"y" : @2, @"width" : @3, @"height" : @4, @"popoverWidth": @200, @"popoverHeight": @300 };
args = @[
@(49),
@(DestinationTypeDataUrl),
@@ -95,7 +93,6 @@
@YES,
@YES,
@YES,
popoverOptions,
@(UIImagePickerControllerCameraDeviceFront),
];
@@ -112,22 +109,17 @@
XCTAssertEqual(options.allowsEditing, YES);
XCTAssertEqual(options.correctOrientation, YES);
XCTAssertEqual(options.saveToPhotoAlbum, YES);
XCTAssertEqualObjects(options.popoverOptions, popoverOptions);
XCTAssertEqual(options.cameraDirection, (int)UIImagePickerControllerCameraDeviceFront);
XCTAssertEqual(options.popoverSupported, NO);
XCTAssertEqual(options.usesGeolocation, NO);
}
- (void) testCameraPickerCreate
- (void)testCameraPickerCreate
{
NSDictionary* popoverOptions;
NSArray* args;
CDVPictureOptions* pictureOptions;
CDVCameraPicker* picker;
// Souce is Camera, and image type
popoverOptions = @{ @"x" : @1, @"y" : @2, @"width" : @3, @"height" : @4, @"popoverWidth": @200, @"popoverHeight": @300 };
// Source is Camera, uses always UIImagePickerController
args = @[
@(49),
@(DestinationTypeDataUrl),
@@ -139,7 +131,6 @@
@YES,
@YES,
@YES,
popoverOptions,
@(UIImagePickerControllerCameraDeviceFront),
];
@@ -157,7 +148,7 @@
XCTAssertEqual(picker.cameraDevice, pictureOptions.cameraDirection);
}
// Souce is not Camera, and all media types
// Source is Photo Library, and all media types - On iOS 14+ uses PHPicker, below uses UIImagePickerController
args = @[
@(49),
@@ -170,7 +161,6 @@
@YES,
@YES,
@YES,
popoverOptions,
@(UIImagePickerControllerCameraDeviceFront),
];
@@ -187,7 +177,7 @@
XCTAssertEqualObjects(picker.mediaTypes, [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType]);
}
// Souce is not Camera, and either Image or Movie media type
// Source is Photo Library, and either Image or Movie media type - On iOS 14+ uses PHPicker
args = @[
@(49),
@@ -200,7 +190,6 @@
@YES,
@YES,
@YES,
popoverOptions,
@(UIImagePickerControllerCameraDeviceFront),
];
@@ -218,7 +207,8 @@
}
}
- (UIImage*) createImage:(CGRect)rect orientation:(UIImageOrientation)imageOrientation {
- (UIImage*)createImage:(CGRect)rect orientation:(UIImageOrientation)imageOrientation
{
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
@@ -233,8 +223,8 @@
return image;
}
- (void) testImageScaleCropForSize {
- (void)testImageScaleCropForSize
{
UIImage *sourceImagePortrait, *sourceImageLandscape, *targetImage;
CGSize targetSize = CGSizeZero;
@@ -279,7 +269,8 @@
XCTAssertEqual(targetImage.size.height, targetSize.height);
}
- (void) testImageScaleNoCropForSize {
- (void)testImageScaleNoCropForSize
{
UIImage *sourceImagePortrait, *sourceImageLandscape, *targetImage;
CGSize targetSize = CGSizeZero;
@@ -330,7 +321,8 @@
XCTAssertEqual(targetImage.size.height, targetSize.height);
}
- (void) testImageCorrectedForOrientation {
- (void)testImageCorrectedForOrientation
{
UIImage *sourceImagePortrait, *sourceImageLandscape, *targetImage;
CGSize targetSize = CGSizeZero;
@@ -383,7 +375,7 @@
}
- (void) testRetrieveImage
- (void)testRetrieveImage
{
CDVPictureOptions* pictureOptions = [[CDVPictureOptions alloc] init];
NSDictionary *infoDict1, *infoDict2;
@@ -461,7 +453,7 @@
XCTAssertEqual(resultImage.size.height, scaledImageWithCrop.size.height);
}
- (void) testProcessImage
- (void)testProcessImage
{
CDVPictureOptions* pictureOptions = [[CDVPictureOptions alloc] init];
NSData* resultData;
@@ -508,4 +500,123 @@
// TODO: usesGeolocation is not tested
}
#pragma mark - PHPickerViewController Tests (iOS 14+)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 // Always true on XCode12+
- (void)testPHPickerAvailability API_AVAILABLE(ios(14))
{
XCTAssertNotNil([PHPickerViewController class]);
PHPickerConfiguration *config = [[PHPickerConfiguration alloc] init];
XCTAssertNotNil(config);
config.filter = [PHPickerFilter imagesFilter];
XCTAssertNotNil(config.filter);
PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:config];
XCTAssertNotNil(picker);
}
- (void)testPHPickerConfiguration API_AVAILABLE(ios(14))
{
// Test image filter configuration
PHPickerConfiguration *imageConfig = [[PHPickerConfiguration alloc] init];
imageConfig.filter = [PHPickerFilter imagesFilter];
imageConfig.selectionLimit = 1;
XCTAssertNotNil(imageConfig);
XCTAssertEqual(imageConfig.selectionLimit, 1);
// Test video filter configuration
PHPickerConfiguration *videoConfig = [[PHPickerConfiguration alloc] init];
videoConfig.filter = [PHPickerFilter videosFilter];
XCTAssertNotNil(videoConfig.filter);
// Test all media types configuration
PHPickerConfiguration *allConfig = [[PHPickerConfiguration alloc] init];
allConfig.filter = [PHPickerFilter anyFilterMatchingSubfilters:@[
[PHPickerFilter imagesFilter],
[PHPickerFilter videosFilter]
]];
XCTAssertNotNil(allConfig.filter);
}
- (void)testPHPickerDelegateConformance API_AVAILABLE(ios(14))
{
// Test that CDVCamera conforms to PHPickerViewControllerDelegate
XCTAssertTrue([self.plugin conformsToProtocol:@protocol(PHPickerViewControllerDelegate)]);
// Test that the delegate method is implemented
SEL delegateSelector = @selector(picker:didFinishPicking:);
XCTAssertTrue([self.plugin respondsToSelector:delegateSelector]);
}
- (void)testShowPHPickerMethod API_AVAILABLE(ios(14))
{
// Test that showPHPicker method exists
SEL showPHPickerSelector = @selector(showPHPicker:withOptions:);
XCTAssertTrue([self.plugin respondsToSelector:showPHPickerSelector]);
// Test that processPHPickerImage method exists
SEL processSelector = @selector(processPHPickerImage:assetIdentifier:callbackId:options:);
XCTAssertTrue([self.plugin respondsToSelector:processSelector]);
// Test that finalizePHPickerImage method exists
SEL finalizeSelector = @selector(finalizePHPickerImage:metadata:callbackId:options:);
XCTAssertTrue([self.plugin respondsToSelector:finalizeSelector]);
}
#endif
- (void)testConvertImageMetadata
{
// Create a test image
UIImage* testImage = [self createImage:CGRectMake(0, 0, 100, 100) orientation:UIImageOrientationUp];
NSData* imageData = UIImageJPEGRepresentation(testImage, 1.0);
XCTAssertNotNil(imageData);
// Test metadata conversion
NSDictionary* metadata = [self.plugin convertImageMetadata:imageData];
// Metadata may be nil for generated images, but the method should not crash
// Real camera images would have EXIF data
XCTAssertTrue(metadata == nil || [metadata isKindOfClass:[NSDictionary class]]);
}
- (void)testPictureOptionsForPHPicker
{
NSArray* args;
CDVPictureOptions* options;
// Test options configuration for photo library (which would use PHPicker on iOS 14+)
args = @[
@(75),
@(DestinationTypeFileUri),
@(UIImagePickerControllerSourceTypePhotoLibrary),
@(800),
@(600),
@(EncodingTypeJPEG),
@(MediaTypePicture),
@NO,
@YES,
@NO,
[NSNull null],
@(UIImagePickerControllerCameraDeviceRear),
];
CDVInvokedUrlCommand* command = [[CDVInvokedUrlCommand alloc] initWithArguments:args callbackId:@"dummy" className:@"myclassname" methodName:@"mymethodname"];
options = [CDVPictureOptions createFromTakePictureArguments:command];
// Verify options are correctly set for photo library source
XCTAssertEqual(options.sourceType, (int)UIImagePickerControllerSourceTypePhotoLibrary);
XCTAssertEqual([options.quality intValue], 75);
XCTAssertEqual(options.destinationType, (int)DestinationTypeFileUri);
XCTAssertEqual(options.targetSize.width, 800);
XCTAssertEqual(options.targetSize.height, 600);
XCTAssertEqual(options.correctOrientation, YES);
XCTAssertEqual(options.mediaType, (int)MediaTypePicture);
}
@end

13
tests/package-lock.json generated Normal file
View File

@@ -0,0 +1,13 @@
{
"name": "cordova-plugin-camera-tests",
"version": "8.0.1-dev",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "cordova-plugin-camera-tests",
"version": "8.0.1-dev",
"license": "Apache-2.0"
}
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-camera-tests",
"version": "5.0.0",
"version": "9.0.0-dev",
"description": "",
"cordova": {
"id": "cordova-plugin-camera-tests",

View File

@@ -22,7 +22,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rim="http://www.blackberry.com/ns/widgets"
id="cordova-plugin-camera-tests"
version="5.0.0">
version="9.0.0-dev">
<name>Cordova Camera Plugin Tests</name>
<license>Apache 2.0</license>

View File

@@ -19,7 +19,7 @@
*
*/
/* globals Camera, resolveLocalFileSystemURL, FileEntry, CameraPopoverOptions, LocalFileSystem, MSApp */
/* globals Camera, resolveLocalFileSystemURL, FileEntry, LocalFileSystem */
/* eslint-env jasmine */
exports.defineAutoTests = function () {
@@ -78,20 +78,20 @@ exports.defineAutoTests = function () {
/******************************************************************************/
exports.defineManualTests = function (contentEl, createActionButton) {
var pictureUrl = null;
var fileObj = null;
var fileEntry = null;
var pageStartTime = +new Date();
let pictureUrl = null;
let fileObj = null;
let fileEntry = null;
const pageStartTime = +new Date();
// default camera options
var camQualityDefault = ['50', 50];
var camDestinationTypeDefault = ['FILE_URI', 1];
var camPictureSourceTypeDefault = ['CAMERA', 1];
var camAllowEditDefault = ['allowEdit', false];
var camEncodingTypeDefault = ['JPEG', 0];
var camMediaTypeDefault = ['mediaType', 0];
var camCorrectOrientationDefault = ['correctOrientation', false];
var camSaveToPhotoAlbumDefault = ['saveToPhotoAlbum', true];
const camQualityDefault = ['50', 50];
const camDestinationTypeDefault = ['FILE_URI', 1];
const camPictureSourceTypeDefault = ['CAMERA', 1];
const camAllowEditDefault = ['allowEdit', false];
const camEncodingTypeDefault = ['JPEG', 0];
const camMediaTypeDefault = ['mediaType', 0];
const camCorrectOrientationDefault = ['correctOrientation', false];
const camSaveToPhotoAlbumDefault = ['saveToPhotoAlbum', true];
function log (value) {
console.log(value);
@@ -101,7 +101,7 @@ exports.defineManualTests = function (contentEl, createActionButton) {
function clearStatus () {
document.getElementById('camera_status').innerHTML = '';
document.getElementById('camera_image').src = 'about:blank';
var canvas = document.getElementById('canvas');
const canvas = document.getElementById('canvas');
canvas.width = canvas.height = 1;
pictureUrl = null;
fileObj = null;
@@ -119,8 +119,8 @@ exports.defineManualTests = function (contentEl, createActionButton) {
log('URL: "' + url.slice(0, 90) + '"');
pictureUrl = url;
var img = document.getElementById('camera_image');
var startTime = new Date();
const img = document.getElementById('camera_image');
const startTime = new Date();
img.src = url;
img.onload = function () {
log('Img size: ' + img.naturalWidth + 'x' + img.naturalHeight);
@@ -147,22 +147,16 @@ exports.defineManualTests = function (contentEl, createActionButton) {
} else if (pictureUrl.indexOf('data:image/jpeg;base64') === 0) {
// do nothing
} else {
var path = pictureUrl.replace(/^file:\/\/(localhost)?/, '').replace(/%20/g, ' ');
const path = pictureUrl.replace(/^file:\/\/(localhost)?/, '').replace(/%20/g, ' ');
fileEntry = new FileEntry('image_name.png', path);
}
}
function getPicture () {
clearStatus();
var options = extractOptions();
const options = extractOptions();
log('Getting picture with options: ' + JSON.stringify(options));
var popoverHandle = navigator.camera.getPicture(getPictureWin, onGetPictureError, options);
// Reposition the popover if the orientation changes.
window.onorientationchange = function () {
var newPopoverOptions = new CameraPopoverOptions(0, 0, 100, 100, 0, 300, 400);
popoverHandle.setPosition(newPopoverOptions);
};
navigator.camera.getPicture(getPictureWin, onGetPictureError, options);
}
function logCallback (apiName, success) {
@@ -177,7 +171,7 @@ exports.defineManualTests = function (contentEl, createActionButton) {
*/
function readFile () {
function onFileReadAsDataURL (evt) {
var img = document.getElementById('camera_image');
const img = document.getElementById('camera_image');
img.style.visibility = 'visible';
img.style.display = 'block';
img.src = evt.target.result;
@@ -188,7 +182,7 @@ exports.defineManualTests = function (contentEl, createActionButton) {
log('Got file: ' + JSON.stringify(file));
fileObj = file;
/* eslint-disable no-undef */
var reader = new FileReader();
const reader = new FileReader();
/* eslint-enable no-undef */
reader.onload = function () {
log('FileReader.readAsDataURL() - length = ' + reader.result.length);
@@ -219,9 +213,9 @@ exports.defineManualTests = function (contentEl, createActionButton) {
* This calls FileEntry.copyTo and FileEntry.moveTo.
*/
function copyImage () {
var onFileSystemReceived = function (fileSystem) {
var destDirEntry = fileSystem.root;
var origName = fileEntry.name;
const onFileSystemReceived = function (fileSystem) {
const destDirEntry = fileSystem.root;
const origName = fileEntry.name;
// Test FileEntry API here.
fileEntry.copyTo(destDirEntry, 'copied_file.png', logCallback('FileEntry.copyTo', true), logCallback('FileEntry.copyTo', false));
@@ -253,13 +247,13 @@ exports.defineManualTests = function (contentEl, createActionButton) {
* This calls FileEntry.createWriter, FileWriter.write, and FileWriter.truncate.
*/
function writeImage () {
var onFileWriterReceived = function (fileWriter) {
const onFileWriterReceived = function (fileWriter) {
fileWriter.onwrite = logCallback('FileWriter.write', true);
fileWriter.onerror = logCallback('FileWriter.write', false);
fileWriter.write('some text!');
};
var onFileTruncateWriterReceived = function (fileWriter) {
const onFileTruncateWriterReceived = function (fileWriter) {
fileWriter.onwrite = logCallback('FileWriter.truncate', true);
fileWriter.onerror = logCallback('FileWriter.truncate', false);
fileWriter.truncate(10);
@@ -270,15 +264,15 @@ exports.defineManualTests = function (contentEl, createActionButton) {
}
function displayImageUsingCanvas () {
var canvas = document.getElementById('canvas');
var img = document.getElementById('camera_image');
var w = img.width;
var h = img.height;
const canvas = document.getElementById('canvas');
const img = document.getElementById('camera_image');
let w = img.width;
let h = img.height;
h = 100 / w * h;
w = 100;
canvas.width = w;
canvas.height = h;
var context = canvas.getContext('2d');
const context = canvas.getContext('2d');
context.drawImage(img, 0, 0, w, h);
}
@@ -311,9 +305,9 @@ exports.defineManualTests = function (contentEl, createActionButton) {
return;
}
/* eslint-enable no-undef */
var URLApi = window.URL || window.webkitURL;
const URLApi = window.URL || window.webkitURL;
if (URLApi) {
var blobURL = URLApi.createObjectURL(fileObj);
const blobURL = URLApi.createObjectURL(fileObj);
if (blobURL) {
setPicture(blobURL, function () {
URLApi.revokeObjectURL(blobURL);
@@ -327,11 +321,11 @@ exports.defineManualTests = function (contentEl, createActionButton) {
}
function extractOptions () {
var els = document.querySelectorAll('#image-options select');
var ret = {};
const els = document.querySelectorAll('#image-options select');
const ret = {};
/* eslint-disable no-cond-assign */
for (var i = 0, el; el = els[i]; ++i) {
var value = el.value;
for (let i = 0, el; el = els[i]; ++i) {
let value = el.value;
if (value === '') continue;
value = +value;
@@ -346,20 +340,20 @@ exports.defineManualTests = function (contentEl, createActionButton) {
}
function createOptionsEl (name, values, selectionDefault) {
var openDiv = '<div style="display: inline-block">' + name + ': ';
var select = '<select name=' + name + ' id="' + name + '">';
const openDiv = '<div style="display: inline-block">' + name + ': ';
const select = '<select name=' + name + ' id="' + name + '">';
var defaultOption = '';
let defaultOption = '';
if (selectionDefault === undefined) {
defaultOption = '<option value="">default</option>';
}
var options = '';
let options = '';
if (typeof values === 'boolean') {
values = { true: 1, false: 0 };
}
for (var k in values) {
var isSelected = '';
for (const k in values) {
let isSelected = '';
if (selectionDefault) {
if (selectionDefault[0] === k) {
isSelected = 'selected';
@@ -368,20 +362,20 @@ exports.defineManualTests = function (contentEl, createActionButton) {
options += '<option value="' + values[k] + '" ' + isSelected + '>' + k + '</option>';
}
var closeDiv = '</select></div>';
const closeDiv = '</select></div>';
return openDiv + select + defaultOption + options + closeDiv;
}
/******************************************************************************/
var info_div = '<h1>Camera</h1>' +
const info_div = '<h1>Camera</h1>' +
'<div id="info">' +
'<b>Status:</b> <div id="camera_status"></div>' +
'img: <img width="100" id="camera_image">' +
'canvas: <canvas id="canvas" width="1" height="1"></canvas>' +
'</div>';
var options_div = '<h2>Cordova Camera API Options</h2>' +
const options_div = '<h2>Cordova Camera API Options</h2>' +
'<div id="image-options">' +
createOptionsEl('sourceType', Camera.PictureSourceType, camPictureSourceTypeDefault) +
createOptionsEl('destinationType', Camera.DestinationType, camDestinationTypeDefault) +
@@ -395,8 +389,8 @@ exports.defineManualTests = function (contentEl, createActionButton) {
createOptionsEl('saveToPhotoAlbum', true, camSaveToPhotoAlbumDefault) +
createOptionsEl('cameraDirection', Camera.Direction) +
'</div>';
var getpicture_div = '<div id="getpicture"></div>';
var test_procedure = '<h4>Recommended Test Procedure</h4>' +
const getpicture_div = '<div id="getpicture"></div>';
const test_procedure = '<h4>Recommended Test Procedure</h4>' +
'Options not specified should be the default value' +
'<br>Status box should update with image and info whenever an image is taken or selected from library' +
'</p><div style="background:#B0C4DE;border:1px solid #FFA07A;margin:15px 6px 0px;min-width:295px;max-width:97%;padding:4px 0px 2px 10px;min-height:160px;max-height:200px;overflow:auto">' +
@@ -410,13 +404,13 @@ exports.defineManualTests = function (contentEl, createActionButton) {
'</p><li>sourceType=CAMERA<br>targetWidth & targetHeight=50<br>allowEdit=false<br>Do Get File Metadata test below and take note of size<br>Repeat test but with width and height=800. Size should be significantly larger.</li>' +
'</p><li>quality=0<br>targetWidth & targetHeight=default<br>allowEdit=false<br>Do Get File Metadata test below and take note of size<br>Repeat test but with quality=80. Size should be significantly larger.</li>' +
'</ol></div>';
var inputs_div = '<h2>Native File Inputs</h2>' +
const inputs_div = '<h2>Native File Inputs</h2>' +
'For the following tests, status box should update with file selected' +
'</p><div>input type=file <input type="file" class="testInputTag"></div>' +
'<div>capture=camera <input type="file" accept="image/*;capture=camera" class="testInputTag"></div>' +
'<div>capture=camcorder <input type="file" accept="video/*;capture=camcorder" class="testInputTag"></div>' +
'<div>capture=microphone <input type="file" accept="audio/*;capture=microphone" class="testInputTag"></div>';
var actions_div = '<h2>Actions</h2>' +
const actions_div = '<h2>Actions</h2>' +
'For the following tests, ensure that an image is set in status box' +
'</p><div id="metadata"></div>' +
'Expected result: Get metadata about file selected.<br>Status box will show, along with the metadata, "Call to FileEntry.getMetadata success, Call to FileEntry.setMetadata success, Call to FileEntry.getParent success"' +
@@ -433,22 +427,14 @@ exports.defineManualTests = function (contentEl, createActionButton) {
'</p><div id="remove"></div>' +
'Expected result: Remove image from library.<br>Status box will show "FileEntry.remove success:["OK"]';
// We need to wrap this code due to Windows security restrictions
// see http://msdn.microsoft.com/en-us/library/windows/apps/hh465380.aspx#differences for details
if (window.MSApp && window.MSApp.execUnsafeLocalFunction) {
MSApp.execUnsafeLocalFunction(function () {
contentEl.innerHTML = info_div + options_div + getpicture_div + test_procedure + inputs_div + actions_div;
});
} else {
contentEl.innerHTML = info_div + options_div + getpicture_div + test_procedure + inputs_div + actions_div;
}
contentEl.innerHTML = info_div + options_div + getpicture_div + test_procedure + inputs_div + actions_div;
var elements = document.getElementsByClassName('testInputTag');
var listener = function (e) {
const elements = document.getElementsByClassName('testInputTag');
const listener = function (e) {
testInputTag(e.target);
};
for (var i = 0; i < elements.length; ++i) {
var item = elements[i];
for (let i = 0; i < elements.length; ++i) {
const item = elements[i];
item.addEventListener('change', listener, false);
}

55
types/index.d.ts vendored
View File

@@ -36,11 +36,6 @@ interface Camera {
cameraSuccess: (data: string) => void,
cameraError: (message: string) => void,
cameraOptions?: CameraOptions): void;
// Next will work only on iOS
//getPicture(
// cameraSuccess: (data: string) => void,
// cameraError: (message: string) => void,
// cameraOptions?: CameraOptions): CameraPopoverHandle;
}
interface CameraOptions {
@@ -58,7 +53,7 @@ interface CameraOptions {
* Defined in navigator.camera.PictureSourceType. Default is CAMERA.
* PHOTOLIBRARY : 0,
* CAMERA : 1,
* SAVEDPHOTOALBUM : 2
* SAVEDPHOTOALBUM : 2 // same as PHOTOLIBRARY, kept for backward compatibility
*/
sourceType?: number;
/** Allow simple editing of image before selection. */
@@ -100,46 +95,6 @@ interface CameraOptions {
* BACK: 1
*/
cameraDirection?: number;
/** iOS-only options that specify popover location in iPad. Defined in CameraPopoverOptions. */
popoverOptions?: CameraPopoverOptions;
}
/**
* A handle to the popover dialog created by navigator.camera.getPicture. Used on iOS only.
*/
interface CameraPopoverHandle {
/**
* Set the position of the popover.
* @param popoverOptions the CameraPopoverOptions that specify the new position.
*/
setPosition(popoverOptions: CameraPopoverOptions): void;
}
/**
* iOS-only parameters that specify the anchor element location and arrow direction
* of the popover when selecting images from an iPad's library or album.
*/
interface CameraPopoverOptions {
x: number;
y: number;
width: number;
height: number;
/**
* Direction the arrow on the popover should point. Defined in Camera.PopoverArrowDirection
* Matches iOS UIPopoverArrowDirection constants.
* ARROW_UP : 1,
* ARROW_DOWN : 2,
* ARROW_LEFT : 4,
* ARROW_RIGHT : 8,
* ARROW_ANY : 15
*/
arrowDir : number;
popoverWidth: number;
popoverHeight: number;
}
declare class CameraPopoverOptions implements CameraPopoverOptions {
constructor(x?: number, y?: number, width?: number, height?: number, arrowDir?: number);
}
declare var Camera: {
@@ -166,12 +121,4 @@ declare var Camera: {
CAMERA: number;
SAVEDPHOTOALBUM: number;
}
// Used only on iOS
PopoverArrowDirection: {
ARROW_UP: number;
ARROW_DOWN: number;
ARROW_LEFT: number;
ARROW_RIGHT: number;
ARROW_ANY: number;
}
};

View File

@@ -19,11 +19,9 @@
*
*/
var argscheck = require('cordova/argscheck');
var exec = require('cordova/exec');
var Camera = require('./Camera');
// XXX: commented out
// CameraPopoverHandle = require('./CameraPopoverHandle');
const argscheck = require('cordova/argscheck');
const exec = require('cordova/exec');
const Camera = require('./Camera');
/**
* @namespace navigator
@@ -32,10 +30,10 @@ var Camera = require('./Camera');
/**
* @exports camera
*/
var cameraExport = {};
const cameraExport = {};
// Tack on the Camera Constants to the base camera plugin.
for (var key in Camera) {
for (const key in Camera) {
cameraExport[key] = Camera[key];
}
@@ -73,7 +71,6 @@ for (var key in Camera) {
* @property {module:Camera.MediaType} [mediaType=PICTURE] - Set the type of media to select from. Only works when `PictureSourceType` is `PHOTOLIBRARY` or `SAVEDPHOTOALBUM`.
* @property {Boolean} [correctOrientation] - Rotate the image to correct for the orientation of the device during capture.
* @property {Boolean} [saveToPhotoAlbum] - Save the image to the photo album on the device after capture.
* @property {module:CameraPopoverOptions} [popoverOptions] - iOS-only options that specify popover location in iPad.
* @property {module:Camera.Direction} [cameraDirection=BACK] - Choose the camera to use (front- or back-facing).
*/
@@ -114,14 +111,8 @@ for (var key in Camera) {
* __Supported Platforms__
*
* - Android
* - BlackBerry
* - Browser
* - Firefox
* - FireOS
* - iOS
* - Windows
* - WP8
* - Ubuntu
*
* More examples [here](#camera-getPicture-examples). Quirks [here](#camera-getPicture-quirks).
*
@@ -134,27 +125,28 @@ for (var key in Camera) {
cameraExport.getPicture = function (successCallback, errorCallback, options) {
argscheck.checkArgs('fFO', 'Camera.getPicture', arguments);
options = options || {};
var getValue = argscheck.getValue;
const getValue = argscheck.getValue;
var quality = getValue(options.quality, 50);
var destinationType = getValue(options.destinationType, Camera.DestinationType.FILE_URI);
var sourceType = getValue(options.sourceType, Camera.PictureSourceType.CAMERA);
var targetWidth = getValue(options.targetWidth, -1);
var targetHeight = getValue(options.targetHeight, -1);
var encodingType = getValue(options.encodingType, Camera.EncodingType.JPEG);
var mediaType = getValue(options.mediaType, Camera.MediaType.PICTURE);
var allowEdit = !!options.allowEdit;
var correctOrientation = !!options.correctOrientation;
var saveToPhotoAlbum = !!options.saveToPhotoAlbum;
var popoverOptions = getValue(options.popoverOptions, null);
var cameraDirection = getValue(options.cameraDirection, Camera.Direction.BACK);
const quality = getValue(options.quality, 50);
const destinationType = getValue(options.destinationType, Camera.DestinationType.FILE_URI);
const sourceType = getValue(options.sourceType, Camera.PictureSourceType.CAMERA);
const targetWidth = getValue(options.targetWidth, -1);
const targetHeight = getValue(options.targetHeight, -1);
const encodingType = getValue(options.encodingType, Camera.EncodingType.JPEG);
const mediaType = getValue(options.mediaType, Camera.MediaType.PICTURE);
const allowEdit = !!options.allowEdit;
const correctOrientation = !!options.correctOrientation;
const saveToPhotoAlbum = !!options.saveToPhotoAlbum;
const cameraDirection = getValue(options.cameraDirection, Camera.Direction.BACK);
var args = [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType,
mediaType, allowEdit, correctOrientation, saveToPhotoAlbum, popoverOptions, cameraDirection];
if (allowEdit) {
console.warn('allowEdit is deprecated. It does not work reliably on all platforms. Utilise a dedicated image editing library instead. allowEdit functionality is scheduled to be removed in a future release.');
}
const args = [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType,
mediaType, allowEdit, correctOrientation, saveToPhotoAlbum, cameraDirection];
exec(successCallback, errorCallback, 'Camera', 'takePicture', args);
// XXX: commented out
// return new CameraPopoverHandle();
};
/**

View File

@@ -62,24 +62,19 @@ module.exports = {
* @enum {number}
*/
PictureSourceType: {
/** Choose image from the device's photo library (same as SAVEDPHOTOALBUM for Android) */
/**
* Choose image from the device's photo library.
**/
PHOTOLIBRARY: 0,
/** Take picture from camera */
CAMERA: 1,
/** Choose image only from the device's Camera Roll album (same as PHOTOLIBRARY for Android) */
/**
* Same as PHOTOLIBRARY, when running on Android, or iOS 14+.
* On iOS older than 14, an image can only be chosen from the device's Camera Roll album
* with this setting.
**/
SAVEDPHOTOALBUM: 2
},
/**
* Matches iOS UIPopoverArrowDirection constants to specify arrow location on popover.
* @enum {number}
*/
PopoverArrowDirection: {
ARROW_UP: 1,
ARROW_DOWN: 2,
ARROW_LEFT: 4,
ARROW_RIGHT: 8,
ARROW_ANY: 15
},
/**
* @enum {number}
*/

View File

@@ -1,32 +0,0 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/**
* @ignore in favour of iOS' one
* A handle to an image picker popover.
*/
var CameraPopoverHandle = function () {
this.setPosition = function (popoverOptions) {
console.log('CameraPopoverHandle.setPosition is only supported on iOS.');
};
};
module.exports = CameraPopoverHandle;

View File

@@ -1,56 +0,0 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var Camera = require('./Camera');
/**
* @namespace navigator
*/
/**
* iOS-only parameters that specify the anchor element location and arrow
* direction of the popover when selecting images from an iPad's library
* or album.
* Note that the size of the popover may change to adjust to the
* direction of the arrow and orientation of the screen. Make sure to
* account for orientation changes when specifying the anchor element
* location.
* @module CameraPopoverOptions
* @param {Number} [x=0] - x pixel coordinate of screen element onto which to anchor the popover.
* @param {Number} [y=32] - y pixel coordinate of screen element onto which to anchor the popover.
* @param {Number} [width=320] - width, in pixels, of the screen element onto which to anchor the popover.
* @param {Number} [height=480] - height, in pixels, of the screen element onto which to anchor the popover.
* @param {module:Camera.PopoverArrowDirection} [arrowDir=ARROW_ANY] - Direction the arrow on the popover should point.
* @param {Number} [popoverWidth=0] - width of the popover (0 or not specified will use apple's default width).
* @param {Number} [popoverHeight=0] - height of the popover (0 or not specified will use apple's default height).
*/
var CameraPopoverOptions = function (x, y, width, height, arrowDir, popoverWidth, popoverHeight) {
// information of rectangle that popover should be anchored to
this.x = x || 0;
this.y = y || 32;
this.width = width || 320;
this.height = height || 480;
this.arrowDir = arrowDir || Camera.PopoverArrowDirection.ARROW_ANY;
this.popoverWidth = popoverWidth || 0;
this.popoverHeight = popoverHeight || 0;
};
module.exports = CameraPopoverOptions;

View File

@@ -1,66 +0,0 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var exec = require('cordova/exec');
/**
* @namespace navigator
*/
/**
* A handle to an image picker popover.
*
* __Supported Platforms__
*
* - iOS
*
* @example
* navigator.camera.getPicture(onSuccess, onFail,
* {
* destinationType: Camera.DestinationType.FILE_URI,
* sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
* popoverOptions: new CameraPopoverOptions(300, 300, 100, 100, Camera.PopoverArrowDirection.ARROW_ANY, 300, 600)
* });
*
* // Reposition the popover if the orientation changes.
* window.onorientationchange = function() {
* var cameraPopoverHandle = new CameraPopoverHandle();
* var cameraPopoverOptions = new CameraPopoverOptions(0, 0, 100, 100, Camera.PopoverArrowDirection.ARROW_ANY, 400, 500);
* cameraPopoverHandle.setPosition(cameraPopoverOptions);
* }
* @module CameraPopoverHandle
*/
var CameraPopoverHandle = function () {
/**
* Can be used to reposition the image selection dialog,
* for example, when the device orientation changes.
* @memberof CameraPopoverHandle
* @instance
* @method setPosition
* @param {module:CameraPopoverOptions} popoverOptions
*/
this.setPosition = function (popoverOptions) {
var args = [popoverOptions];
exec(null, null, 'Camera', 'repositionPopover', args);
};
};
module.exports = CameraPopoverHandle;