Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(camera): Add size to returned object #1873

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions camera/README.md
Expand Up @@ -216,6 +216,7 @@ Request camera and photo album permissions
| **`exif`** | <code>any</code> | Exif data, if any, retrieved from the image | 1.0.0 |
| **`format`** | <code>string</code> | The format of the image, ex: jpeg, png, gif. iOS and Android only support jpeg. Web supports jpeg, png and gif, but the exact availability may vary depending on the browser. gif is only supported if `webUseInput` is set to `true` or if `source` is set to `Photos`. | 1.0.0 |
| **`saved`** | <code>boolean</code> | Whether if the image was saved to the gallery or not. On Android and iOS, saving to the gallery can fail if the user didn't grant the required permissions. On Web there is no gallery, so always returns false. | 1.1.0 |
| **`size`** | <code>number</code> | The size of the imagefile in bytes | 5.?.0 |


#### ImageOptions
Expand Down Expand Up @@ -254,6 +255,7 @@ Request camera and photo album permissions
| **`webPath`** | <code>string</code> | webPath returns a path that can be used to set the src attribute of an image for efficient loading and rendering. | 1.2.0 |
| **`exif`** | <code>any</code> | Exif data, if any, retrieved from the image | 1.2.0 |
| **`format`** | <code>string</code> | The format of the image, ex: jpeg, png, gif. iOS and Android only support jpeg. Web supports jpeg, png and gif. | 1.2.0 |
| **`size`** | <code>number</code> | The size of the imagefile in bytes | 5.?.0 |


#### GalleryImageOptions
Expand Down
Expand Up @@ -725,6 +725,7 @@ private void returnFileURI(PluginCall call, ExifWrapper exif, Bitmap bitmap, Uri
ret.put("path", newUri.toString());
ret.put("webPath", FileUtils.getPortablePath(getContext(), bridge.getLocalUrl(), newUri));
ret.put("saved", isSaved);
ret.put("size", bitmapOutputStream.size());
call.resolve(ret);
} else {
call.reject(UNABLE_TO_PROCESS_IMAGE);
Expand Down Expand Up @@ -787,6 +788,7 @@ private void returnDataUrl(PluginCall call, ExifWrapper exif, ByteArrayOutputStr
data.put("format", "jpeg");
data.put("dataUrl", "data:image/jpeg;base64," + encoded);
data.put("exif", exif.toJson());
data.put("size", bitmapOutputStream.size());
call.resolve(data);
}

Expand All @@ -798,6 +800,7 @@ private void returnBase64(PluginCall call, ExifWrapper exif, ByteArrayOutputStre
data.put("format", "jpeg");
data.put("base64String", encoded);
data.put("exif", exif.toJson());
data.put("size", bitmapOutputStream.size());
call.resolve(data);
}

Expand Down
15 changes: 10 additions & 5 deletions camera/ios/Plugin/CameraPlugin.swift
Expand Up @@ -321,7 +321,8 @@ private extension CameraPlugin {
"path": fileURL.absoluteString,
"exif": processedImage.exifData,
"webPath": webURL.absoluteString,
"format": "jpeg"
"format": "jpeg",
"size": jpeg.count
]]
])
return
Expand All @@ -331,21 +332,24 @@ private extension CameraPlugin {
"exif": processedImage.exifData,
"webPath": webURL.absoluteString,
"format": "jpeg",
"saved": isSaved
"saved": isSaved,
"size": jpeg.count
])
} else if settings.resultType == CameraResultType.base64 {
self.call?.resolve([
"base64String": jpeg.base64EncodedString(),
"exif": processedImage.exifData,
"format": "jpeg",
"saved": isSaved
"saved": isSaved,
"size": jpeg.count
])
} else if settings.resultType == CameraResultType.dataURL {
call?.resolve([
"dataUrl": "data:image/jpeg;base64," + jpeg.base64EncodedString(),
"exif": processedImage.exifData,
"format": "jpeg",
"saved": isSaved
"saved": isSaved,
"size": jpeg.count
])
}
}
Expand All @@ -368,7 +372,8 @@ private extension CameraPlugin {
"path": fileURL.absoluteString,
"exif": processedImage.exifData,
"webPath": webURL.absoluteString,
"format": "jpeg"
"format": "jpeg",
"size": jpeg.count
])
}
call?.resolve([
Expand Down
12 changes: 12 additions & 0 deletions camera/src/definitions.ts
Expand Up @@ -237,6 +237,12 @@ export interface Photo {
* @since 1.1.0
*/
saved: boolean;
/**
* The size of the imagefile in bytes.
*
* @since 5.?.0
*/
size: number;
}

export interface GalleryPhotos {
Expand Down Expand Up @@ -277,6 +283,12 @@ export interface GalleryPhoto {
* @since 1.2.0
*/
format: string;
/**
* The size of the imagefile in bytes.
*
* @since 5.?.0
*/
size: number;
}
export interface GalleryImageOptions {
/**
Expand Down
10 changes: 10 additions & 0 deletions camera/src/web.ts
Expand Up @@ -106,6 +106,7 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
input.addEventListener('change', (_e: any) => {
const file = input.files![0];
let format = 'jpeg';
const filesize = file.size;

if (file.type === 'image/png') {
format = 'png';
Expand All @@ -124,12 +125,14 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
resolve({
dataUrl: reader.result,
format,
size: filesize,
} as Photo);
} else if (options.resultType === 'base64') {
const b64 = (reader.result as string).split(',')[1];
resolve({
base64String: b64,
format,
size: filesize,
} as Photo);
}

Expand All @@ -141,6 +144,7 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
resolve({
webPath: URL.createObjectURL(file),
format: format,
size: filesize,
});
cleanup();
}
Expand Down Expand Up @@ -186,6 +190,7 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
for (let i = 0; i < input.files!.length; i++) {
const file = input.files![i];
let format = 'jpeg';
const filesize = file.size;

if (file.type === 'image/png') {
format = 'png';
Expand All @@ -195,6 +200,7 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
photos.push({
webPath: URL.createObjectURL(file),
format: format,
size: filesize,
});
}
resolve({ photos });
Expand All @@ -211,11 +217,13 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
return new Promise<Photo>((resolve, reject) => {
const reader = new FileReader();
const format = photo.type.split('/')[1];
const fileSize = phoyo.size;
if (options.resultType === 'uri') {
resolve({
webPath: URL.createObjectURL(photo),
format: format,
saved: false,
size: filesize,
});
} else {
reader.readAsDataURL(photo);
Expand All @@ -226,12 +234,14 @@ export class CameraWeb extends WebPlugin implements CameraPlugin {
dataUrl: r,
format: format,
saved: false,
size: filesize,
});
} else {
resolve({
base64String: r.split(',')[1],
format: format,
saved: false,
size: filesize,
});
}
};
Expand Down