What I need to do is lower the quality of the images. Both those taken by the cameras or those loaded from the gallery. For that I change the following parameters width, height and quality
const takePhoto = async () => {
const cameraPhoto = await getPhoto({
resultType: CameraResultType.Uri,
source: CameraSource.Camera,
width:30,
height:30,
preserveAspectRatio:true,
quality: 50
});
But I don’t see any difference, when lowering the pixel size or the quality. Is there something I am doing wrong or missing in the code?
This is the code I use to take pictures with Capacitor:
export function usePhotoGallery() {
const [photos, setPhotos] = useState<Photo[]>([]);
const { getPhoto } = useCamera();
const { deleteFile, readFile, writeFile } = useFilesystem();
const { get, set } = useStorage();
const takePhoto = async () => {
const cameraPhoto = await getPhoto({
resultType: CameraResultType.Uri,
source: CameraSource.Camera,
width:30,
height:30,
preserveAspectRatio:true,
quality: 50
});
const fileName = new Date().getTime() + '.jpeg';
const savedFileImage = await savePicture(cameraPhoto, fileName);
const newPhotos = [savedFileImage, ...photos];
setPhotos(newPhotos);
set(PHOTO_STORAGE, JSON.stringify(newPhotos));
return newPhotos
};
const savePicture = async (photo: CameraPhoto, fileName: string): Promise<Photo> => {
let base64Data: string;
// "hybrid" will detect Cordova or Capacitor;
if (isPlatform('hybrid')) {
const file = await readFile({
path: photo.path!
});
base64Data = file.data;
} else {
base64Data = await base64FromPath(photo.webPath!);
}
const savedFile = await writeFile({
path: fileName,
data: base64Data,
directory: FilesystemDirectory.Data
});
if (isPlatform('hybrid')) {
// Display the new image by rewriting the 'file://' path to HTTP
// Details: https://ionicframework.com/docs/building/webview#file-protocol
return {
filepath: savedFile.uri,
webviewPath: Capacitor.convertFileSrc(savedFile.uri),
};
}
else {
// Use webPath to display the new image instead of base64 since it's
// already loaded into memory
return {
filepath: fileName,
webviewPath: photo.webPath
};
}
};
const deletePhoto = async (photo: Photo) => {
// Remove this photo from the Photos reference data array
const newPhotos = photos.filter(p => p.filepath !== photo.filepath);
// Update photos array cache by overwriting the existing photo array
set(PHOTO_STORAGE, JSON.stringify(newPhotos));
// delete photo file from filesystem
const filename = photo.filepath.substr(photo.filepath.lastIndexOf('/') + 1);
await deleteFile({
path: filename,
directory: FilesystemDirectory.Data
});
setPhotos(newPhotos);
};
return {
deletePhoto,
photos,
takePhoto
};
}
export interface Photo {
filepath: string;
webviewPath?: string;
}
1 post - 1 participant